设备管理员(锁屏,回复出厂设置等功能)

1.新建一个子类 继承 DeviceAdminReceiver

  

public class MyAdmin extends DeviceAdminReceiver {

}

2.到清单文件注册这个广播接受者

<receiver            
            android:name="com.example.lockscreen.MyAdmin" 
            android:description="@string/sample_device_admin_description"
            android:label="@string/sample_device_admin"
            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>
然后声明所需权限 (这个对应上面的 receiver-> meta-data -> android:resorece的目录)

<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.开启设备管理员权限

	public void startAdmin(View view) {
		Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
		ComponentName mDeviceAdmin = new ComponentName(MainActivity.this, MyAdmin.class);
        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdmin);
        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
                "啊啊啊啊啊");
        startActivity(intent);
	}


4.声明变量并赋值

private DevicePolicyManager dpm = null;

在oncreate中

dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE);

接下来进行系列操作
比如锁屏 并重设密码

<span style="white-space:pre">		</span>dpm.lockNow();<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>dpm.resetPassword("1234", 0);//android 4.4 密码为4位 否则始终错误 不知道是不是没有设置密码长度的关系

5.关闭授权

(必须要关闭授权才能对软件进行卸载)

ComponentName mDeviceAdmin = new ComponentName(MainActivity.this, MyAdmin.class);//第二个参数写前面继承的广播接受者子类
		dpm.removeActiveAdmin(mDeviceAdmin);

详情请查看android官方文档: Admnistrator->Devices Polices

地址: http://developer.android.com/guide/topics/admin/device-admin.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值