本文實(shí)例講述了Android編程實(shí)現(xiàn)一鍵鎖屏的方法。分享給大家供大家參考,具體如下:
這里要用到下面兩個(gè)類:
DeviceAdminReceiver 設(shè)備管理組件。這個(gè)類提供了一個(gè)方便解釋由系統(tǒng)發(fā)出的意圖的動(dòng)作。你的設(shè)備管理應(yīng)用程序必須包含一個(gè)DeviceAdminReceiver的子類。本程序中,就代表一個(gè)手機(jī)上的設(shè)備管理器.
DevicePolicyManager 一個(gè)管理設(shè)備上規(guī)范的類。 大多數(shù)客戶端必須聲明一個(gè)用戶當(dāng)前已經(jīng)啟用的DeviceAdminReceiver。 這個(gè)DevicePolicyManager為一個(gè)或者多個(gè)DeviceAdminReceiver實(shí)例管理這些規(guī)范。
DevicePolicyManager 的實(shí)例有個(gè)方法叫l(wèi)ockNow可以直接鎖定屏幕.但是在這之前,需要激活程序中的設(shè)備管理器.
下面是主類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)) { //判斷是否有權(quán)限(激活了設(shè)備管理器) policyManager.lockNow(); // 直接鎖屏 android.os.Process.killProcess(android.os.Process.myPid()); } else { activeManager(); //激活設(shè)備管理器獲取權(quán)限 } } // 解除綁定 public void Bind(View v){ if (componentName!= null ){ policyManager.removeActiveAdmin(componentName); activeManager(); } } @Override protected void onResume() { //重寫此方法用來在第一次激活設(shè)備管理器之后鎖定屏幕 if (policyManager!= null && policyManager.isAdminActive(componentName)) { policyManager.lockNow(); android.os.Process.killProcess(android.os.Process.myPid()); } super .onResume(); } private void activeManager() { //使用隱式意圖調(diào)用系統(tǒng)方法來激活指定的設(shè)備管理器 Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, "一鍵鎖屏" ); startActivity(intent); } } |
下面是設(shè)備管理器類LockReceiver,這是一個(gè)繼承自DeviceAdminReceiver的類,可以接收到激活/接觸激活的廣播,進(jìn)行下一步操作,本程序中,只是簡單打印一下信息.
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 > |
其中l(wèi)ock_screen是設(shè)備管理器的權(quán)限聲明,需要在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.現(xiàn)在自己也可以做一鍵鎖屏了.不用去網(wǎng)上找各種各樣帶廣告帶推送的了.
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。