Android 处理屏幕解锁和设置锁屏密码

完成自定义service后,在frameworks/base/core/java/android/app/customized/CustomizedService.java中实现一下方法

    public boolean setLockPassword(String pwd) {
        long ident = Binder.clearCallingIdentity();
        LockPatternUtils lockPatternUtils = new LockPatternUtils(mContext);
        lockPatternUtils.saveLockPassword(pwd, null, lockPatternUtils
                .getRequestedPasswordQuality(mContext.getUserId()),
                UserHandle.USER_OWNER);
        lockPatternUtils.setLockScreenDisabled(false, mContext.getUserId());
        boolean result = lockPatternUtils.isLockScreenDisabled(mContext
                .getUserId());
        Log.d(TAG, "setLockPassword" + !result);
        Binder.restoreCallingIdentity(ident);
        return !result;
    }

    public boolean unlockScreen() {
        long ident = Binder.clearCallingIdentity();
        boolean checkResult = true;
        try {
            lockPatternUtils.clearLock(UserHandle.USER_OWNER);
            lockPatternUtils.setLockScreenDisabled(true, mContext.getUserId());
            Intent sendlock = new Intent(
                    "com.android.systemui.keyguard.unlock");
            mContext.sendBroadcastAsUser(sendlock, UserHandle.OWNER);
     
            Thread.sleep(200);  //sleep 0.2s
            lockPatternUtils.setLockScreenDisabled(false, mContext.getUserId());
        } catch (Exception e) {
            checkResult = false;
        }
        Binder.restoreCallingIdentity(ident);
    }

然后修改frameworks/base/packages/SystemUI/src/com/android/systemui/keyguard/KeyguardViewMediator.java 文件

private static final String UNLOCK_KEYGUARD_ACTION = "com.android.systemui.keyguard.unlock";

                                   
    filter.addAction(UNLOCK_KEYGUARD_ACTION);  //在IntentFilter中添加UNLOCK_KEYGUARD_ACTION

    if(UNLOCK_KEYGUARD_ACTION.equals(action)){
        handleKeyguardDone(true);  //在收到intent后,receiver中做处理
    }

猜你喜欢

转载自blog.csdn.net/lancelots/article/details/82388711