android:name="android.app.device_admin" android:resource="@。android|android 手机应用设备管理激活状态,获取激活状态,监听激活状态。" />

android|android 手机应用设备管理激活状态,获取激活状态,监听激活状态



注册一个Receiver

  1. android:name="LockReceiver"
  2. android:description="@string/app_name"
  3. android:label="@string/app_name"
  4. android:permission="android.permission.BIND_DEVICE_ADMIN" >
  5. android:name="android.app.device_admin"
  6. android:resource="@xml/lock_screen" />

配置权限文件lock_screen.xml,将文件放置在/res/xml/下

实现Receiver
  1. package com.huron.xx;
  2. import android.app.admin.DeviceAdminReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. public class LockReceiver extends DeviceAdminReceiver {
  6. @Override
  7. public void onReceive(Context context, Intent intent) {
  8. super.onReceive(context, intent);
  9. System.out.println("onreceiver");
  10. }
  11. @Override
  12. public void onEnabled(Context context, Intent intent) {
  13. System.out.println("激活使用");
  14. super.onEnabled(context, intent);
  15. }
  16. @Override
  17. public void onDisabled(Context context, Intent intent) {
  18. System.out.println("取消激活");
  19. super.onDisabled(context, intent);
  20. }
  21. }
获取激活状态
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(); } }


删除硬件管理设备 真的要删除该硬件设备管理吗? 硬件设备管理




    推荐阅读