@@ -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
0 commit comments