0

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.

  1. 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);
      }
  }
}
  1. Press home button to leave the Test app.
  2. Press square button to show all running task and swipe off the Test app. The MediaService is still running perfectly fine as expected.
  3. 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);
}
  1. 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" />
Jona
  • 13,325
  • 15
  • 86
  • 129
  • That is exactly what I would expect to happen. http://stackoverflow.com/a/22895083/1953590 – Kevin Krumwiede Mar 23 '15 at 22:11
  • @KevinKrumwiede: The `doBindService()` above (despite its name), calls both `startService()` and `bindService()`. – CommonsWare Mar 23 '15 at 22:14
  • 1
    You might want to check out [this issue](https://code.google.com/p/android/issues/detail?id=104308) and [this issue](https://code.google.com/p/android/issues/detail?id=63618), which are for seemingly related phenomena. Though ignore the "force stop" references in the latter link, as the person filing the issue was somewhat confused about the definition of that term. – CommonsWare Mar 23 '15 at 22:18
  • Hey @CommonsWare thanks for commenting. So two points, one the service getting killed is running on a separate process than the MainActivity. I checked the two issues you provided but I think they are slightly different to this issue. The issue seen here is happening after the app has been removed from the task list and re-added to the task list by tapping on a notification. Tapping on the launcher icon doesn't reproduce this issue. Somehow android is loosing the state or priority of the service thus killing it. – Jona Mar 23 '15 at 23:30
  • 1
    "I checked the two issues you provided but I think they are slightly different to this issue" -- both, if I understand correctly, involve services being stopped unexpectedly after having previously terminated the task that started the service. That's why I said "seemingly related phenomena", as your problem is from a service being stopped unexpectedly after having previously terminated the task that started the service. I figured that perhaps you could glean some ideas from the issues. – CommonsWare Mar 23 '15 at 23:34
  • @CommonsWare I checked the issues again and they do seem related just a very different way to reproduce the issue. I think my way seems a bit simpler. I noticed that the issue is not reproduce-able if I skip the bind/unbind calls on the activity. Somehow the binding is changing the service state. – Jona Mar 24 '15 at 16:53
  • Have you looked at this question?http://stackoverflow.com/questions/20636330/start-sticky-does-not-work-on-android-kitkat-edit-and-jelly-bean – Muzikant Mar 24 '15 at 17:31

0 Answers0