So this is not your typical issue with services being tied to an activity's life cycle. This particular bug seems to be related to Android 5. I'm particularly working with a Nexus 5 running 5.0.1.
What is happening is that after a set of events and then pressing home causes Android to kill my MediaService.
Let me explain how to reproduce this bug along with some code from my test app. There are two classes involved MainActivity and MediaService.
- Open the Test app via launcher icon. The MainActivity is started and on
onStart()
we bind to the MediaService as follows.
private void doBindService () { // no need to bind if already bound if (!mIsServiceBound) { Log.v(TAG, "doBindService()"); Intent intent = new Intent(this, MediaService.class); //intent.setComponent(mServiceName); // start Service startService(intent); // connect to service if (14 > android.os.Build.VERSION.SDK_INT) { mIsServiceBound = bindService(intent, MainActivity.this, Context.BIND_AUTO_CREATE); } else { // Context.BIND_IMPORTANT = 0x40 mIsServiceBound = bindService(intent, MainActivity.this, Context.BIND_AUTO_CREATE | 0x40); } } }
- Press home button to leave the Test app.
- Press square button to show all running task and swipe off the Test app. The MediaService is still running perfectly fine as expected.
- Press the notification belonging to the Test app foreground MediaService to jump back into the Test app. This is the intent used for the notification to take us to the Test app.
public PendingIntent getPendingIntent (Context context) { Intent showPlayer = new Intent(context, MainActivity.class); showPlayer.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); return PendingIntent.getActivity( context, 0, showPlayer, PendingIntent.FLAG_CANCEL_CURRENT); }
- Press home button to close the Test app. At
onStop()
we call unbindService(). Immediately after this the MediaService is killed!?? Here is what I see on the logs.
I/ActivityManager(790): Killing 12049:com.test.MediaService:RemoteMediaService/u0a91 (adj 0): remove task
The service is running on a different process.
<service android:name=".MediaService" android:process=":RemoteMediaService" />