Android一键锁屏开发全过程【源码+附图】


一、项目简介:

项目:《Android 一键锁屏》

开发周期:4天

代码量:100行

二、项目流程:

三、项目代码

1、主程序代码:

  1. private DevicePolicyManager policyManager;   
  2. private ComponentName componentName;   
  3.   
  4. @Override  
  5. protected void onCreate(Bundle savedInstanceState) {   
  6.         super.onCreate(savedInstanceState);   
  7.         setContentView(R.layout.locklayout);   
  8.            
  9.         //获取设备管理服务   
  10.         policyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);   
  11.            
  12.         //AdminReceiver 继承自 DeviceAdminReceiver   
  13.         componentName = new ComponentName(this, AdminReceiver.class);   
  14.            
  15.         mylock();   
  16.     //  killMyself ,锁屏之后就立即kill掉我们的Activity,避免资源的浪费;      
  17.         android.os.Process.killProcess(android.os.Process.myPid());       
  18.            
  19. }  

2、其中,mylock()为:

  1. private void mylock(){   
  2.        
  3.     boolean active = policyManager.isAdminActive(componentName);   
  4.     if(!active){//若无权限   
  5.         activeManage();//去获得权限   
  6.         policyManager.lockNow();//并锁屏   
  7.     }   
  8.     if (active) {   
  9.             policyManager.lockNow();//直接锁屏   
  10.     }   
  11. }  

3、activeManage()代码为:

  1. private void activeManage() {   
  2.         // 启动设备管理(隐式Intent) - 在AndroidManifest.xml中设定相应过滤器   
  3.         Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);   
  4.            
  5.         //权限列表   
  6.         intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);   
  7.   
  8.         //描述(additional explanation)   
  9.                 intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "------ 其他描述 ------");   
  10.   
  11.                 startActivityForResult(intent, 0);   
  12. }  

4、AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>   
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="cn.hnu"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">   
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">   
  7.         <activity android:name=".LockFirst"  
  8.                   android:label="@string/app_name">   
  9.             <intent-filter>   
  10.                 <action android:name="android.intent.action.MAIN" />   
  11.                 <category android:name="android.intent.category.LAUNCHER" />   
  12.             </intent-filter>   
  13.         </activity>   
  14.         <!-- 设备管理 -->   
  15.         <receiver android:name=".AdminReceiver"  
  16.                   android:label="@string/app_name"  
  17.                   android:description="@string/app_name"  
  18.                   android:permission="android.permission.BIND_DEVICE_ADMIN">   
  19.                 <meta-data android:name="android.app.device_admin"  
  20.                            android:resource="@xml/lock_screen" />   
  21.                    <intent-filter>   
  22.                         <action   
  23.                            android:name="android.app.action.DEVICE_ADMIN_ENABLED" />   
  24.                    </intent-filter>   
  25.         </receiver>   
  26.     </application>   
  27.        
  28.   
  29. </manifest>   

 5、其中lock_screen.xml(lock_screen.xml文件放在res/xml文件夹下)代码为:

  1. <?xml version="1.0" encoding="UTF-8"?>    
  2. <device-admin   
  3.   xmlns:android="http://schemas.android.com/apk/res/android">   
  4.     <uses-policies>   
  5.         <!-- 强行锁定  在里仅这个是需要的-->   
  6.         <force-lock />   
  7.         <!-- 清除所有数据(恢复出厂设置) -->   
  8.         <wipe-data />   
  9.          <!-- 重置密码 -->   
  10.         <reset-password />   
  11.         <!-- 限制密码选择 -->   
  12.          <limit-password />   
  13.          <!-- 监控登录尝试 -->   
  14.           <watch-login />   
  15.     </uses-policies>   
  16. </device-admin>  
  • 1
  • 2
  • 下一页

相关内容