Skip to content

Commit

Permalink
Killing the service gracefully
Browse files Browse the repository at this point in the history
  • Loading branch information
niranjan94 committed Sep 7, 2015
1 parent 002bd71 commit 7b4f92b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public class ProcessService extends Service {

public String decompilerToUse = "cfr";

public int startID;

public void onCreate() {
super.onCreate();
Expand All @@ -61,6 +62,7 @@ public void onCreate() {
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);

this.startID = startId;
/**
* Initialize a handler for posting runnables that have to run on the UI thread
*/
Expand Down Expand Up @@ -93,8 +95,12 @@ public int onStartCommand(Intent intent, int flags, int startId) {
*
* Uses the {@link #killSelf()} method.
*/
killSelf();

int toKillStartId = intent.getIntExtra("startId",-1);
killSelf(true, toKillStartId);

} else if(intent.getAction().equals(Constants.ACTION.STOP_PROCESS_FOR_NEW)) {
killSelf(false, -1);
}

return START_NOT_STICKY;
Expand Down Expand Up @@ -164,7 +170,7 @@ public void run() {
}
})).start();
} else {
killSelf();
killSelf(true, startID);
}
}

Expand Down Expand Up @@ -297,6 +303,7 @@ private Notification buildNotification() {

Intent stopIntent = new Intent(this, ProcessService.class);
stopIntent.setAction(Constants.ACTION.STOP_PROCESS);
stopIntent.putExtra("startID", startID);

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

Expand Down Expand Up @@ -326,7 +333,6 @@ private Notification buildNotification() {
public void onDestroy() {
super.onDestroy();
try {
kill();
processNotify.cancel();
} catch (Exception e) {
Ln.e(e);
Expand Down Expand Up @@ -358,16 +364,18 @@ public void run() {
}
}

private void killSelf(){
broadcastStatus("exit");
private void killSelf(boolean shouldBroadcast, int startID){
if(shouldBroadcast){
broadcastStatus("exit");
}
stopForeground(true);
stopSelf(startID);
try {
NotificationManager mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyManager.cancel(Constants.PROCESS_NOTIFICATION_ID);
Utils.killAllProcessorServices(this);
Utils.forceKillAllProcessorServices(this);
} catch (Exception e) {
Ln.e(e);
}
stopSelf();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public void run() {
}

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

case "exit":
Toast.makeText(baseContext, "Exiting.", Toast.LENGTH_SHORT).show();
finish();
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
break;
Expand Down
15 changes: 10 additions & 5 deletions app/src/main/java/com/njlabs/showjava/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,18 @@

public class Utils {

public static void killAllProcessorServices(Context context) {

public static void killAllProcessorServices(Context context, boolean forNew) {
Intent mServiceIntent = new Intent(context, ProcessService.class);
mServiceIntent.setAction(Constants.ACTION.STOP_PROCESS);
context.startService(mServiceIntent);
if(forNew){
mServiceIntent.setAction(Constants.ACTION.STOP_PROCESS_FOR_NEW);
} else {
mServiceIntent.setAction(Constants.ACTION.STOP_PROCESS);
}
context.stopService(mServiceIntent);
}

ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
public static void forceKillAllProcessorServices(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo next : runningAppProcesses) {
String processName = context.getPackageName() + ":service";
Expand Down

0 comments on commit 7b4f92b

Please sign in to comment.