超级管理员 卸载应用 清除数据 锁屏

配置超级管理员步骤:

1. 自定义Receiver,继承DeviceAdminReceiver

2. 配置manifest

		 <receiver
            android:name=".AdminReceiver"
            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>

3. 添加配置文件@xml/device_admin_sample

<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>

4. 通过代码打开超级管理员权限( 调用下面的方法):

		public void openAdmin() {
			Intent intent = new Intent

(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
			ComponentName component = new ComponentName(this, 

AdminReceiver.class); //组件名,写刚才自定义的广播接收者
	        intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, component);
	        intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,
	               "打开超级管理员权限,可以一键锁屏,删除数据等");
			startActivity(intent);
		}

激活了以后就可以使用了:

5. 获取DevicePolicyManager

		<span style="font-size:14px;">mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE)</span>


6. 验证是否已经激活设备管理员


<span style="font-size:14px;">                ComponentName component = new ComponentName(this, AdminReceiver.class);
		if (mDPM.isAdminActive(component)) {//验证是否自己自定义的组件.
		}</span>

7. 锁屏

		mDPM.lockNow();//锁屏
		mDPM.resetPassword("123", 0);//设置锁屏密码
		注意: 必须先打开设置->安全->设备管理器的权限,否则运行崩溃

8. 卸载应用(调用下面的方法):

		public void uninstall(View view) {
			ComponentName component = new ComponentName(this, AdminReceiver.class); 
			mDPM.removeActiveAdmin(component);//删除超级管理权限,没有激活的应用才可以卸载
			
			//跳转到卸载页面
			Intent intent = new Intent(Intent.ACTION_VIEW);
			intent.addCategory(Intent.CATEGORY_DEFAULT);
			intent.setData(Uri.parse("package:" + getPackageName()));//需要卸载的程序包名.只要当前手机里有的程序可以卸载.
			startActivity(intent);
		}

9. 清除数据

		mDPM.wipeData(0);//恢复出厂设置
		mDPM.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);//清除sdcard内容



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值