本文介绍了Android的 - 如何完全禁用键盘锁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想控制启用/禁用键盘锁设备上。要做到这一点,我使用的Andr​​oid SDK的DevicePolicyManager和KeyguardLock API。

I want to control enabling/disabling keyguard on the device.To do that I'm using DevicePolicyManager and KeyguardLock API of Android SDK.

下面是我的实现来管理这样的:

Below is my implementation to manage this:

public class DeviceLocker {

private static DeviceLocker instance;

public static synchronized DeviceLocker getInstance(Context context) {
    if(instance==null) {
        instance = new DeviceLocker(context);
    }
    return instance;
}

private Context context;
private KeyguardLock lock;

private DeviceLocker(Context context) {
    this.context = context;
}

public void lock() {
    lock(true);
}

public void lock(boolean lockNow) {
    getLock().reenableKeyguard();
    DevicePolicyManager devicePolicyManager = getDevicePolicyManager();
    if(devicePolicyManager==null) {
        return;
    }
    LocalStorage storage = LocalStorage.from(context);

    boolean result = devicePolicyManager.resetPassword(storage.getPassword(),
            DevicePolicyManager.RESET_PASSWORD_REQUIRE_ENTRY);

    if(lockNow) {
        devicePolicyManager.lockNow();
    }
    storage.setDeviceLocked(true);
}

public void unlock() {
    DevicePolicyManager devicePolicyManager = getDevicePolicyManager();
    if(devicePolicyManager==null) {
        return;
    }
    devicePolicyManager.resetPassword("",0);
    getLock().disableKeyguard();
    LocalStorage.from(context).setDeviceLocked(false);
}

private KeyguardLock getLock() {
    if(lock==null){
        KeyguardManager kgManager = (KeyguardManager)context.getSystemService(Activity.KEYGUARD_SERVICE);
        lock = kgManager.newKeyguardLock(Context.KEYGUARD_SERVICE);
    }
    return lock;
}

private DevicePolicyManager getDevicePolicyManager() {
    DevicePolicyManager devicePolicyManager =
        (DevicePolicyManager)context.getSystemService(Context.DEVICE_POLICY_SERVICE);
    ComponentName deviceAdmin = new ComponentName(context, WatchGuardDeviceAdminReceiver.class);
    LocalStorage storage = LocalStorage.from(context);
    if(!devicePolicyManager.isAdminActive(deviceAdmin)) {
        return null;
    }
    if(!storage.isPasswordSet()) {
        UIUtils.showMessage(context, R.string.password_not_set);
        return null;
    }
    devicePolicyManager.setPasswordQuality(deviceAdmin,DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED);

    return devicePolicyManager;
}

}

它的工作原理确定关于屏幕锁,但是解锁fucntionality工作的一些问题:有时它的作品,因为我想(完全删除任何类型的键盘锁屏),但有时它显示解锁采用滑盖键盘锁屏。

It works ok regarding the lock of the screen, but unlock fucntionality works with some issues:sometimes it works as I want(completely removes any type of keyguard screen), but sometimes it shows "Unlock with slide" keyguard screen.

你知道是什么问题吗?如何使它工作稳定(至少在所有情况下,无论是演出解锁滑或彻底删除键盘锁)?

Do you know what is the problem here? How to make it work stable (at least in all cases either show "Unlock to slide" or completely remove keyguard)?

先谢谢您的帮助。

修改

只是想指出的是,我的解决方案的工作,但问题是,它不稳定的(有时完全删除键盘锁,有时显示幻灯片键盘锁)。而我也用它不只是禁用键盘保护,同时表现出一些活动,但控制装置共同的锁定/解锁,所以我用这个code的服务,因此,我不能叫 getWindow( ).addFlags(..),因为我没有足够的窗口提出申请。

Just want to point out that my solution works but the problem is that it works unstable (sometimes removes keyguard completely and sometimes show "slide" keyguard).And also I use it not just to disable keyguard while showing some activity but to control lock/unlock of device in common, so I use this code in Service, therefore I can't call getWindow().addFlags(..) cause I don't have the Window to apply.

只是想知道,也许任何人处理这个不稳定的行为。

Just wondering maybe anyone dealt with this unstable behavior.

推荐答案

您需要添加这个code:

You need to add this code:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD_DIALOG);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED|WindowManager.LayoutParams.FLAG_FULLSCREEN|
                WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lockscreen);

        startService(new Intent(this,LockService.class).setAction(Intent.ACTION_SCREEN_OFF));
    }

创建LockService有:

Create LockService with:

@Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {

        if(intent == null || intent.getAction() == null)
            return START_NOT_STICKY;

        String action = intent.getAction();

        if(action.equals(ACTION_SCREEN_OFF))
        {
           KeyguardManager.KeyguardLock k1;
           KeyguardManager km =(KeyguardManager)getSystemService(KEYGUARD_SERVICE);
           k1= km.newKeyguardLock("IN");
           k1.disableKeyguard();
        }

        return START_NOT_STICKY;
    }

其实我也不知道为什么,但 k1.disableKeyguard(); 仅在服务。

这篇关于Android的 - 如何完全禁用键盘锁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-04 08:21