这是我的服务,我希望它是粘性的。我试图使用以下方法终止服务:
context.stopService(new Intent(context, HauntedSoundService.class));`
然而,音频仍在播放。
public class HauntedSoundService extends Service {
Context context = this;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;// this make the service sticky which prevents it
// from getting killed by advanced task killers
}
@Override
public void onCreate() {
// volume full
AudioManager am = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
am.setStreamVolume(AudioManager.STREAM_MUSIC,
am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
// volume full
// play audio
final MediaPlayer mp2 = MediaPlayer.create((context), R.raw.cry); // -<
mp2.setLooping(false);
mp2.start();
// play audio
}
}