Skip to content

Commit df54ad8

Browse files
committed
fix STICKY for foreground services
1 parent c0f63c6 commit df54ad8

File tree

2 files changed

+43
-61
lines changed

2 files changed

+43
-61
lines changed

pythonforandroid/bootstraps/common/build/src/main/java/org/kivy/android/PythonService.java

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ public class PythonService extends Service implements Runnable {
2525
private Thread pythonThread = null;
2626

2727
// Python environment variables
28-
protected String androidPrivate;
29-
protected String androidArgument;
30-
protected String pythonName;
31-
protected String pythonHome;
32-
protected String pythonPath;
33-
protected String serviceEntrypoint;
28+
private String androidPrivate;
29+
private String androidArgument;
30+
private String pythonName;
31+
private String pythonHome;
32+
private String pythonPath;
33+
private String serviceEntrypoint;
3434
// Argument to pass to Python code,
35-
protected String pythonServiceArgument;
35+
private String pythonServiceArgument;
3636

3737

3838
public static PythonService mService = null;
@@ -60,36 +60,33 @@ public void onCreate() {
6060

6161
@Override
6262
public int onStartCommand(Intent intent, int flags, int startId) {
63-
startIntent = intent;
6463
if (pythonThread != null) {
6564
Log.v("python service", "service exists, do not start again");
6665
return startType();
6766
}
68-
//when app is closed, OS will restart the service with null intent See #2401
69-
if (intent != null) {
70-
71-
Bundle extras = intent.getExtras();
72-
androidPrivate = extras.getString("androidPrivate");
73-
androidArgument = extras.getString("androidArgument");
74-
serviceEntrypoint = extras.getString("serviceEntrypoint");
75-
pythonName = extras.getString("pythonName");
76-
pythonHome = extras.getString("pythonHome");
77-
pythonPath = extras.getString("pythonPath");
78-
boolean serviceStartAsForeground = false;
79-
String foreground = extras.getString("serviceStartAsForeground");
80-
if (foreground != null) {
81-
serviceStartAsForeground = foreground.equals("true");
82-
}
83-
pythonServiceArgument = extras.getString("pythonServiceArgument");
84-
pythonThread = new Thread(this);
85-
pythonThread.start();
67+
//intent is null if OS restarts a STICKY service
68+
if (intent == null) {
69+
Context context = getApplicationContext();
70+
intent = getThisDefaultIntent(context, "");
71+
}
8672

87-
if (serviceStartAsForeground) {
88-
doStartForeground(extras);
89-
}
90-
} else {
91-
pythonThread = new Thread(this);
92-
pythonThread.start();
73+
startIntent = intent;
74+
Bundle extras = intent.getExtras();
75+
androidPrivate = extras.getString("androidPrivate");
76+
androidArgument = extras.getString("androidArgument");
77+
serviceEntrypoint = extras.getString("serviceEntrypoint");
78+
pythonName = extras.getString("pythonName");
79+
pythonHome = extras.getString("pythonHome");
80+
pythonPath = extras.getString("pythonPath");
81+
boolean serviceStartAsForeground = (
82+
extras.getString("serviceStartAsForeground").equals("true")
83+
);
84+
pythonServiceArgument = extras.getString("pythonServiceArgument");
85+
pythonThread = new Thread(this);
86+
pythonThread.start();
87+
88+
if (serviceStartAsForeground) {
89+
doStartForeground(extras);
9390
}
9491

9592
return startType();
@@ -99,6 +96,10 @@ protected int getServiceId() {
9996
return 1;
10097
}
10198

99+
protected Intent getThisDefaultIntent(Context ctx, String pythonServiceArgument) {
100+
return null;
101+
}
102+
102103
protected void doStartForeground(Bundle extras) {
103104
String serviceTitle = extras.getString("serviceTitle");
104105
String serviceDescription = extras.getString("serviceDescription");
@@ -161,7 +162,8 @@ public void onDestroy() {
161162
@Override
162163
public void onTaskRemoved(Intent rootIntent) {
163164
super.onTaskRemoved(rootIntent);
164-
if (startType() == START_NOT_STICKY) {
165+
//sticky servcie runtime/restart is managed by the OS. leave it running when app is closed
166+
if (startType() != START_STICKY) {
165167
stopSelf();
166168
}
167169
}

pythonforandroid/bootstraps/common/build/templates/Service.tmpl.java

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import android.content.Intent;
44
import android.content.Context;
55
import org.kivy.android.PythonService;
6-
import java.io.File;
76

87

98
public class Service{{ name|capitalize }} extends PythonService {
@@ -20,6 +19,11 @@ protected int getServiceId() {
2019
}
2120

2221
static public void start(Context ctx, String pythonServiceArgument) {
22+
Intent intent = getDefaultIntent(ctx, pythonServiceArgument);
23+
ctx.startService(intent);
24+
}
25+
26+
static public Intent getDefaultIntent(Context ctx, String pythonServiceArgument) {
2327
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
2428
String argument = ctx.getFilesDir().getAbsolutePath() + "/app";
2529
intent.putExtra("androidPrivate", ctx.getFilesDir().getAbsolutePath());
@@ -32,36 +36,12 @@ static public void start(Context ctx, String pythonServiceArgument) {
3236
intent.putExtra("pythonHome", argument);
3337
intent.putExtra("pythonPath", argument + ":" + argument + "/lib");
3438
intent.putExtra("pythonServiceArgument", pythonServiceArgument);
35-
ctx.startService(intent);
39+
return intent;
3640
}
3741

3842
@Override
39-
public void run(){
40-
String package_root = getFilesDir().getAbsolutePath();
41-
String app_root = package_root + "/app";
42-
File app_root_file = new File(app_root);
43-
if (androidPrivate == null) {
44-
androidPrivate = package_root;
45-
}
46-
if (androidArgument == null) {
47-
androidArgument = app_root;
48-
}
49-
if (serviceEntrypoint == null) {
50-
serviceEntrypoint ="{{ entrypoint }}";
51-
}
52-
if (pythonName == null) {
53-
pythonName = "{{ name }}";
54-
}
55-
if (pythonHome == null) {
56-
pythonHome = app_root;
57-
}
58-
if (pythonPath == null) {
59-
pythonPath = package_root;
60-
}
61-
if (pythonServiceArgument == null) {
62-
pythonServiceArgument = app_root+":"+app_root+"/lib";
63-
}
64-
super.run();
43+
protected Intent getThisDefaultIntent(Context ctx, String pythonServiceArgument) {
44+
return Service{{ name|capitalize }}.getDefaultIntent(ctx, pythonServiceArgument);
6545
}
6646

6747
static public void stop(Context ctx) {

0 commit comments

Comments
 (0)