简体   繁体   English

如何在Android中使用模式/密码模式锁定/解锁屏幕?

[英]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). 我成功锁定/解锁使用我的屏幕DevicePolicyManagerKeyguardManager在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 . 是否有可能锁定/使用高安全性的解锁屏幕DevicePolicyManagerKeyguardManager ? 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.

相关问题 以编程方式安装Android模式锁定/解锁屏幕 - Android pattern lock/unlock screen programmatically Android以编程方式设置/更改/删除锁定屏幕PIN,密码或解锁图案 - Android to set/change/remove Lock Screen PIN, Password or Unlock Pattern programatically 如何在Android上检测屏幕固定的锁定和解锁 - How to detect lock and unlock of screen pinning on android 通知到达Android应用后无法解锁屏幕图案/锁定密码 - Unable to unlock screen pattern/pin lock on notification arrival in Android app 如何在我的应用程序中使用智能锁API来解锁模式模式? - How to use smart lock API in my application to unlock pattern mode? Android:如何通过锁定屏幕显示对话框或活动[未解锁屏幕] - Android : How to show dialog or activity over lock screen[ not unlock the screen] 如何以编程方式锁定/解锁屏幕? - How to Lock/Unlock screen programmatically? 如何在Android模拟器上获取锁定解锁屏幕? - How can i get lock unlock screen on emulator in android? 在 Android 中锁定/解锁屏幕时如何启用/禁用辅助功能服务 - How Enable/Disable Accessibility Service When Lock/Unlock Screen in Android Cordova / Ionic2 | 如何锁定和解锁Android和iOS屏幕? - Cordova/Ionic2 | How to lock and unlock Screen of Android and iOS?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM