Android - 检测手机解锁事件,而不是屏幕开启
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3446202/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android - detect phone unlock event, not screen on
提问by Chris
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_ON
and ACTION_SCREEN_OFF
,但是当按下电源按钮时屏幕打开/关闭时,这些似乎会被触发,但实际上当按下菜单按钮时手机被解锁时不会被触发......
I am trying to detect the unlock/lock while an activity is running, and I want to resume the activity once unlocked.
我试图在活动运行时检测解锁/锁定,并且我想在解锁后恢复活动。
回答by Chris
Here's what to do:
以下是该怎么做:
Say you want to detect the unlock event and do something in your activity when the phone is unlocked. 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. Create a handler and wait for ACTION_USER_PRESENT. When it is fired, implement what you want for your activity.
当 ACTION_SCREEN_ON 被触发时,将调用活动的 onResume。创建处理程序并等待 ACTION_USER_PRESENT。当它被触发时,为你的活动实现你想要的。
Credit goes to CommonsWare's answer here: Android -- What happens when device is unlocked?
归功于此处 CommonsWare 的回答:Android -- 当设备解锁时会发生什么?
回答by Doron Manor
After straggling 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 (e.g when the keyguard is gone)."
“广播动作:当设备唤醒后用户在场时发送(例如,当键盘锁消失时)。”
To distinguish between cases where the user has turned on his screen when it wasn't locked to when he 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
}
}
}
回答by Karan Nagpal
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);
回答by adamk
Didn't test it but try the following:
没有测试它,但尝试以下方法:
- Wait for
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.
- 等
ACTION_SCREEN_ON
。 - (屏幕打开后,)等待类别为 CATEGORY_HOME 的 ACTION_MAIN(启动主屏幕) - 这可能是手机解锁后发送的内容。
The 1st step is needed to filter out regular HOME key presses.
需要第一步来过滤掉常规的 HOME 按键。