Skip to content

Commit 7ad96b2

Browse files
committed
add sticky services, auto-restart services, fix foreground option
1 parent 1664b8a commit 7ad96b2

File tree

3 files changed

+49
-19
lines changed

3 files changed

+49
-19
lines changed

pythonforandroid/bootstraps/sdl2/build/build.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,8 @@ def make_package(args):
311311
entrypoint = spec[1]
312312
options = spec[2:]
313313

314-
foreground = False
315-
if 'foreground' in options:
316-
foreground = True
314+
foreground = 'foreground' in options
315+
sticky = 'sticky' in options
317316

318317
service_names.append(name)
319318
render(
@@ -323,6 +322,7 @@ def make_package(args):
323322
entrypoint=entrypoint,
324323
args=args,
325324
foreground=foreground,
325+
sticky=sticky,
326326
service_id=sid + 1,
327327
)
328328

pythonforandroid/bootstraps/sdl2/build/src/org/kivy/android/PythonService.java

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,23 @@ public class PythonService extends Service implements Runnable {
2929
private String serviceEntrypoint;
3030
// Argument to pass to Python code,
3131
private String pythonServiceArgument;
32-
public static Service mService = null;
32+
public static PythonService mService = null;
33+
private Intent startIntent = null;
34+
35+
private boolean autoRestartService = false;
36+
37+
public void setAutoRestartService(boolean restart) {
38+
autoRestartService = restart;
39+
}
3340

3441
public boolean canDisplayNotification() {
3542
return true;
3643
}
3744

45+
public int startType() {
46+
return START_NOT_STICKY;
47+
}
48+
3849
@Override
3950
public IBinder onBind(Intent arg0) {
4051
return null;
@@ -52,6 +63,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
5263
return START_NOT_STICKY;
5364
}
5465

66+
startIntent = intent;
5567
Bundle extras = intent.getExtras();
5668
androidPrivate = extras.getString("androidPrivate");
5769
androidArgument = extras.getString("androidArgument");
@@ -64,31 +76,35 @@ public int onStartCommand(Intent intent, int flags, int startId) {
6476
pythonThread = new Thread(this);
6577
pythonThread.start();
6678

67-
doStartForeground(extras);
79+
if (canDisplayNotification()) {
80+
doStartForeground(extras);
81+
}
6882

69-
return START_NOT_STICKY;
83+
return startType();
7084
}
7185

7286
protected void doStartForeground(Bundle extras) {
73-
if (canDisplayNotification()) {
74-
String serviceTitle = extras.getString("serviceTitle");
75-
String serviceDescription = extras.getString("serviceDescription");
76-
77-
Context context = getApplicationContext();
78-
Notification notification = new Notification(context.getApplicationInfo().icon,
79-
serviceTitle, System.currentTimeMillis());
80-
Intent contextIntent = new Intent(context, PythonActivity.class);
81-
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
82-
PendingIntent.FLAG_UPDATE_CURRENT);
83-
notification.setLatestEventInfo(context, serviceTitle, serviceDescription, pIntent);
84-
startForeground(1, notification);
85-
}
87+
String serviceTitle = extras.getString("serviceTitle");
88+
String serviceDescription = extras.getString("serviceDescription");
89+
90+
Context context = getApplicationContext();
91+
Notification notification = new Notification(context.getApplicationInfo().icon,
92+
serviceTitle, System.currentTimeMillis());
93+
Intent contextIntent = new Intent(context, PythonActivity.class);
94+
PendingIntent pIntent = PendingIntent.getActivity(context, 0, contextIntent,
95+
PendingIntent.FLAG_UPDATE_CURRENT);
96+
notification.setLatestEventInfo(context, serviceTitle, serviceDescription, pIntent);
97+
startForeground(1, notification);
8698
}
8799

88100
@Override
89101
public void onDestroy() {
90102
super.onDestroy();
91103
pythonThread = null;
104+
if (autoRestartService && startIntent != null) {
105+
Log.v("python service", "service restart requested");
106+
startService(startIntent);
107+
}
92108
Process.killProcess(Process.myPid());
93109
}
94110

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,20 @@
1010

1111

1212
public class Service{{ name|capitalize }} extends PythonService {
13+
{% if sticky %}
14+
@Override
15+
public int startType() {
16+
return START_STICKY;
17+
}
18+
{% endif %}
19+
20+
{% if not foreground %}
21+
@Override
22+
public boolean canDisplayNotification() {
23+
return false;
24+
}
25+
{% endif %}
26+
1327
@Override
1428
protected void doStartForeground(Bundle extras) {
1529
Context context = getApplicationContext();

0 commit comments

Comments
 (0)