如何在使用音乐服务时在后台停止音乐

时间:2014-12-06 12:40:31

标签: java android service intentservice

当我播放应用程序然后关闭后,有时应用程序音乐再次在后台启动 任何帮助将不胜感激 感谢

我有一个包含Code的活动类     @override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

    setContentView(R.layout.mygame);


    final Handler mHandler = new Handler();

    Intent svc=new Intent(this, MusicService.class);
    startService(svc);
    // Create runnable for posting

    final Runnable mUpdateResults = new Runnable() {

        public void run() {

            AnimateandSlideShow();
         }
    };

    int delay = 500; // delay for 1 sec.

    int period = 6000; // repeat every 4 sec.

    Timer timer = new Timer();

    timer.scheduleAtFixedRate(new TimerTask() {

    public void run() {

         mHandler.post(mUpdateResults);}

    }, delay, period);}

   public void onClick(View v) {

    finish();
    android.os.Process.killProcess(android.os.Process.myPid());

      Intent srv=new Intent(SliderActivity.this, MusicService.class);     
    stopService(srv); }

1 个答案:

答案 0 :(得分:1)

尝试在活动类的onStop方法中删除处理程序的回调:

@Override
protected void onStop() {
    super.onStop();
    mHandler.removeCallbacksAndMessages(null);
    timer = null;
    mHandler= null;
}

希望它有所帮助。