AとBの2つのアクティビティがあります。サービスはBで次のコードで始まります:
startService(new Intent(this, PlayerService.class));
Intent connectionIntent = new Intent(this, PlayerService.class);
bindService(connectionIntent, mp3PlayerServiceConnection, Context.BIND_AUTO_CREATE);
private ServiceConnection mp3PlayerServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName arg0, IBinder binder) {
mp3Service = ((LocalBinder) binder).getService();
Thread t = new Thread() {
public void run() {
mp3Service.playSong(getApplicationContext(),url);
}
};
t.start();
}
@Override
public void onServiceDisconnected(ComponentName arg0) {
}
};
アクティビティ A を行っているときにサービスを終了できるようにする必要があります (B は終了していますが、音楽は再生されます)。onDestroy() から StopService を呼び出す方法は? はいの場合、どのように、どこでバインドを解除する必要がありますか?
単純に置くstopService(new Intent(this,PlayerService.class));
とエラーが発生します: アクティビティ B がサービス接続を漏えいしました ... 元々ここにバインドされていました。(ただし、Bはすでに閉鎖されています)