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

完成自定义service后,自定义service参考之前的博客.

在frameworks/base/core/java/android/app/customized/ICustomizedService.aidl定义方法

package android.app.customized;
 
interface ICustomizedService{
    boolean setLockPassword(String pwd);
    boolean unlockScreen();
}

 在frameworks/base/core/java/android/app/customized/CustomizedManager.java中实现方法

package android.app.customized;
 
import android.util.Log;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.RemoteException;
import android.provider.Settings;
import java.io.IOException;
import android.os.ServiceManager;
import android.os.IBinder;
import java.util.List;
import android.app.ActivityManager;
import android.graphics.Bitmap;
 
 
public class CustomizedManager{
    private static final String TAG="CustomizedManager";
    private static final boolean DBG=true;
    
    private static ICustomizedService mService;
    private final Context mContext;
 
 
    public CustomizedManager(Context context){
        mContext = context;
        mService = ICustomizedService.Stub.asInterface(
                ServiceManager.getService("customized"));
    }
    private static ICustomizedService getService(){
        if (mService != null) {
            return mService;
        }
       
        IBinder b = ServiceManager.getService("customized");
        mService = ICustomizedService.Stub.asInterface(b);
        return mService;
    }
 
    public boolean setLockPassword(String pwd){
	    ICustomizedService service = getService();
        try {
            return service.setLockPassword(pwd);
        } catch (RemoteException e) {}
        return false;
    } 


    public boolean unlockScreen(){
	    ICustomizedService service = getService();
        try {
            return service.unlockScreen();
        } catch (RemoteException e) {}
        return false;
    } 
 
}

在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";

    IntentFilter filter = new IntentFilter();
    filter.addAction(UNLOCK_KEYGUARD_ACTION);  //在IntentFilter中添加UNLOCK_KEYGUARD_ACTION
    Intent intent = context.registerReceiver(mDockReceiver, filter);                              
  

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

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值