1. 程式人生 > >android 手機應用裝置管理啟用狀態,獲取啟用狀態,監聽啟用狀態

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.    <intent-filter>
  10.        <actionandroid:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
  11.    </intent-filter>
  12. </receiver>
配置許可權檔案lock_screen.xml,將檔案放置在/res/xml/下
  1. <?xmlversion="1.0"encoding="UTF-8"?>
  2. <device-adminxmlns:android="http://schemas.android.com/apk/res/android">
  3.     <uses-policies
    >
  4.         <!-- 強行鎖定(這裡只需要這個) -->
  5.         <force-lock/>
  6.         <!-- 清除所有資料(恢復出廠設定) -->
  7.         <wipe-data/>
  8.         <!-- 重置密碼 -->
  9.         <reset-password/>
  10.         <!-- 限制密碼選擇 -->
  11.         <limit-password/>
  12.         <!-- 監控登入嘗試 -->
  13.         <watch-login/>
  14.     </uses-policies>
  15. </device-admin>
實現Receiver
  1. package com.huron.xx;  
  2. import android.app.admin.DeviceAdminReceiver;  
  3. import android.content.Context;  
  4. import android.content.Intent;  
  5. publicclass LockReceiver extends DeviceAdminReceiver {  
  6.     @Override
  7.     publicvoid onReceive(Context context, Intent intent) {  
  8.         super.onReceive(context, intent);  
  9.         System.out.println("onreceiver");  
  10.     }  
  11.     @Override
  12.     publicvoid onEnabled(Context context, Intent intent) {  
  13.         System.out.println("啟用使用");  
  14.         super.onEnabled(context, intent);  
  15.     }  
  16.     @Override
  17.     publicvoid onDisabled(Context context, Intent intent) {  
  18.         System.out.println("取消啟用");  
  19.         super.onDisabled(context, intent);  
  20.     }  

獲取啟用狀態
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>