@@ -29,12 +29,23 @@ public class PythonService extends Service implements Runnable {
29
29
private String serviceEntrypoint ;
30
30
// Argument to pass to Python code,
31
31
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
+ }
33
40
34
41
public boolean canDisplayNotification () {
35
42
return true ;
36
43
}
37
44
45
+ public int startType () {
46
+ return START_NOT_STICKY ;
47
+ }
48
+
38
49
@ Override
39
50
public IBinder onBind (Intent arg0 ) {
40
51
return null ;
@@ -52,6 +63,7 @@ public int onStartCommand(Intent intent, int flags, int startId) {
52
63
return START_NOT_STICKY ;
53
64
}
54
65
66
+ startIntent = intent ;
55
67
Bundle extras = intent .getExtras ();
56
68
androidPrivate = extras .getString ("androidPrivate" );
57
69
androidArgument = extras .getString ("androidArgument" );
@@ -64,31 +76,35 @@ public int onStartCommand(Intent intent, int flags, int startId) {
64
76
pythonThread = new Thread (this );
65
77
pythonThread .start ();
66
78
67
- doStartForeground (extras );
79
+ if (canDisplayNotification ()) {
80
+ doStartForeground (extras );
81
+ }
68
82
69
- return START_NOT_STICKY ;
83
+ return startType () ;
70
84
}
71
85
72
86
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 );
86
98
}
87
99
88
100
@ Override
89
101
public void onDestroy () {
90
102
super .onDestroy ();
91
103
pythonThread = null ;
104
+ if (autoRestartService && startIntent != null ) {
105
+ Log .v ("python service" , "service restart requested" );
106
+ startService (startIntent );
107
+ }
92
108
Process .killProcess (Process .myPid ());
93
109
}
94
110
0 commit comments