简体   繁体   English

Android - 检测手机解锁事件,而不是屏幕开启

[英]Android - detect phone unlock event, not screen on

Is there a way to detect when a user unlocks the phone?有没有办法检测用户何时解锁手机? I know about ACTION_SCREEN_ON and ACTION_SCREEN_OFF , but these seem to be fired when the screen switches on/off when pressing the power button, but not actually when the phone gets unlocked when pressing the Menu button...我知道ACTION_SCREEN_ONACTION_SCREEN_OFF ,但这些似乎是在按下电源按钮时屏幕打开/关闭时触发的,但实际上不是在按下菜单按钮时手机解锁时......

I am trying to detect the unlock/lock while an activity is running, and I want to resume the activity once unlocked.我正在尝试在活动运行时检测解锁/锁定,并且我想在解锁后恢复活动。

Here's what to do:以下是该怎么做:

Say you want to detect the unlock event and do something in your activity when the phone is unlocked.假设您想检测解锁事件并在手机解锁时在您的 Activity 中做一些事情。 Have a Broadcast Receiver for ACTION_SCREEN_ON, ACTION_SCREEN_OFF and ACTION_USER_PRESENT.有一个用于 ACTION_SCREEN_ON、ACTION_SCREEN_OFF 和 ACTION_USER_PRESENT 的广播接收器。

onResume of the activity will be called when ACTION_SCREEN_ON is fired.当触发 ACTION_SCREEN_ON 时,将调用活动的 onResume。 Create a handler and wait for ACTION_USER_PRESENT.创建一个处理程序并等待 ACTION_USER_PRESENT。 When it is fired, implement what you want for your activity.当它被触发时,实现你想要的活动。

Credit goes to CommonsWare's answer here: Android -- What happens when device is unlocked?归功于 CommonsWare 的回答:Android—— 设备解锁时会发生什么?

After struggling with this for a while, I've found that the best way to do this is to register a BroadcastReceiver on the "android.intent.action.USER_PRESENT" action.在为此苦苦挣扎了一段时间之后,我发现最好的方法是在“android.intent.action.USER_PRESENT”操作上注册一个 BroadcastReceiver。

"Broadcast Action: Sent when the user is present after device wakes up (eg when the key-guard is gone)." “广播动作:当设备唤醒后用户在场时发送(例如,当键盘保护器消失时)。”

To distinguish between cases where the user has turned on phone screen when it wasn't locked, and when they actually unlocked it use the KeyguardManager to check the security settings.要区分用户在未锁定时打开手机屏幕的情况,以及他们实际解锁手机屏幕的情况,请使用 KeyguardManager 检查安全设置。

Code example:代码示例:

Add this to your activity:将此添加到您的活动中:

registerReceiver(new PhoneUnlockedReceiver(), new IntentFilter("android.intent.action.USER_PRESENT"));

Then use this class:然后使用这个类:

public class PhoneUnlockedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        KeyguardManager keyguardManager = (KeyguardManager)context.getSystemService(Context.KEYGUARD_SERVICE);
        if (keyguardManager.isKeyguardSecure()) {
            //phone was unlocked, do stuff here
        }
    }
}       
public class PhoneUnlockedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_USER_PRESENT)){
            Log.d(TAG, "Phone unlocked");
        }else if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)){
            Log.d(TAG, "Phone locked");
        }
    }
}

register receiver by this statement通过这条语句注册接收者

receiver = new PhoneUnlockedReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(Intent.ACTION_USER_PRESENT);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(receiver, filter);

Didn't test it but try the following:没有测试它,但尝试以下操作:

  • Wait for ACTION_SCREEN_ON .等待ACTION_SCREEN_ON
  • (After screen is on,) Wait for ACTION_MAIN with category CATEGORY_HOME (Which launches the home screen) - This is probably what is sent after the phone gets unlocked. (屏幕打开后,)等待类别为 CATEGORY_HOME 的 ACTION_MAIN(启动主屏幕)- 这可能是手机解锁后发送的内容。

The 1st step is needed to filter out regular HOME key presses.第一步需要过滤掉常规的 HOME 按键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM