Skip to content

Commit 7b4f92b

Browse files
committed
Killing the service gracefully
1 parent 002bd71 commit 7b4f92b

3 files changed

Lines changed: 26 additions & 14 deletions

File tree

app/src/main/java/com/njlabs/showjava/processor/ProcessService.java

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class ProcessService extends Service {
5050

5151
public String decompilerToUse = "cfr";
5252

53+
public int startID;
5354

5455
public void onCreate() {
5556
super.onCreate();
@@ -61,6 +62,7 @@ public void onCreate() {
6162
public int onStartCommand(Intent intent, int flags, int startId) {
6263
super.onStartCommand(intent, flags, startId);
6364

65+
this.startID = startId;
6466
/**
6567
* Initialize a handler for posting runnables that have to run on the UI thread
6668
*/
@@ -93,8 +95,12 @@ public int onStartCommand(Intent intent, int flags, int startId) {
9395
*
9496
* Uses the {@link #killSelf()} method.
9597
*/
96-
killSelf();
9798

99+
int toKillStartId = intent.getIntExtra("startId",-1);
100+
killSelf(true, toKillStartId);
101+
102+
} else if(intent.getAction().equals(Constants.ACTION.STOP_PROCESS_FOR_NEW)) {
103+
killSelf(false, -1);
98104
}
99105

100106
return START_NOT_STICKY;
@@ -164,7 +170,7 @@ public void run() {
164170
}
165171
})).start();
166172
} else {
167-
killSelf();
173+
killSelf(true, startID);
168174
}
169175
}
170176

@@ -297,6 +303,7 @@ private Notification buildNotification() {
297303

298304
Intent stopIntent = new Intent(this, ProcessService.class);
299305
stopIntent.setAction(Constants.ACTION.STOP_PROCESS);
306+
stopIntent.putExtra("startID", startID);
300307

301308
PendingIntent pendingStopIntent = PendingIntent.getService(this, 0, stopIntent, 0);
302309

@@ -326,7 +333,6 @@ private Notification buildNotification() {
326333
public void onDestroy() {
327334
super.onDestroy();
328335
try {
329-
kill();
330336
processNotify.cancel();
331337
} catch (Exception e) {
332338
Ln.e(e);
@@ -358,16 +364,18 @@ public void run() {
358364
}
359365
}
360366

361-
private void killSelf(){
362-
broadcastStatus("exit");
367+
private void killSelf(boolean shouldBroadcast, int startID){
368+
if(shouldBroadcast){
369+
broadcastStatus("exit");
370+
}
363371
stopForeground(true);
372+
stopSelf(startID);
364373
try {
365374
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
366375
mNotifyManager.cancel(Constants.PROCESS_NOTIFICATION_ID);
367-
Utils.killAllProcessorServices(this);
376+
Utils.forceKillAllProcessorServices(this);
368377
} catch (Exception e) {
369378
Ln.e(e);
370379
}
371-
stopSelf();
372380
}
373381
}

app/src/main/java/com/njlabs/showjava/ui/AppProcessActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void run() {
129129
}
130130

131131
private void startProcessorService() {
132-
Utils.killAllProcessorServices(this);
132+
Utils.killAllProcessorServices(this, true);
133133
Intent mServiceIntent = new Intent(getContext(), ProcessService.class);
134134
mServiceIntent.setAction(Constants.ACTION.START_PROCESS);
135135
mServiceIntent.putExtra("package_file_path", packageFilePath);
@@ -253,7 +253,6 @@ public void onReceive(Context context, Intent intent) {
253253
break;
254254

255255
case "exit":
256-
Toast.makeText(baseContext, "Exiting.", Toast.LENGTH_SHORT).show();
257256
finish();
258257
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
259258
break;

app/src/main/java/com/njlabs/showjava/utils/Utils.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,18 @@
1818

1919
public class Utils {
2020

21-
public static void killAllProcessorServices(Context context) {
22-
21+
public static void killAllProcessorServices(Context context, boolean forNew) {
2322
Intent mServiceIntent = new Intent(context, ProcessService.class);
24-
mServiceIntent.setAction(Constants.ACTION.STOP_PROCESS);
25-
context.startService(mServiceIntent);
23+
if(forNew){
24+
mServiceIntent.setAction(Constants.ACTION.STOP_PROCESS_FOR_NEW);
25+
} else {
26+
mServiceIntent.setAction(Constants.ACTION.STOP_PROCESS);
27+
}
28+
context.stopService(mServiceIntent);
29+
}
2630

27-
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
31+
public static void forceKillAllProcessorServices(Context context) {
32+
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
2833
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses();
2934
for (ActivityManager.RunningAppProcessInfo next : runningAppProcesses) {
3035
String processName = context.getPackageName() + ":service";

0 commit comments

Comments
 (0)