/** * 初始化操作 */ public FingerPrintSDK init(Activity mActivity){ dpm = (DevicePolicyManager)mActivity.getSystemService(DEVICE_POLICY_SERVICE); lockReceiver = new LockReceiver(); IntentFilter filter = new IntentFilter(); //添加action filter.addAction("android.intent.action.SCREEN_OFF"); filter.addAction("android.intent.action.SCREEN_ON"); //注册广播接收者 mActivity.registerReceiver(lockReceiver,filter); return printSDK; }
public void lockcreen(Activity mActivity) { ComponentName who = new ComponentName(mActivity, LockReceiver.class); // 判断是否已经开启管理员权限 if (dpm.isAdminActive(who)) { // 锁屏 dpm.lockNow(); // 设置屏幕密码 第一个是密码 第二个是附加参数 // dpm.resetPassword("123", 0); // 清楚数据 // WIPE_EXTERNAL_STORAGE 清楚sdcard的数据 // 0 恢复出厂设置 //dpm.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE); } else { openAdmin(mActivity); dpm.lockNow(); } } /** * 代码开启管理权限 */ public void openAdmin(Activity mActivity) { // 创建一个Intent 添加设备管理员 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); // 激活MyAdmin广播接收着 ComponentName who = new ComponentName(mActivity, LockReceiver.class); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, who); // 说明用户开启管理员权限的好处 intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "开启可以一键锁屏,防止误碰"); mActivity.startActivity(intent); } public void unRegister(Activity mActivity){ if(mActivity!=null&&lockReceiver!=null) { mActivity.unregisterReceiver(lockReceiver);//注销广播 } }
调用锁频:
lockcreen(mActivity);