为什么我不能停止服务?

时间:2016-07-03 01:05:06

标签: android

这是我的服务,我希望它是粘性的。我试图使用以下方法终止服务:

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

0 个答案:

没有答案