I have a background service called MyService in a remote (specific application) process called MyApp:MyOtherProcess . What happens with MyApp:MyOtherProcess and MyService when I uninstall the application?
Apps user interface => Running Apps tells me that I have 0 processes and 1 service. I interpret this as MyApp:MyOtherProcess not active, but will wake up when MyService wakes MyService .
MyService Thinking is still running, and I tried to stop the service.
Stop MyService:
Intent i = new Intent(context, MyService.class); if (isMyOtherProcessRunning(context)) { //Test case: I have swiped away the application //Ahh, nothing is logged so I think MyService.onDestory is not invoked! //Is MyService still running??? context.stopService(context, i); } else { //Test case: I have not swiped away the application //Good, "destroying" is logged so MyService.onDestroy was invoked! context.stopService(context, i); }
MyService:
public MyService : Service { @Override public void onCreate() { Log.d("MyService", "creating"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("MyService", "starting"); return START_REDELIVER_INTENT; } @Override public void onDestroy() { Log.d("MyService", "destroying"); } }
Helper:
public boolean isMyOtherProcessRunning(Context applicationContext) { ActivityManager activityManager = (ActivityManager)applicationContext.getSystemService(Context.ACTIVITY_SERVICE); List<RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses(); for(int i = 0; i < procInfos.size(); i++) { String processName = procInfos.get(i).processName; if(processName.equals("MyApp:MyOtherProcess")) { return true; } } return false; }
AndroidManifest.xml:
<service android:enabled="true" android:name="com.myapp.MyService" android:process=":MyOtherProcess" />
To get around this, I start MyService and immediately stop MyService if the process was killed. This seems like bad practice. Restarting the service just to kill it with bad practice? I feel like I donβt understand how MyService supported when the contained process is βkilledβ ... it is a process, even killed in the first place ... can the process just become inactive ... so lost!
Current workaround
Stop MyService:
Intent i = new Intent(context, MyService.class); if (isMyOtherProcessRunning(context)) { //Test case: I have swiped away the application //Ahh, nothing is logged so I think MyService.onDestory is not invoked! //Is MyService still running??? i.putExtra("stopImmediately", true); context.stopService(context, i); } else { //Test case: I have not swiped away the application //Good, "destroying" is logged so MyService.onDestroy was invoked! context.stopService(context, i); }
MyService:
public MyService : Service { @Override public void onCreate() { Log.d("MyService", "creating"); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("MyService", "starting"); if (intent.getBooleanExtra("stopImmediately", false)) { this.stopSelf(); return START_NOT_STICKY; } return START_REDELIVER_INTENT; } @Override public void onDestroy() { Log.d("MyService", "destroying"); } }
I am running Android 4.4.4 API 19 on a Samsung Galaxy S5 with Genymotion version 2.3.1.