Android的onResume不叫 [英] Android onResume not called

查看:211
本文介绍了Android的onResume不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Android开发的应用程序,我希望它暂停音乐,当你preSS的home键(的onPause)。这工作得很好,但后来当我尝试再次启动游戏,onResume,onRestart,ONSTART,onRestoreInstanceState和的onCreate都不会被调用,它告诉我,该应用程序没有响应。有在LogCat中没有表现出异常。所以我不知道发生了什么。没有人有任何建议,为什么这可能是这样?

-EDIT- 我得到这个错误在LogCat中,当应用程序暂停:

04-16 20:09:32.659:ERROR / ActivityManager(66):原因:意向广播{行动= android.intent.action.CLOSE_SYSTEM_DIALOGS CMP = com.android.settings / .widget.SettingsAppWidgetProvider(有额外) }

我的onPause()code:

 公共无效的onPause(){
    super.onPause();
    panel.mediaPlayer.pause();
    panel.thread.running = FALSE;
}
 

主应用程序线程:

 公共无效的run(){
        运行=真;
        而(运行){
            //新的画布。
            帆布C = NULL;
            //更新信息
            更新();
            //绘制的一切画面
            尝试 {
                //获取来自surfaceHolder画布。
                C = surfaceHolder.lockCanvas(空);
                同步(surfaceHolder){
                    //画到画布
                    doDraw(C);
                    尝试 {
                        视频下载(gameSpeed​​);
                    }赶上(InterruptedException异常E){
                        e.printStackTrace();
                    }

                }
            } 最后 {
                如果(C!= NULL){
                    surfaceHolder.unlockCanvasAndPost(C);
                }
            }
        }
 

MediaPlayer的:

 媒体播放器=新的MediaPlayer();
    崇高= context.getResources()openRawResourceFd(R.raw.sublime)。

    尝试 {
        mediaPlayer.setLooping(真正的);
        mediaPlayer.setDataSource(sublime.getFileDescriptor(),sublime.getStartOffset(),sublime.getLength());
        媒体播放器prepare()。

    }赶上(抛出:IllegalArgumentException E1){

    }赶上(IllegalStateException异常E1){

    }赶上(IOException异常E1){

    }
    mediaPlayer.start();
 

解决方案

当你得到一个应用程序没有响应的错误,这意味着你的活动时间过长主应用程序线程上。找出所有你正在做的主应用程序线程的工作和缓慢的东西迁移到后台线程或的AsyncTask

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();

解决方案

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.

这篇关于Android的onResume不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆