我正在为 Android 开发一个应用程序,我希望它在您按下主页按钮 (onPause) 时暂停音乐。这很好,但是当我尝试再次启动游戏时,onResume、onRestart、onStart、onRestoreInstanceState 和 onCreate 永远不会被调用,它告诉我应用程序没有响应。LogCat 中没有显示异常......所以我不知道发生了什么。有人对为什么会出现这种情况有任何建议吗?
-EDIT- 当应用程序暂停时,我确实在 LogCat 中收到此错误:
04-16 20:09:32.659: 错误/ActivityManager(66): 原因: 意图广播 { act=android.intent.action.CLOSE_SYSTEM_DIALOGS cmp=com.android.settings/.widget.SettingsAppWidgetProvider (有附加功能)}
我的 onPause() 代码:
public void onPause() {
super.onPause();
panel.mediaPlayer.pause();
panel.thread.running=false;
}
主应用线程:
public void run() {
running = true;
while(running) {
//new Canvas.
Canvas c = null;
//Update information
update();
//Draw everything to screen
try {
//Gets the canvas from the surfaceHolder.
c = surfaceHolder.lockCanvas(null);
synchronized(surfaceHolder) {
//draw to the canvas
doDraw(c);
try {
Thread.sleep(gameSpeed);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} finally {
if (c != null) {
surfaceHolder.unlockCanvasAndPost(c);
}
}
}
媒体播放器:
mediaPlayer = new MediaPlayer();
sublime = context.getResources().openRawResourceFd(R.raw.sublime);
try {
mediaPlayer.setLooping(true);
mediaPlayer.setDataSource(sublime.getFileDescriptor(), sublime.getStartOffset(), sublime.getLength());
mediaPlayer.prepare();
} catch (IllegalArgumentException e1) {
} catch (IllegalStateException e1) {
} catch (IOException e1) {
}
mediaPlayer.start();