问题
I'm developing an app for Android, and I want it to pause the music when you press the home button (onPause). That works fine, but then when I try to start up the game again, onResume, onRestart, onStart, onRestoreInstanceState and onCreate are never called and it tells me that the application is not responding. There are no Exceptions shown in the LogCat... So I have no idea what is happening. Does anyone have any suggestions as to why this might be the case?
-EDIT- I do get this error in the LogCat when the application pauses:
04-16 20:09:32.659: ERROR/ActivityManager(66): Reason: Broadcast of Intent { act=android.intent.action.CLOSE_SYSTEM_DIALOGS cmp=com.android.settings/.widget.SettingsAppWidgetProvider (has extras) }
my onPause() code:
public void onPause() {
super.onPause();
panel.mediaPlayer.pause();
panel.thread.running=false;
}
Main application thread:
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:
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();
回答1:
When you get an "application not responding" error, that means that your activity has taken too long on the main application thread. Identify all the work that you are doing on the main application thread and migrate the slow stuff to a background thread or AsyncTask
.
回答2:
I think I may know what my error is: It is running some unneeded code when onSizeChanged is called on the SurfaceView (which i guess might happen when you pause the Activity and something else comes in to view)... I'll check it out. Thanks everyone!
来源:https://stackoverflow.com/questions/5689316/android-onresume-not-called