歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android一鍵鎖屏開發全過程【源碼+附圖】

Android一鍵鎖屏開發全過程【源碼+附圖】

日期:2017/3/1 10:37:25   编辑:Linux編程

一、項目簡介:

項目:《Android 一鍵鎖屏》

開發周期:4天

代碼量:100行

二、項目流程:

三、項目代碼

1、主程序代碼:

  1. private DevicePolicyManager policyManager;
  2. private ComponentName componentName;
  3. @Override
  4. protected void onCreate(Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView(R.layout.locklayout);
  7. //獲取設備管理服務
  8. policyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
  9. //AdminReceiver 繼承自 DeviceAdminReceiver
  10. componentName = new ComponentName(this, AdminReceiver.class);
  11. mylock();
  12. // killMyself ,鎖屏之後就立即kill掉我們的Activity,避免資源的浪費;
  13. android.os.Process.killProcess(android.os.Process.myPid());
  14. }

2、其中,mylock()為:

  1. private void mylock(){
  2. boolean active = policyManager.isAdminActive(componentName);
  3. if(!active){//若無權限
  4. activeManage();//去獲得權限
  5. policyManager.lockNow();//並鎖屏
  6. }
  7. if (active) {
  8. policyManager.lockNow();//直接鎖屏
  9. }
  10. }

3、activeManage()代碼為:

  1. private void activeManage() {
  2. // 啟動設備管理(隱式Intent) - 在AndroidManifest.xml中設定相應過濾器
  3. Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
  4. //權限列表
  5. intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
  6. //描述(additional explanation)
  7. intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "------ 其他描述 ------");
  8. startActivityForResult(intent, 0);
  9. }

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. </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>
Copyright © Linux教程網 All Rights Reserved