5
5
import android .app .Service ;
6
6
import android .content .Context ;
7
7
import android .content .Intent ;
8
+ import android .content .pm .ApplicationInfo ;
8
9
import android .os .Bundle ;
9
10
import android .os .IBinder ;
10
11
import android .os .Process ;
14
15
public class PythonService extends Service implements Runnable {
15
16
private static String TAG = PythonService .class .getSimpleName ();
16
17
17
- public static PythonService mService = null ;
18
18
/**
19
19
* Intent that started the service
20
20
*/
@@ -31,15 +31,8 @@ public class PythonService extends Service implements Runnable {
31
31
private String serviceEntrypoint ;
32
32
private String pythonServiceArgument ;
33
33
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 ;
43
36
44
37
public int startType () {
45
38
return START_NOT_STICKY ;
@@ -88,22 +81,24 @@ public int onStartCommand(Intent intent, int flags, int startId) {
88
81
pythonThread = new Thread (this );
89
82
pythonThread .start ();
90
83
91
- if (canDisplayNotification () ) {
84
+ if (startForeground ) {
92
85
doStartForeground (extras );
93
86
}
94
87
95
88
return startType ();
96
89
}
97
90
98
91
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 ( );
101
94
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 );
103
98
104
99
NotificationCompat .Builder builder =
105
100
new NotificationCompat .Builder (this )
106
- .setSmallIcon (context . getApplicationInfo (). icon )
101
+ .setSmallIcon (serviceIconId )
107
102
.setContentTitle (serviceTitle )
108
103
.setContentText (serviceDescription );
109
104
@@ -136,7 +131,6 @@ public void onDestroy() {
136
131
@ Override
137
132
public void run () {
138
133
PythonUtil .loadLibraries (getFilesDir ());
139
- mService = this ;
140
134
nativeStart (androidPrivate , androidArgument , serviceEntrypoint ,
141
135
pythonName , pythonHome , pythonPath , pythonServiceArgument );
142
136
stopSelf ();
0 commit comments