设备管理器勾选后不能再取消了

自己最近在学习Android过程中,下载了一个Demo尽然不能取消设备管理器,也不能卸载。自己猛一想这事如何实现的?

首先AndroidManifest.xml:

Xml代码  收藏代码
  1. <receiver  
  2.             android:name="com.dean.autosetting.DeviceMyReceiver"  
  3.             android:description="@string/description"  
  4.             android:label="@string/app_name"  
  5.             android:permission="android.permission.BIND_DEVICE_ADMIN" >  
  6.             <meta-data  
  7.                 android:name="android.app.device_admin"  
  8.                 android:resource="@xml/device_admin" />  
  9.   
  10.             <intent-filter>  
  11.                 <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />  
  12.                 <action android:name="android.intent.action.BOOT_COMPLETED" />  
  13.   
  14.                 <category android:name="android.intent.category.HOME" />  
  15.             </intent-filter>  
  16.         </receiver>  

 DeviceMyReceiver.java

Java代码  收藏代码
  1. package com.dean.autosetting;  
  2.   
  3. import android.app.admin.DeviceAdminReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.content.SharedPreferences;  
  7. import android.util.Log;  
  8. import android.widget.Toast;  
  9.   
  10. public class DeviceMyReceiver extends DeviceAdminReceiver {  
  11.   
  12.     @Override  
  13.     public void onReceive(Context context, Intent intent) {  
  14.         super.onReceive(context, intent);  
  15.         // Intent i = new Intent(context, MainActivity.class);  
  16.         // i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  17.         // context.startActivity(i);  
  18.         Log.e("autosetting", "receiver onReceive");  
  19.     }  
  20.   
  21.     /** 
  22.      * 获取设备存储的数值 
  23.      *  
  24.      * @param context 
  25.      * @return 
  26.      */  
  27.     public static SharedPreferences getDevicePreference(Context context) {  
  28.         return context.getSharedPreferences(DeviceMyReceiver.class.getName(), 0);  
  29.     }  
  30.   
  31.     // 密码的特点  
  32.     public static String PREF_PASSWORD_QUALITY = "password_quality";  
  33.     // 密码的长度  
  34.     public static String PREF_PASSWORD_LENGTH = "password_length";  
  35.   
  36.     public static String PREF_MAX_FAILED_PW = "max_failed_pw";  
  37.   
  38.     void showToast(Context context, CharSequence text) {  
  39.         Toast.makeText(context, text, Toast.LENGTH_SHORT).show();  
  40.     }  
  41.   
  42.     @Override  
  43.     public void onEnabled(Context context, Intent intent) {  
  44.         showToast(context, "设备管理:可用");  
  45.     }  
  46.   
  47.     @Override  
  48.     public void onDisabled(Context context, Intent intent) {  
  49.         showToast(context, "设备管理:不可用");  
  50.     }  
  51.   
  52.     @Override  
  53.     public CharSequence onDisableRequested(Context context, Intent intent) {  
  54.         // 这里处理 不可编辑设备。  
  55.         Intent intent2 = new Intent(context, NoticeSetting.class);  
  56.         intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  57.         context.startActivity(intent2);  
  58.         context.stopService(intent);// 是否可以停止  
  59.         return ""; // "这是一个可选的消息,警告有关禁止用户的请求";  
  60.     }  
  61.   
  62.     @Override  
  63.     public void onPasswordChanged(Context context, Intent intent) {  
  64.         showToast(context, "设备管理:密码己经改变");  
  65.     }  
  66.   
  67.     @Override  
  68.     public void onPasswordFailed(Context context, Intent intent) {  
  69.         showToast(context, "设备管理:改变密码失败");  
  70.     }  
  71.   
  72.     @Override  
  73.     public void onPasswordSucceeded(Context context, Intent intent) {  
  74.         showToast(context, "设备管理:改变密码成功");  
  75.     }  
  76.   
  77. }  

 核心代码断:

Java代码  收藏代码
  1. @Override  
  2.     public CharSequence onDisableRequested(Context context, Intent intent) {  
  3.         // 这里处理 不可编辑设备。  
  4.         Intent intent2 = new Intent(context, NoticeSetting.class);  
  5.         intent2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);  
  6.         context.startActivity(intent2);  
  7.         context.stopService(intent);// 是否可以停止  
  8.         return ""; // "这是一个可选的消息,警告有关禁止用户的请求";  
  9.     }  

 核心为 onDisableRequested方法可以做很作操作。正常情况下是取消勾选时,提示用户一句话。这里  context.stopService(intent);我把设置界面给关了。然后呢 跳转到我制定死机界面 NoticeSetting。NoticeSetting界面按键任何都不管用,只能重启。。

PS: 只是学习,不建议大家做流氓软件。本人自己也很抵触。学习之中,如果大家有问题欢迎留言,或者版本兼容问题。共同学习。 大家如果遇到这样的软件,可以用比较权威的软件卸载,如果还卸载不了,可以Root后,直接到app文件夹中删除apk文件。

原文地址:https://www.cnblogs.com/xiaochao1234/p/4121470.html