-1

i have a problem .. i used a MediaPlayer class to start a song and the problem is that when the app is closed the song playback also stops.

I search the net and found i can override on back pressed method to move task to background, it worked fine for me but , when i remove the app from recent apps (by swiping the recent apps or clearing the recent apps) the song playback stops.

So is there any way to prevent this.

My code is like this :

Main Activity :

    oncreate()
    {
        //
        public static MediaPlayer mp;
        startsong();
    }

    void startsong()
    {
        mp = MediaPlayer.create(....);
        mp.start();
        Intent intent = new Intent(this,Service.class);
        startService(inent);

    }
    onBackPressed()
    {
        moveTaskToBack(true);
    }

Service Class :

onStartCommand()
{
    Main.mp.setOnCompletitionListener( ... new oncomp ...)
    {
        Main.mp.stop();
        Main.mp.release();
    }

}

Edit : I found the answer , the answer is to stick the service with a notification in foreground

Ronit Jain
  • 45
  • 2
  • 9
  • I believe there different OS versions will have a slightly different behavior, but the latests ones (Lollipop for sure) that's the OS behavior and there's not that much that you can do about it. It's coded in the OS, "if the user dismisses the app, it kills the VM". You can test it with Google Play Music. I guess you have to just accept that is how the OS works, and the users will know not to close your activity from recent. – Budius Jul 29 '15 at 13:05
  • Is that the reason ? when i compiled my app with API 19 there was no problem, and now when compiling with API 21 this problem arises – Ronit Jain Jul 29 '15 at 13:08
  • But what if the app is not cleared from recent ? and killed by the system ? isn't there any way to prevent that ? there should be a way ? – Ronit Jain Jul 29 '15 at 13:10
  • I don't know exactly that this is the reason, I'm just telling you that if even Google Play Music works like, why are you trying to fight it? The system only kills a service that is in foreground, if the system is really in trouble for lack of memory; so again, trying to fight it or find hack around it doesn't sound the best thing to do. – Budius Jul 29 '15 at 13:12
  • Google Play music does not stop song playback after clearing recent aapps – Ronit Jain Jul 29 '15 at 14:27
  • as I said "I believe there different OS versions will have slightly different behavior" on my Nexus 5 it does because I tested before commenting. – Budius Jul 29 '15 at 14:28

3 Answers3

0

Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). from: Services

Your Service is probably bound to the Activity, and dies with it. Check the Creating a Background Service in the Android documentation for how to make it not so.

Sebastian
  • 1,076
  • 9
  • 24
  • No my service is not bound and not getting destroyed , i checked that by log cat – Ronit Jain Jul 29 '15 at 13:03
  • Then maybe you can check this other answer: [Android: keeping a background service alive (preventing process death)](http://stackoverflow.com/questions/3856767/android-keeping-a-background-service-alive-preventing-process-death) – Sebastian Jul 29 '15 at 13:12
0

Dont try to keep your activity alive once its closed, you wont succeed and this is a hack. What you want is to keep your service foreground which should prevent your application process from beeing killed. You should design your activity in such a way that MediaPlayer will not be stopped once Activity is closed and destroyed.

for more info look here: Using a Service with MediaPlayer

there is also a part on making service foreground: Running as a foreground service

[edit]

I might missunderstood your question, if the problem is that service is killed after clearing recent list then look at this SO: Android - Service stops when swiping app from recent apps, its solution is to use alarmmanager which will restart your service after task gets killed.

Community
  • 1
  • 1
marcinj
  • 48,511
  • 9
  • 79
  • 100
  • Service is not getting killed, rather the Mediaplayer goes null and thus the onCompleteListener won't do any thing – Ronit Jain Jul 29 '15 at 14:22
  • @RonitJain follow the first link I have provided in my answer, you should not keep MediaPlayer as a static field in your Activity class, move it to your service. Full example is in the link above. – marcinj Jul 29 '15 at 14:34
  • @RonitJain `rather the Mediaplayer goes null` - this is probably because your app process gets killed, then service gets restarted. static MediaPlayer is not initialized - because activity is not restarted, and this is why it is null. – marcinj Jul 29 '15 at 14:38
0

You can restart the service if the process is being killed, You have to override public void onTaskRemoved()

Example

@Override
public void onTaskRemoved(Intent rootIntent){
    Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
    restartServiceIntent.setPackage(getPackageName());

    PendingIntent restartServicePendingIntent = PendingIntent.getService(getApplicationContext(), 1, restartServiceIntent, PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(
    AlarmManager.ELAPSED_REALTIME,
    SystemClock.elapsedRealtime() + 1000,
    restartServicePendingIntent);

    super.onTaskRemoved(rootIntent);
 }

or you can return START_STICKY in onStartCommand() which will restart the serivice if it gets terminated.

@Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    return Service.START_STICKY;
  }

Source: The process of the service is killed after the application is removed from the application tray

Community
  • 1
  • 1
Jitin Jassal
  • 137
  • 2
  • 13
  • What it will do is it restart the service , this is not what i wanted as my service class is not getting killed, rather the mediaplayer object which is static member of the main class is flushed out on removing recent apps , hence the playback is stopped, i want to prevent this – Ronit Jain Jul 29 '15 at 14:31