解锁设备后ANR

时间:2015-02-26 06:12:20

标签: android onresume onpause unlock wakeup

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

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

这意味着解锁设备时没有恢复。

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

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

1 个答案:

答案 0 :(得分:0)

也许您需要广播接收器才能开启或关闭屏幕。

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;
        }
    }

}

在你的活动中,它会像这样

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

}