Skip to content

Commit 4ecaa5f

Browse files
authored
services: fix START_STICKY (#2401)
* fix sticky * cleanup * fix STICKY for foreground services
1 parent 06dd1e0 commit 4ecaa5f

File tree

2 files changed

+26
-4
lines changed

2 files changed

+26
-4
lines changed

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

+15-3
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,15 @@ public void onCreate() {
6262
public int onStartCommand(Intent intent, int flags, int startId) {
6363
if (pythonThread != null) {
6464
Log.v("python service", "service exists, do not start again");
65-
return START_NOT_STICKY;
65+
return startType();
66+
}
67+
//intent is null if OS restarts a STICKY service
68+
if (intent == null) {
69+
Context context = getApplicationContext();
70+
intent = getThisDefaultIntent(context, "");
6671
}
6772

68-
startIntent = intent;
73+
startIntent = intent;
6974
Bundle extras = intent.getExtras();
7075
androidPrivate = extras.getString("androidPrivate");
7176
androidArgument = extras.getString("androidArgument");
@@ -91,6 +96,10 @@ protected int getServiceId() {
9196
return 1;
9297
}
9398

99+
protected Intent getThisDefaultIntent(Context ctx, String pythonServiceArgument) {
100+
return null;
101+
}
102+
94103
protected void doStartForeground(Bundle extras) {
95104
String serviceTitle = extras.getString("serviceTitle");
96105
String serviceDescription = extras.getString("serviceDescription");
@@ -153,7 +162,10 @@ public void onDestroy() {
153162
@Override
154163
public void onTaskRemoved(Intent rootIntent) {
155164
super.onTaskRemoved(rootIntent);
156-
stopSelf();
165+
//sticky servcie runtime/restart is managed by the OS. leave it running when app is closed
166+
if (startType() != START_STICKY) {
167+
stopSelf();
168+
}
157169
}
158170

159171
@Override

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

+11-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ protected int getServiceId() {
1919
}
2020

2121
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) {
2227
Intent intent = new Intent(ctx, Service{{ name|capitalize }}.class);
2328
String argument = ctx.getFilesDir().getAbsolutePath() + "/app";
2429
intent.putExtra("androidPrivate", ctx.getFilesDir().getAbsolutePath());
@@ -31,7 +36,12 @@ static public void start(Context ctx, String pythonServiceArgument) {
3136
intent.putExtra("pythonHome", argument);
3237
intent.putExtra("pythonPath", argument + ":" + argument + "/lib");
3338
intent.putExtra("pythonServiceArgument", pythonServiceArgument);
34-
ctx.startService(intent);
39+
return intent;
40+
}
41+
42+
@Override
43+
protected Intent getThisDefaultIntent(Context ctx, String pythonServiceArgument) {
44+
return Service{{ name|capitalize }}.getDefaultIntent(ctx, pythonServiceArgument);
3545
}
3646

3747
static public void stop(Context ctx) {

0 commit comments

Comments
 (0)