[英]How to lock/unlock the screen with Pattern/Password mode in Android?
I was successful to lock/unlock my screen using DevicePolicyManager
and KeyguardManager
in Android L. It worked well when I lock/unlock screen using swipe mode (no security). 我成功锁定/解锁使用我的屏幕
DevicePolicyManager
和KeyguardManager
在Android的L.它运作良好,当我用划模式(无安全性)锁定/解锁屏幕。 However, I cannot lock/unlock it when I lock/unlock screen by using Pattern and Password mode (higher security). 但是,当我使用模式和密码模式锁定/解锁屏幕时,我无法锁定/解锁它(更高的安全性)。 Is it possible to lock/unlock screen with high security using
DevicePolicyManager
and KeyguardManager
. 是否有可能锁定/使用高安全性的解锁屏幕
DevicePolicyManager
和KeyguardManager
。 ? ? This is what I did
这就是我做的
protected static final int REQUEST_ENABLE = 0;
DevicePolicyManager devicePolicyManager;
ComponentName adminComponent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.btn);
button.setOnClickListener(btnListener);
}
//LOCK
Button.OnClickListener btnListener = new Button.OnClickListener() {
public void onClick(View v) {
adminComponent = new ComponentName(MainActivity.this, Darclass.class);
devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
if (!devicePolicyManager.isAdminActive(adminComponent)) {
Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent);
startActivityForResult(intent, REQUEST_ENABLE);
} else {
devicePolicyManager.lockNow();
}
}
};
//UNLOCK
private KeyguardManager keyguardManager;
KeyguardManager.KeyguardLock kl;
keyguardManager = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
kl = keyguardManager.newKeyguardLock("MyKeyguardLock");
kl.disableKeyguard();
Note that, I am using it in a service. 请注意,我在服务中使用它。
You mentioned that you are using the code in a Service, but while constructing your adminComponent ComponentName object you provide MainActivity.this as your context ! 您提到您正在使用服务中的代码,但在构建adminComponent ComponentName对象时,您提供MainActivity.this作为您的上下文! MainActivity.this might be NULL if your MainActivity is not currently running.
如果您的MainActivity当前未运行,则MainActivity.this可能为NULL。
I suggest you initiate adminComponent with the Service class as the Context. 我建议您使用Service类作为Context启动adminComponent。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.