Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call onDestroy() of Service

I want to call

onDestroy()

method of Service in android.

I already searched a lot on internet and many answers are like if

service force stop or somehow its onDestroy() will never call.

But I really need to know when service is stop.

My project is about music player.

So it uses service and there is an ongoing notification. I need to find out when the service stop? and need to stop the music and remove the notification.

But it never shows any log of onDestroy(). Can anyone help me what is the alternative for it? if not onDestroy() then which method and how?

Edit:

I don't want to call onDestroy() explicitly. I want to remove notification when I remove my app from the device menu of running applications. Because when I stop my application, onDestroy() don't call and my notification remains in the status bar.

I have started my service with this code.

Intent playin = new Intent(this, MusicService.class);
startService(playin);
like image 312
djk Avatar asked Jul 18 '14 12:07

djk


3 Answers

From within the Service class, call:

stopSelf();

From within another class, like your MainActivity for example:

Intent i = new Intent(this, ServiceName.class); stopService(i);

Both of these will stop your service. Make sure you are returning START_NOT_STICKY so that the service doesn't start back up again.

like image 123
jaytj95 Avatar answered Oct 13 '22 01:10

jaytj95


I want to call onDestroy() method of Service in android.

  • Do not call this method directly

public void onDestroy ()

Called by the system to notify a Service that it is no longer used and is being removed. The service should clean up any resources it holds (threads, registered receivers, etc) at this point. Upon return, there will be no more calls in to this Service object and it is effectively dead. Do not call this method directly.

However you can check if the service is running or not.

I need to find out when service stop? and need to stop music and remove notification.

Use the following way -

private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

Then call it using - isMyServiceRunning(MyService.class).

Reference:

1) Service onDestroy().

2) how-to-check-if-a-service-is-running-in-android.

like image 23
My God Avatar answered Oct 13 '22 01:10

My God


When you want to stop your service then simply fire an intent to stop the service as shown below.

Intent intent = new Intent();
intent.setClass(getApplicationContext(), YourService.class);
stopService(intent);

This is to stop service forcefully.When you stop service in this manner it's guaranteed that onDestroy method is called by android framework.

Hope this helps to solve you issue.

like image 23
Ashwin N Bhanushali Avatar answered Oct 13 '22 01:10

Ashwin N Bhanushali