Skip to content Skip to sidebar Skip to footer

How To Make Sure Service Is Not Killed

I am trying to write a music player in Android, and was trying to use service to play music in infinite loop in background. And I need the service to interact with the activity for

Solution 1:

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.

Solution 2:

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.

publicstaticbooleanisServiceRunning(String serviceClassName){
    finalActivityManageractivityManager= (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)){
            returntrue;
        }
    }
    returnfalse;
 }

For music player app you need to crerate bound service.check this docs docs

publicclassMusicPlayerServiceextendsService {
// Binder given to clientsprivate 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.
 */publicclassLocalBinderextendsBinder {
    MusicPlayerService getService() {
        // Return this instance of MusicPlayerService so clients can call public methodsreturn MusicPlayerService.this;
    }
}

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

/** method for clients */publicvoidplay() {

}
publicvoidpause() {

}
publicvoidstop() {

}

}

in your activity class, declare mBound boolean variable,

boolean mBound;

add bindServiceImplementation

@OverrideprotectedvoidonStart() {
    super.onStart();
    // Bind to MusicPlayerServiceIntent intent = newIntent(this, MusicPlayerService.class);
    bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
}
@OverrideprotectedvoidonStop() {
    super.onStop();
    // Unbind from the serviceif (mBound) {
        unbindService(mConnection);
        mBound = false;
    }
}

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

privateServiceConnectionmConnection=newServiceConnection() {

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

    @OverridepublicvoidonServiceDisconnected(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();

    }
}

Post a Comment for "How To Make Sure Service Is Not Killed"