1,查看google文档Administration---->device polices---->清单文件的配置mainfest.xml 2,将mainfest.xml广播接受者子类创建出来DeviceAdmin继承至DeviceAdminReceiver 3,将清单文件中的错误进行修复(字符串创建,xml文件创建) 4,xml文件夹 <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> 5,开启激活界面的activity ComponentName mDeviceAdminSample = new ComponentName(context, DeviceAdmin.class); Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, mActivity.getString(R.string.add_admin_extra_app_text)); startActivity(intent); 前提:必须激活当前应用 6,一键锁屏 //获取设备的管理者对象 DevicePolicyManager mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); //锁屏 mDPM.lockNow(); 7,一键清除数据 mDPM.wipeData(); 8,一键卸载 在设备管理器中没有激活,可以卸载 在设备管理器中有做激活,不可以卸载,系统会提示取消在设备管理器中激活,然后才可以卸载 卸载?android系统自带功能,看源码,卸载界面 查看packageInstaller源码,找到uninstallActivity源码, 匹配对应的action,category,data(应用的包名)去卸载指定应用
public class MainActivity extends Activity { private ComponentName mDeviceAdminSample; private DevicePolicyManager mDPM; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button bt_start = (Button) findViewById(R.id.bt_start); Button bt_lock = (Button) findViewById(R.id.bt_lock); Button bt_wipedata = (Button) findViewById(R.id.bt_wipedata); Button bt_uninstall = (Button) findViewById(R.id.bt_uninstall); //(上下文环境,广播接受者对应的字节码文件) //组件对象可以作为是否激活的判断标志 mDeviceAdminSample = new ComponentName(this, DeviceAdmin.class); mDPM = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE); bt_start.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //开启设备管理器的activity Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mDeviceAdminSample); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION,"设备管理器"); startActivity(intent); } }); bt_lock.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //是否开启的判断 if(mDPM.isAdminActive(mDeviceAdminSample)){ //激活--->锁屏 mDPM.lockNow(); //锁屏同时去设置密码 mDPM.resetPassword("123", 0); }else{ Toast.makeText(getApplicationContext(), "请先激活", 0).show(); } } }); bt_wipedata.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if(mDPM.isAdminActive(mDeviceAdminSample)){ mDPM.wipeData(0);//手机数据 // mDPM.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE);//手机sd卡数据 }else{ Toast.makeText(getApplicationContext(), "请先激活", 0).show(); } } }); bt_uninstall.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("android.intent.action.DELETE"); intent.addCategory("android.intent.category.DEFAULT"); intent.setData(Uri.parse("package:"+getPackageName())); startActivity(intent); } }); } }
package com.example.device; import android.app.admin.DeviceAdminReceiver; import android.content.Context; import android.content.Intent; public class DeviceAdmin extends DeviceAdminReceiver { @Override public void onReceive(Context context, Intent intent) { super.onReceive(context, intent); } }