我开发的应用程序具有后台进程,我的应用程序中的一些功能只有在用户打开屏幕并解锁“锁定屏幕”时才会起作用。有 SCREEN_ON , SCREEN_OFF 和 USER_PRESENT 的操作。
我在 SCREEN_OFF 事件中重置了一个标记并将其设置为 USER_PRESENT 事件,这样可以正常工作但是我还有另一个问题; 在设置中有一个选项"立即使用电源按钮锁定"如果未选中,设备将在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);
谢谢。任何其他方法也应该受到赞赏。
答案 0 :(得分: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;
}