本文實例講述了Android編程實現一鍵鎖屏的方法。分享給大家供大家參考,具體如下:
這里要用到下面兩個類:
DeviceAdminReceiver 設備管理組件。這個類提供了一個方便解釋由系統發出的意圖的動作。你的設備管理應用程序必須包含一個DeviceAdminReceiver的子類。本程序中,就代表一個手機上的設備管理器.
DevicePolicyManager 一個管理設備上規范的類。 大多數客戶端必須聲明一個用戶當前已經啟用的DeviceAdminReceiver。 這個DevicePolicyManager為一個或者多個DeviceAdminReceiver實例管理這些規范。
DevicePolicyManager 的實例有個方法叫lockNow可以直接鎖定屏幕.但是在這之前,需要激活程序中的設備管理器.
下面是主類LockActivity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package com.iceman.test; import android.app.Activity; import android.app.admin.DevicePolicyManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.os.Bundle; public class LockActivity extends Activity { private DevicePolicyManager policyManager; private ComponentName componentName; @Override public void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.main); } public void LockScreen(View v){ policyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); componentName = new ComponentName( this , LockReceiver. class ); if (policyManager.isAdminActive(componentName)) { //判斷是否有權限(激活了設備管理器) policyManager.lockNow(); // 直接鎖屏 android.os.Process.killProcess(android.os.Process.myPid()); } else { activeManager(); //激活設備管理器獲取權限 } } // 解除綁定 public void Bind(View v){ if (componentName!= null ){ policyManager.removeActiveAdmin(componentName); activeManager(); } } @Override protected void onResume() { //重寫此方法用來在第一次激活設備管理器之后鎖定屏幕 if (policyManager!= null && policyManager.isAdminActive(componentName)) { policyManager.lockNow(); android.os.Process.killProcess(android.os.Process.myPid()); } super .onResume(); } private void activeManager() { //使用隱式意圖調用系統方法來激活指定的設備管理器 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "一鍵鎖屏" ); startActivity(intent); } } |
下面是設備管理器類LockReceiver,這是一個繼承自DeviceAdminReceiver的類,可以接收到激活/接觸激活的廣播,進行下一步操作,本程序中,只是簡單打印一下信息.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
import android.app.admin.DeviceAdminReceiver; import android.content.Context; import android.content.Intent; public class LockReceiver extends DeviceAdminReceiver{ @Override public void onReceive(Context context, Intent intent) { super .onReceive(context, intent); System.out.println( "onreceiver" ); } @Override public void onEnabled(Context context, Intent intent) { System.out.println( "激活使用" ); super .onEnabled(context, intent); } @Override public void onDisabled(Context context, Intent intent) { System.out.println( "取消激活" ); super .onDisabled(context, intent); } } |
主配置文件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<? xml version = "1.0" encoding = "utf-8" ?> < manifest xmlns:android = "http://schemas.android.com/apk/res/android" package = "com.iceman.test" android:versionCode = "1" android:versionName = "1.0" > < uses-sdk android:minSdkVersion = "9" /> < application android:icon = "@drawable/ic_launcher" android:label = "@string/app_name" > < activity android:name = ".LockActivity" android:label = "@string/app_name" android:theme = "@android:style/Theme.Translucent" > < intent-filter > < action android:name = "android.intent.action.MAIN" /> < category android:name = "android.intent.category.LAUNCHER" /> </ intent-filter > </ activity > < receiver android:name = ".LockReceiver" android:description = "@string/app_name" android:label = "@string/app_name" android:permission = "android.permission.BIND_DEVICE_ADMIN" > < meta-data android:name = "android.app.device_admin" android:resource = "@xml/lock_screen" /> < intent-filter > < action android:name = "android.app.action.DEVICE_ADMIN_ENABLED" /> </ intent-filter > </ receiver > </ application > </ manifest > |
其中lock_screen是設備管理器的權限聲明,需要在res/xml目錄下以xml文件形式定義
1
2
3
4
5
6
7
|
<? xml version = "1.0" encoding = "UTF-8" ?> < device-admin xmlns:android = "http://schemas.android.com/apk/res/android" > < uses-policies > <!-- 鎖定屏幕 --> < force-lock /> </ uses-policies > </ device-admin > |
OK.現在自己也可以做一鍵鎖屏了.不用去網上找各種各樣帶廣告帶推送的了.
希望本文所述對大家Android程序設計有所幫助。