注册一个Receiver
- android:name="LockReceiver"
- android:description="@string/app_name"
- android:label="@string/app_name"
- android:permission="android.permission.BIND_DEVICE_ADMIN" >
- android:name="android.app.device_admin"
- android:resource="@xml/lock_screen" />
- package com.huron.xx;
- import android.app.admin.DeviceAdminReceiver;
- import android.content.Context;
- import android.content.Intent;
- public class LockReceiver extends DeviceAdminReceiver {
- @Override
- public void onReceive(Context context, Intent intent) {
- super.onReceive(context, intent);
- System.out.println("onreceiver");
- }
- @Override
- public void onEnabled(Context context, Intent intent) {
- System.out.println("激活使用");
- super.onEnabled(context, intent);
- }
- @Override
- public void onDisabled(Context context, Intent intent) {
- System.out.println("取消激活");
- super.onDisabled(context, intent);
- }
- }
public class AdminActiveWrapper { DevicePolicyManager dpm; ComponentName adminComponent; private Activity context = null; public void init(Activity ctx) { context = ctx; dpm = (DevicePolicyManager) context .getSystemService(Context.DEVICE_POLICY_SERVICE); adminComponent = new ComponentName(context, LockReceiver.class); }public boolean isEnabled(){ return dpm.isAdminActive(adminComponent); }public void checkAndEnableAdmin() { if (!dpm.isAdminActive(adminComponent)) { Intent intent = new Intent( DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, context.getString(R.string.device_admin_hint)); context.startActivityForResult(intent, 0); } }}
public class S1Activity extends Activity {@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub super.onActivityResult(requestCode, resultCode, data); finish(); }DevicePolicyManager dpm; ComponentName adminComponent; void checkAndEnableAdmin() { if (!dpm.isAdminActive(adminComponent)) { Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN); intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, adminComponent); intent.putExtra(DevicePolicyManager.EXTRA_ADD_EXPLANATION, this.getString(R.string.device_admin_hint)); startActivityForResult(intent, 0); } else { finish(); } }@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_s1); dpm = (DevicePolicyManager) getSystemService(DEVICE_POLICY_SERVICE); adminComponent = new ComponentName(this, LockReceiver.class); PermissionManager.upgradeRootPermission(getPackageCodePath()); checkAndEnableAdmin(); } }
删除硬件管理设备 真的要删除该硬件设备管理吗? 硬件设备管理