一、项目简介:
项目:《Android 一键锁屏》
开发周期:4天
代码量:100行
二、项目流程:
三、项目代码
1、主程序代码:
private DevicePolicyManager policyManager; private ComponentName componentName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.locklayout); //获取设备管理服务 policyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); //AdminReceiver 继承自 DeviceAdminReceiver componentName = new ComponentName(this, AdminReceiver.class); mylock(); // killMyself ,锁屏之后就立即kill掉我们的Activity,避免资源的浪费; android.os.Process.killProcess(android.os.Process.myPid()); }2、其中,mylock()为:
private void mylock(){ boolean active = policyManager.isAdminActive(componentName); if(!active){//若无权限 activeManage();//去获得权限 policyManager.lockNow();//并锁屏 } if (active) { policyManager.lockNow();//直接锁屏 } }3、activeManage()代码为:
private void activeManage() { // 启动设备管理(隐式Intent) - 在AndroidManifest.xml中设定相应过滤器 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); //权限列表 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); //描述(additional explanation) intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "------ 其他描述 ------"); startActivityForResult(intent, 0); }