Android:stopService不会调用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.

活动:

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;
}

服务:

@Override
public void OnDestroy(){
    Log.v("SERVICE","Service killed");
    player.stop();
    super.onDestroy();  
}

但是遗憾的是,它不起作用-实际上,它甚至不能归结为服务被杀死".在OnDestroy()内部.

But sadly it doesn't work - actually it doesn't even come down to the "Service killed" inside OnDestroy().

我在做什么错了?

首先,它是onDestroy,而不是OnDestroy.其次,必须使用@Override批注进行编译时检查,因此您的Service代码应如下所示:

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