完成自定义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. Android 开发中怎么使用自定义字体?
  2. Android 比Timer更好方法
  3. Android中AppWidget使用方法
  4. Android 高德地图在清除Marker的时候会把自定义的定位蓝点也清除
  5. Android 官方 Lambda支持方法
  6. android BroadcastReceiver遇到 java.lang.IllegalAccessExcepti
  7. Android定义的路径全局变量
  8. Android 调用系统相机拍照保存以及调用系统相册的方法
  9. 自定义View

随机推荐

  1. Activity页面切换的效果
  2. Android(安卓)设置虚线分割线
  3. Android(安卓)Framework---styles.xml
  4. Android(安卓)圆角图片的实现
  5. Android可拖动的ImageView
  6. android GPS定位详解(6)
  7. android的service
  8. Some thing about android:visibility
  9. [Android][HTC]HTC Android(安卓)Reboot
  10. android代码片段一