I have big problems with stopping a service, which plays audio file. I want to stop the current service before I play another file.
Activity:
public void playTrack(View view){
Intent i=new Intent(this,playService.class);
i.setAction("com.c0dehunterstudios.relaxer.PLAY");
if(isPlaying){
stopService(i);
isPlaying=false;
Log.v("ACTIVITY", "Stopping..");
}
startService(i);
isPlaying=true;
}
Service:
@Override
public void OnDestroy(){
Log.v("SERVICE","Service killed");
player.stop();
super.onDestroy();
}
But sadly it doesn't work - actually it doesn't even come down to the "Service killed" inside OnDestroy().
What am I doing wrong?
Android Activity onDestroy() is not always called and if called only part of the code is executed. Bookmark this question. Show activity on this post. onDestroy() is not always called.
This example demonstrates about How do I call OnDestroy Activity in Android app. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.
Third, finish() does not call onDestroy() . You can tell that by reading the source code.
If onDestroy() is called as the result of a configuration change, the system immediately creates a new activity instance and then calls onCreate( ) on that new instance in the new configuration.
First, it's onDestroy, not OnDestroy . Second, you must use the @Override annotation for compile-time checking, so your Service code should look somewhat like this:
@Override
public void onDestroy(){
Log.v("SERVICE","Service killed");
player.stop();
super.onDestroy();
}
First, you need to clarify how many types of services in Android. AFAIK, there are:
These services stop in different ways.
stopService()
in activity or fragment where you start that service or call stopSelf()
in its own service. And Please note only these methods trigger service's onDestroy()
.Background: stopService()
and stopSelf()
do in the same way as foreground. Moreover, you should know this service is in the same thread with activity or fragment calling it, so if you destroy activity or fragment, this service will stop also, but without calling onDestroy()
. It basically destroys underground. Thus, if you want to catch when this service stops you need to add android:stopWithTask="false"
when declaring service in Manifest file like this:
<application>
//....
<service android:name=".services.catchingStopService"
android:stopWithTask="false" />
//....
</application>
And now in your service class, you can override this method which triggers whenever this service stops:
@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);
// Your job when the service stops.
}
Bound: you need to handle by your self by bindService()
and unbindService()
, you will find out it's totally the same as starService()
and stopService()
ResultReceiver
to communicate between service and activity or fragment. It's similar to BroadcastReceiver
. You can search for example on Google easily.Summary: base on your purpose, pick the type of service satisfying you. And they all agree with:
onDestroy()
to know when service stops because it
sometimes gets called underground; more accurately, onDestroy calls
when stopService() or stopSelf() calls (BoundService works in the
same way, I think, but by a similar method, maybe unBind(), I did not
test yet).onStartCommand()
to
be able to deal with when services stop like restart service again or
something. Here are some keyword you can search:
START_STICKY
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.ACTION_SHUTDOWN" />
<actionandroid:name="android.intent.action.QUICKBOOT_POWEROFF" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With