参考:http://www.cnblogs.com/androidez/archive/2013/02/17/2915020.html
1.创建AdminReceiver,继承DeviceAdminReceiver
1 import android.app.admin.DeviceAdminReceiver; 2 /* 3 * 设备管理器,可以设置锁屏和清除数据。请查看API指南中的Administration 4 */ 5 public class AdminReceiver extends DeviceAdminReceiver { 6 }
2.清单文件中配置:
1 <receiver 2 android:name=".receiver.AdminReceiver" 3 android:description="@string/device_admin_description" 4 android:label="@string/device_admin" 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 <intent-filter> 10 <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/> 11 intent-filter> 12 receiver>
3.在res/xml文件夹下创建device_admin.xml文件
1 <device-admin xmlns:android="http://schemas.android.com/apk/res/android"> 2 <uses-policies> 3 <limit-password /> //设置密码规则 4 <watch-login /> //监控屏幕解锁尝试次数 5 <reset-password /> //更改屏幕解锁密码 6 <force-lock /> //设备自动解锁 7 <wipe-data /> //清除数据 8 <expire-password /> //激活密码 9 <encrypted-storage /> //数据加密 10 <disable-camera /> //禁用摄像头 11 uses-policies> 12 device-admin>
4.在Activity中让用户激活
1 public class Setup4Activity extends BaseSetupActivity { 2 public static final int DPM_REQUEST_CODE = 201603; 3 private CheckBox cbProtect; 4 private ComponentName componentName; 5 private DevicePolicyManager mDpm; 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_setup4); 11 componentName = new ComponentName(Setup4Activity.this, 12 AdminReceiver.class); 13 mDpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE); 14 cbProtect = (CheckBox) findViewById(R.id.cb_protect); 15 boolean protect = mPref.getBoolean("protect", false); 16 if (protect) { 17 cbProtect.setText("防盗保护已经开启"); 18 cbProtect.setChecked(true); 19 } else { 20 cbProtect.setText("防盗保护没有开启"); 21 cbProtect.setChecked(false); 22 } 23 cbProtect.setOnCheckedChangeListener(new OnCheckedChangeListener() { 24 25 26 @Override 27 public void onCheckedChanged(CompoundButton buttonView, 28 boolean isChecked) { 29 30 if (isChecked) { 31 if (mDpm.isAdminActive(componentName)) { 32 cbProtect.setText("防盗保护已经开启"); 33 mPref.edit().putBoolean("protect", true).commit(); 34 } else { 35 //激活设备管理器 36 Intent intent = new Intent( 37 DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); 38 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, 39 componentName); 40 intent.putExtra( 41 DevicePolicyManager.EXTRA_ADD_EXPLANATION, 42 "提示文字"); 43 startActivityForResult(intent, DPM_REQUEST_CODE); 44 45 } 46 47 } else { 48 mDpm.removeActiveAdmin(componentName); 49 cbProtect.setText("防盗保护没有开启"); 50 mPref.edit().putBoolean("protect", false).commit(); 51 } 52 } 53 }); 54 } 55 56 @Override 57 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 58 super.onActivityResult(requestCode, resultCode, data); 59 if ( requestCode == DPM_REQUEST_CODE ) { 60 if (resultCode == RESULT_OK) { 61 cbProtect.setText("防盗保护已经开启"); 62 mPref.edit().putBoolean("protect", true).commit(); 63 } else { 64 cbProtect.setText("防盗保护没有开启"); 65 cbProtect.setChecked(false); 66 mPref.edit().putBoolean("protect", false).commit(); 67 } 68 } 69 } 70 }
5.通过监听短信,执行重置数据,修改密码等功能
1 public class SmsMangerReceiver extends BroadcastReceiver { 2 private DevicePolicyManager mDPM; 3 private SharedPreferences mPref; 4 5 @Override 6 public void onReceive(Context context, Intent intent) { 7 mDPM = (DevicePolicyManager) context 8 .getSystemService(Context.DEVICE_POLICY_SERVICE); 9 mPref = context.getSharedPreferences("config", 10 Context.MODE_PRIVATE); 11 boolean protect = mPref.getBoolean("protect", false); 12 //开启了保护以后,才会激活短信命令。 13 if ( protect ) { 14 Object[] object = (Object[]) intent.getExtras().get("pdus"); 15 for ( Object ob : object ) { 16 SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) ob, 17 "3gpp"); 18 String originatingAddress = smsMessage.getOriginatingAddress(); 19 String messageBody = smsMessage.getMessageBody(); 20 if ( messageBody.equals("#*alarm*#") ) { 21 // 使用MediaPlayer播放音乐 22 MediaPlayer player = MediaPlayer 23 .create(context, R.raw.ylzs); 24 player.setVolume(1.0f, 1.0f); 25 player.setLooping(true); 26 player.start(); 27 abortBroadcast(); 28 } else if ( messageBody.equals("#*location*#") ) { 29 // 通过service获取到经纬度 30 context.startService(new Intent(context, 31 LocationService.class)); 32 abortBroadcast(); 33 } else if ( messageBody.equals("#*wipedata*#") ) { 34 //清除数据 35 mDPM.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE); 36 abortBroadcast(); 37 38 } else if ( messageBody.equals("#*lockscreen*#") ) { 39 //锁屏并设置密码 40 mDPM.lockNow(); 41 mDPM.resetPassword("123456", 0); 42 abortBroadcast(); 43 } 44 } 45 } 46 } 47 }