Search code examples
androidservice

Android: stopService doesn't call onDestroy


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?


Solution

  • 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();  
    }