设备管理员

一旦程序拥有系统管理员权限,该应用程序将不能被卸载和停用,除非在系统设置——安全——设备管理器
一旦拥有系统管理员权限受系统保护,如果想卸载该应用就要取消系统保护

参考开发文档: Develop --> API Guides --->Administration

开发文档解读:
查看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" 注:DeviceAdminReceiver 的子类
         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" ?>
< device-admin xmlns:android = "http://schemas.android.com/apk/res/android" >
   < 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);
     }
 
}

好了一键锁屏完成了








设置截屏密码:
resetPassword("123",0);
lockNow();
弹出设备管理员激活界面:
isAdminAction(who);




转载于:https://www.cnblogs.com/candledragle/p/4243015.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值