how to make sure service is not killed

妖精的绣舞 提交于 2019-12-02 05:23:12

Per this process priority blog post, the only thing a higher priority than a foreground service is the foreground activity. This means that a foreground service will almost never be killed.

Per the building an audio app service documentation:

When a 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 low on memory. A foreground service must display a notification so the user knows about it and can optionally control it. The onPlay() callback should put the service in the foreground.

NOTE: Service will be run lifelong until you kill the service using stopSelf() or stopService() methods

Check your service is running or not using below method.

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 music player app you need to crerate bound 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 mBound boolean variable,

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 make communicate with 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 MusicPlayerService public methods 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();

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!