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

       

注册一个Receiver
  1. <receiver   
  2.    android:name="LockReceiver"   
  3.    android:description="@string/app_name"   
  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/lock_screen" />   
  9.      
  10.    <intent-filter>   
  11.        <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />   
  12.    intent-filter>   
  13. receiver>
配置权限文件lock_screen.xml,将文件放置在/res/xml/下
  1. xml version="1.0" encoding="UTF-8"?>  
  2. <device-admin xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.   
  4.     <uses-policies>  
  5.   
  6.           
  7.         <force-lock />  
  8.   
  9.           
  10.         <wipe-data />  
  11.   
  12.           
  13.         <reset-password />  
  14.   
  15.           
  16.         <limit-password />  
  17.   
  18.           
  19.         <watch-login />  
  20.               
  21.     uses-policies>  
  22.   
  23. device-admin> 
实现Receiver
  1. package com.huron.xx;  
  2.   
  3. import android.app.admin.DeviceAdminReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6.   
  7. public class LockReceiver extends DeviceAdminReceiver {  
  8.   
  9.     @Override  
  10.     public void onReceive(Context context, Intent intent) {  
  11.         super.onReceive(context, intent);  
  12.         System.out.println("onreceiver");  
  13.     }  
  14.   
  15.     @Override  
  16.     public void onEnabled(Context context, Intent intent) {  
  17.         System.out.println("激活使用");  
  18.         super.onEnabled(context, intent);  
  19.     }  
  20.   
  21.     @Override  
  22.     public void onDisabled(Context context, Intent intent) {  
  23.         System.out.println("取消激活");  
  24.         super.onDisabled(context, intent);  
  25.     }  
  26.   

获取激活状态
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();
   }
}

<string name="device_admin_hint">删除硬件管理设备string>
<string name="device_admin_cancel">真的要删除该硬件设备管理吗?string>
<string name="device_admin_title">硬件设备管理string>



你可能感兴趣的:(android,软件)