完成自定义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中做处理
}
如果觉得《java修改手机锁屏密码 Android 处理屏幕解锁和设置锁屏密码》对你有帮助,请点赞、收藏,并留下你的观点哦!