0

I have read this SO question but it didnt help me...Android Background Service is restarting when application is killed. but answer mentioned here is not helping me at all. Please consider this

I want to create a service for background music and want to play it even if my app is closed. so i write following code...

package elsner.com.mediaplayer;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.util.Log;

/**
 * Created by nteam on 6/2/16.
 */
public class BackGround extends Service {

    MediaPlayer mPlayer;


    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }


    @Override
    public void onCreate() {
        super.onCreate();

        Log.i("niral", "Service Create");


    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.i("niral", "Service Start");
        final Handler h = new Handler();
        final long delayInMilliseconds = (6000*10);

        new Thread(new Runnable() {

            @Override
            public void run() {
                mPlayer = MediaPlayer.create(getApplicationContext(), R.raw.aayat);
                mPlayer.start();

                h.postDelayed(new Runnable() {
                    public void run() {

                        mPlayer.stop();
                    }
                }, delayInMilliseconds);
            }
        }).start();



        return START_NOT_STICKY;
    }


}

All works fine until i kill app from background. As soon as i kill app from background my service is restarted and music file will play from top.

I kill the application by swiping away the app in the Recent Apps.

Please help me.

Community
  • 1
  • 1
H Raval
  • 1,903
  • 17
  • 39
  • If you kill an app it will get killed, you can try this with default music player, it will also stop if you kill it – Vivek Mishra Feb 06 '16 at 11:38
  • Did You tried to return START_STICKY in service's onStartCommand method? – udenfox Feb 06 '16 at 11:40
  • @VivekMishra no i have tried it with music app...its steel playing – H Raval Feb 06 '16 at 11:45
  • @udenfox yes i have tried that but no luck – H Raval Feb 06 '16 at 11:45
  • thanx @pskink but i wonder how the music player apps works then? i have tried to kill that app from background and steel music is playing – H Raval Feb 06 '16 at 12:37
  • ohk...i have tested with samsung galaxy duos...with its default music player and its not stopping – H Raval Feb 06 '16 at 12:43
  • can anyone suggest any way? – H Raval Feb 06 '16 at 12:43
  • 1
    the only way you can do is to use `startForeground` but still, it doesn't make your service "immortal" – pskink Feb 06 '16 at 13:07
  • Possible duplicate of [Android Background Service is restarting when application is killed](http://stackoverflow.com/questions/15452935/android-background-service-is-restarting-when-application-is-killed) – Martin Tournoij Aug 26 '16 at 20:34
  • @Carpetsmoker i have already mention in my question that i have referred the link but the answer given there not helped me at all....please first read question properly – H Raval Aug 27 '16 at 06:46
  • You marked an answer that is 100% duplicate of an answer on that question as accepted, so obviously it **is** a duplicate. – Martin Tournoij Aug 27 '16 at 10:51

1 Answers1

1

This is a re-post from my answer here: https://stackoverflow.com/a/39169603/6761960. I'll re-post for convenience.

The app and the service live on the same process, which means when the app is killed so is your service. Changing the return value of onStartCommand doesn't affect this process. It simply tells the Service to either start/stop when you tell it or when it's finished doing what it needs to.

To change the Service so that it's killed separately and assuming it's a started service rather than a bound service due to the use of onStartCommand, specify a process name in the manifest for that Service.

From the Process and Threads Developer Guide:

The manifest entry for each type of component element— <activity>, <service>, <receiver>, and <provider>— supports an android:process attribute that can specify a process in which that component should run. You can set this attribute so that each component runs in its own process or so that some components share a process while others do not. You can also set android:process so that components of different applications run in the same process—provided that the applications share the same Linux user ID and are signed with the same certificates.

Android might decide to shut down a process at some point, when memory is low and required by other processes that are more immediately serving the user. Application components running in the process that's killed are consequently destroyed. A process is started again for those components when there's again work for them to do.

From <service> in Manifest File:

android:process

The name of the process where the service is to run. Normally, all components of an application run in the default process created for the application. It has the same name as the application package. The element's process attribute can set a different default for all components. But component can override the default with its own process attribute, allowing you to spread your application across multiple processes.

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process. If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.

Using this method, your Service and your app will have two different process resulting in them being killed for separate reasons. However, this doesn't guarantee it still won't be killed. You should design, expect, and be willing to have your Service be killed.

Also, due to the fact your app is a music app and the use of your Service is something that the user is actively engaged in, creating the Service is an acceptable reason to give it foreground priority using startForeground. But, again, this doesn't mean it won't ever be killed.

Community
  • 1
  • 1
Plumbus
  • 397
  • 3
  • 7