gpt4 book ai didi

Android:如何知道设备何时锁定和解锁

转载 作者:太空狗 更新时间:2023-10-29 14:48:33 28 4
gpt4 key购买 nike

我正在开发一个具有后台进程的应用程序,我的应用程序中的一些功能只有在用户打开屏幕并解锁“锁定屏幕”时才能使用。有针对SCREEN_ONSCREEN_OFFUSER_PRESENT 的操作。

我在 SCREEN_OFF 事件上重置了一个标志,并将其设置在 USER_PRESENT 事件上,这工作正常,但这里我有另一个问题;在设置中有一个选项“使用电源按钮立即锁定”,如果取消选中,设备将在 sleep 5 秒后才会锁定。

现在,如果用户关闭屏幕并在 5 秒内打开它,将调用 SCREEN_OFF 事件而永远不会调用 USER_PRESENT 事件。

是否有针对 USER_NOT_PRESENT 或 DEVICE_LOCKED 的任何操作,以便我可以在那里重置我的标志?

注意:如果我知道设置"lock_screen_lock_after_timeout""power_button_instantly_locks",我可以变通。但是我可以获得如下“lock_screen_lock_after_timeout”设置但不知道如何获得“power_button_instantly_locks”

ContentResolver mResolver = ctx.getContentResolver();
long timeout = Settings.Secure.getLong(mResolver, "lock_screen_lock_after_timeout", 0);

谢谢。任何其他方法也应受到赞赏。

最佳答案

使用反射获取power_button_instantly_locks的设置值。请看下面的代码:

/**
*This function is used to detect the system setting: Power button instantly locks. To see whether this setting is enabled or not.
* @return true if Power button instantly locks is enabled, otherwise, false.
*/

private boolean isPowerButtonInstantlyLocksOn(){
String LOCK_PATTERN_UTILS="com.android.internal.widget.LockPatternUtils";
String POWER_BUTTON_INSTANTLY_LOCKS="getPowerButtonInstantlyLocks";
boolean isPowerButtonInstantlyLocks=false;
try{
Class<?> lockPatternUtilsClass=Class.forName(LOCK_PATTERN_UTILS);
Object lockPatternUtils=lockPatternUtilsClass.getConstructor(Context.class).newInstance(this);

// According to the source code of Android 6, the function getPowerButtonInstantlyLocks is declared with a parameter "int userId"
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
Method method = lockPatternUtilsClass.getMethod(POWER_BUTTON_INSTANTLY_LOCKS,new Class[]{int.class});
isPowerButtonInstantlyLocks=Boolean.valueOf(String.valueOf(method.invoke(lockPatternUtils,userID())));
}else {
Method method = lockPatternUtilsClass.getMethod(POWER_BUTTON_INSTANTLY_LOCKS);
isPowerButtonInstantlyLocks=Boolean.valueOf(String.valueOf(method.invoke(lockPatternUtils,null)));
}
}catch (Exception e){
Crashlytics.logException(e);
e.printStackTrace();
}
return isPowerButtonInstantlyLocks;
}

/**
* This function is used to check the current userID
* @return
*/
private int userID(){
// myUserId is a hidden function in Class UserHandle that we want to use to return the current user ID
String MY_USER_ID="myUserId";
int userId=0;

try{
// android.os.Process.myUserHandle() is called to get a UserHandle instance
// myUserHandle requires API 17, but it is OK, cause the function userID() will only be called at API 23
if (Build.VERSION.SDK_INT>Build.VERSION_CODES.JELLY_BEAN){
UserHandle userHandle=android.os.Process.myUserHandle();
Method method = UserHandle.class.getMethod(MY_USER_ID);
userId=Integer.valueOf(String.valueOf(method.invoke(userHandle,null)));
}
}catch (Exception e){
Crashlytics.logException(e);
e.printStackTrace();
}

return userId;
}

关于Android:如何知道设备何时锁定和解锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37351336/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com