55import android .app .Service ;
66import android .content .Context ;
77import android .content .Intent ;
8+ import android .content .pm .ApplicationInfo ;
89import android .os .Bundle ;
910import android .os .IBinder ;
1011import android .os .Process ;
1415public class PythonService extends Service implements Runnable {
1516 private static String TAG = PythonService .class .getSimpleName ();
1617
17- public static PythonService mService = null ;
1818 /**
1919 * Intent that started the service
2020 */
@@ -31,15 +31,8 @@ public class PythonService extends Service implements Runnable {
3131 private String serviceEntrypoint ;
3232 private String pythonServiceArgument ;
3333
34- private boolean autoRestartService = false ;
35-
36- public void setAutoRestartService (boolean restart ) {
37- autoRestartService = restart ;
38- }
39-
40- public boolean canDisplayNotification () {
41- return true ;
42- }
34+ protected boolean autoRestartService = false ;
35+ protected boolean startForeground = true ;
4336
4437 public int startType () {
4538 return START_NOT_STICKY ;
@@ -88,22 +81,24 @@ public int onStartCommand(Intent intent, int flags, int startId) {
8881 pythonThread = new Thread (this );
8982 pythonThread .start ();
9083
91- if (canDisplayNotification () ) {
84+ if (startForeground ) {
9285 doStartForeground (extras );
9386 }
9487
9588 return startType ();
9689 }
9790
9891 protected void doStartForeground (Bundle extras ) {
99- String serviceTitle = extras . getString ( "serviceTitle" );
100- String serviceDescription = extras . getString ( "serviceDescription" );
92+ Context appContext = getApplicationContext ( );
93+ ApplicationInfo appInfo = appContext . getApplicationInfo ( );
10194
102- Context context = getApplicationContext ();
95+ String serviceTitle = extras .getString ("serviceTitle" , TAG );
96+ String serviceDescription = extras .getString ("serviceDescription" , "" );
97+ int serviceIconId = extras .getInt ("serviceIconId" , appInfo .icon );
10398
10499 NotificationCompat .Builder builder =
105100 new NotificationCompat .Builder (this )
106- .setSmallIcon (context . getApplicationInfo (). icon )
101+ .setSmallIcon (serviceIconId )
107102 .setContentTitle (serviceTitle )
108103 .setContentText (serviceDescription );
109104
@@ -136,7 +131,6 @@ public void onDestroy() {
136131 @ Override
137132 public void run () {
138133 PythonUtil .loadLibraries (getFilesDir ());
139- mService = this ;
140134 nativeStart (androidPrivate , androidArgument , serviceEntrypoint ,
141135 pythonName , pythonHome , pythonPath , pythonServiceArgument );
142136 stopSelf ();
0 commit comments