如果用戶沒有鎖定屏幕或僅啓用輕掃,我希望我的應用行爲不同(不存儲內容)。 頂部答案在這裏:check whether lock was enabled or not已被編輯,說在升級到Android 4.3後代碼不再有效。Android 4.3:如何檢查用戶是否啓用了鎖定?
反正有沒有在Android 4.3上檢測到這個?
如果用戶沒有鎖定屏幕或僅啓用輕掃,我希望我的應用行爲不同(不存儲內容)。 頂部答案在這裏:check whether lock was enabled or not已被編輯,說在升級到Android 4.3後代碼不再有效。Android 4.3:如何檢查用戶是否啓用了鎖定?
反正有沒有在Android 4.3上檢測到這個?
好的,所以它似乎有可能與一些反思。不是最好的解決辦法,但至少它的工作原理,我們嘗試在設備上:
Class<?> clazz = Class.forName("com.android.internal.widget.LockPatternUtils");
Constructor<?> constructor = clazz.getConstructor(Context.class);
constructor.setAccessible(true);
Object utils = constructor.newInstance(context);
Method method = clazz.getMethod("isSecure");
return (Boolean)method.invoke(utils);
你可以用KeyguardManager檢查:
KeyguardManager keyguardMgr = (KeyguardManager) getSystemService(Context.KEYGUARD_SERVICE);
if(keyguardMgr.isKeyguardSecure()){
// Secure keyguard, the keyguard requires a password to unlock
} else {
// Insecure keyguard, the keyguard doesn't require a password to unlock
}
您可以KeyguardManager的方法isKeyguardSecure檢查(),以確保如果鎖或密碼是否啓用。
KeyguardManager keyguardManager =(KeyguardManager)getSystemService(KEYGUARD_SERVICE); (this,「KeyGuardEnabled?」+ keyguardManager.isKeyguardSecure(),Toast.LENGTH_LONG).show();
請注意,這不適用於Android 6.x或7. – FunkSoulBrother
是的,使用像這樣的反射是我希望在某些時候打破的黑客。你知道如何在這些版本上做到這一點? – Erlend
當然,請看https://developer.android.com/reference/android/app/KeyguardManager.html。對於Android 5.0及更高版本,它可以完成這項工作。 – FunkSoulBrother