【问题标题】:ANR after unlock the device解锁设备后的 ANR
【发布时间】:2015-02-26 06:12:20
【问题描述】:

我正在开发一个使用大量 RAM 的指纹传感器应用程序,问题可能与内存有关,我尝试了很多仍未解决。

问:-当我按下主页按钮应用程序暂停并从最近按钮恢复成功,但是当我按下电源按钮然后解锁我的设备时,它显示 ANR 而不是我的应用程序屏幕。

表示解锁设备时没有恢复。

如果你明白我的意思,请提出建议或给出解决方案。

注意: 使用的设备专用于我的应用程序,不会安装或运行其他应用程序。

【问题讨论】:

    标签: android onresume onpause unlock wakeup


    【解决方案1】:

    也许您需要广播接收器来打开或关闭屏幕。

    public class ScreenReceiver extends BroadcastReceiver {
    
        // thanks Jason
        public static boolean wasScreenOn = true;
    
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
                // do whatever you need to do here
                wasScreenOn = false;
            } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
                // and do whatever you need to do here
                wasScreenOn = true;
            }
        }
    
    }
    

    而在你的Activity中,会是这样的

    public class ExampleActivity extends Activity {
    
       @Override
        protected void onCreate() {
            // initialize receiver
            IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
            filter.addAction(Intent.ACTION_SCREEN_OFF);
            BroadcastReceiver mReceiver = new ScreenReceiver();
            registerReceiver(mReceiver, filter);
            // your code
        }
    
        @Override
        protected void onPause() {
            // when the screen is about to turn off
            if (ScreenReceiver.wasScreenOn) {
                // this is the case when onPause() is called by the system due to a screen state change
                System.out.println("SCREEN TURNED OFF");
            } else {
                // this is when onPause() is called when the screen state has not changed
            }
            super.onPause();
        }
    
        @Override
        protected void onResume() {
            // only when screen turns on
            if (!ScreenReceiver.wasScreenOn) {
                // this is when onResume() is called due to a screen state change
                System.out.println("SCREEN TURNED ON");
            } else {
                // this is when onResume() is called when the screen state has not changed
            }
            super.onResume();
        }
    
    }
    

    【讨论】:

    • 这个解决方案不起作用,我试过但仍然是同样的问题 ANR
    • “当“主”线程中发生一些长时间的操作时会发生 ANR。这是事件循环线程,如果它很忙,Android 将无法处理任何进一步的 GUI 事件应用程序,从而引发一个 ANR 对话框。” 然后显示您的一些代码
    猜你喜欢
    • 2012-11-23
    • 1970-01-01
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 2015-06-03
    • 2020-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多