How to make sure the service is not killed

I am trying to write a music player on Android and am trying to use a service to play music in an endless loop in the background. And I need a service to interact with actions to control the volume and so on. The question is, if I am using a service binder, then the service will most likely be terminated with activity when the activity is in the background. How can I make sure that the service can continue to run without activity or activity, some of the service methods can still be called?

Are you using foreground with binder? This service won't be killed this way?

Thanks in advance!

+1


source to share


2 answers


Per this blog priority blog , the only priority over front service is front end activity. This means that the foreground service will almost never be killed.

To create documentation for audio applications :



When the service is playing, it should be running in the foreground. This lets the system know that the service is performing a useful function and should not be killed if the system is inactive in memory. The foreground service must display a notification so that the user is aware of it and can optionally control it. The onPlay () callback should bring the service to the fore.

+2


source


NOTE. The service will run for its entire life until you kill the service using the stopSelf () or stopService () methods

Check if your service is running or not using the method below.

public static boolean isServiceRunning(String serviceClassName){
    final ActivityManager activityManager = (ActivityManager)Application.getContext().getSystemService(Context.ACTIVITY_SERVICE);
    final List<RunningServiceInfo> services = activityManager.getRunningServices(Integer.MAX_VALUE);

    for (RunningServiceInfo runningServiceInfo : services) {
        if (runningServiceInfo.service.getClassName().equals(serviceClassName)){
            return true;
        }
    }
    return false;
 }

      

For a music player app, you need to overthrow the associated service.check this docs docs

public class MusicPlayerService extends Service {
// Binder given to clients
private final IBinder mBinder = new LocalBinder();


/**
 * Class used for the client Binder.  Because we know this service always
 * runs in the same process as its clients, we don't need to deal with IPC.
 */
public class LocalBinder extends Binder {
    MusicPlayerService getService() {
        // Return this instance of MusicPlayerService so clients can call public methods
        return MusicPlayerService.this;
    }
}

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

/** method for clients */
public void play() {

}
public void pause() {

}
public void stop() {

}

}

      

in your activity class, declare a boolean variable mBound,

boolean mBound;

      



add bindServiceImplementation

@Override
protected void onStart() {
    super.onStart();
    // Bind to MusicPlayerService
    Intent intent = new Intent(this, MusicPlayerService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
    super.onStop();
    // Unbind from the service
    if (mBound) {
        unbindService(mConnection);
        mBound = false;
    }
}

      

create To communicate with the MusicPlayerService class, initialize the ServiceConnection object,

  private ServiceConnection mConnection = new ServiceConnection() {

    @Override
    public void onServiceConnected(ComponentName className,
            IBinder service) {
        // We've bound to MusicPlayerService, cast the IBinder and get MusicPlayerService instance
        LocalBinder binder = (LocalBinder) service;
        mService = binder.getService();
        mBound = true;
    }

    @Override
    public void onServiceDisconnected(ComponentName arg0) {
        mBound = false;
    }
};

      

then you can call the public methods of the MusicPlayerService like below,

  public void onButtonClick(View v) {
    if (mBound) {

        // However, if this call were something that might hang, then this request should
        // occur in a separate thread to avoid slowing down the activity performance.
       mService.play();
       mService.pause();
       mService.stop();

    }
}

      

0


source







All Articles