一旦程序拥有系统管理员权限,该应用程序将不能被卸载和停用,除非在系统设置——安全——设备管理器
一旦拥有系统管理员权限受系统保护,如果想卸载该应用就要取消系统保护
开发文档解读:
查看Administration下的
DEVICE POLICIES————》 Device Administration
官方提供给一般应用程序的可以获得系统管理员权限的政策(Policies):大致可以分为
1、Note that the Device Administration API currently only supports passwords for screenlock:
2、Disable camera
3、Require storage encryption
4、Prompt user to set a new password.提醒用户设置新密码
5、Lock device immediately.立即锁屏
6、Wipe the device's data (that is, restore the device to its factory defaults).檫除设备上的数据
在清单文件中添加配置解读:接受系统设备管理员权限广播
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<
activity
android:name
=
".app.DeviceAdminSample"
android:label
=
"@string/activity_sample_device_admin"
>
<
intent-filter
>
<
action
android:name
=
"android.intent.action.MAIN"
/>
<
category
android:name
=
"android.intent.category.SAMPLE_CODE"
/>
</
intent-filter
>
</
activity
>
<
receiver
android:name
=
".app.DeviceAdminSample$DeviceAdminSampleReceiver" 注:
android:label
=
"@string/sample_device_admin"
android:description
=
"@string/sample_device_admin_description"
android:permission
=
"android.permission.BIND_DEVICE_ADMIN"
> 注:绑定权限
<
meta-data
android:name
=
"android.app.device_admin"
android:resource
=
"@xml/device_admin_sample"
/>
<
intent-filter
>
<
action
android:name
=
"android.app.action.DEVICE_ADMIN_ENABLED"
/> 注:设备管理员权限是否可用
</
intent-filter
>
</
receiver
>
|
步骤:
1、写一个类MyDeviceAdimin继承DeviceAdminReceiver
1
2
3
4
|
public
class
MyAdminReceiver
extends
DeviceAdminReceiver {
}
|
2、在清单文件中配置:
1
2
3
4
5
6
7
8
9
10
|
<
receiver
android:name
=
".app.DeviceAdminSample$DeviceAdminSampleReceiver"
android:label
=
"@string/sample_device_admin"
android:description
=
"@string/sample_device_admin_description"
android:permission
=
"android.permission.BIND_DEVICE_ADMIN"
>
<
meta-data
android:name
=
"android.app.device_admin"
android:resource
=
"@xml/device_admin_sample"
/>
<
intent-filter
>
<
action
android:name
=
"android.app.action.DEVICE_ADMIN_ENABLED"
/>
</
intent-filter
>
</
receiver
>
|
在res文件夹下创建xml文件夹——再创建device_admin_sample.xml文件
device_admin_sample.xml文件的内容:不需要修改直接在开发文档中拷贝就行
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
uses-policies
> 用户可以使用到的一些策略标签
<
limit-password
/> 设置密码规则的一些策略
<
watch-login
/> 在解屏的时候可以看到它解屏的过程
<
reset-password
/> 从新设置密码,在锁屏之前可以写一段代码设置解屏代码
<
force-lock
/> 强制锁屏
<
wipe-data
/> 清除数据
<
expire-password
/> 设置密码在什么时候过期
<
encrypted-storage
/> 加密存储
<
disable-camera
/> 关闭相机
</
uses-policies
>
</
device-admin
>
|
以上设置就是为应用程序添加了设备管理员权限
3、一键锁屏源码案例讲解使用过程:
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
|
public
class
MainActivity
extends
Activity {
private
DevicePolicyManager dpm;
private
ComponentName cn;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//在系统服务中获得设备管理员
dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);
cn =
new
ComponentName(
this
, MyAdminReceiver.
class
);//注:设备管理员类的class这里写的设备管理员类是:MyAdminReceiver
}
public
void
lockScreen(View view){
//判断当前管理员策略是否被激活
boolean
active = dpm.isAdminActive(cn);
if
(active){
dpm.lockNow();//调用一键锁屏功能
dpm.resetPassword(
"1234"
,
0
);//设置解屏密码
}
else
{
//激活管理员策略
openAmin();
}
}
/**
* 激活管理员策略
*/
private
void
openAmin() {
Intent intent =
new
Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, cn);
intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
"开启管理员策略之后,可以一键锁屏、远程删除数据"
);
//打开激活管理员策略的界面
startActivity(intent);
}
}
|
好了一键锁屏完成了