当我播放应用程序然后关闭时,有时应用程序音乐会再次在后台启动 任何帮助将不胜感激 谢谢
我有一个包含代码的 Activity 类(class) @覆盖
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); }
最佳答案
尝试在 Activity 类的 onStop 方法中删除处理程序的回调:
@Override
protected void onStop() {
super.onStop();
mHandler.removeCallbacksAndMessages(null);
timer = null;
mHandler= null;
}
希望有帮助。
关于java - 如何在使用音乐服务时停止后台音乐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27331783/