android 手機應用裝置管理啟用狀態,獲取啟用狀態,監聽啟用狀態
阿新 • • 發佈:2019-02-03
註冊一個Receiver
- <receiver
- android:name="LockReceiver"
- android:description="@string/app_name"
- android:label="@string/app_name"
- android:permission="android.permission.BIND_DEVICE_ADMIN">
- <meta-data
- android:name="android.app.device_admin"
- android:resource="@xml/lock_screen"/>
- <intent-filter>
- <actionandroid:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
- </intent-filter>
- </receiver>
- <?xmlversion="1.0"encoding="UTF-8"?>
- <device-adminxmlns:android="http://schemas.android.com/apk/res/android">
- <uses-policies
- <!-- 強行鎖定(這裡只需要這個) -->
- <force-lock/>
- <!-- 清除所有資料(恢復出廠設定) -->
- <wipe-data/>
- <!-- 重置密碼 -->
- <reset-password/>
- <!-- 限制密碼選擇 -->
- <limit-password/>
- <!-- 監控登入嘗試 -->
- <watch-login/>
- </uses-policies>
- </device-admin>
- package com.huron.xx;
- import android.app.admin.DeviceAdminReceiver;
- import android.content.Context;
- import android.content.Intent;
- publicclass LockReceiver extends DeviceAdminReceiver {
- @Override
- publicvoid onReceive(Context context, Intent intent) {
- super.onReceive(context, intent);
- System.out.println("onreceiver");
- }
- @Override
- publicvoid onEnabled(Context context, Intent intent) {
- System.out.println("啟用使用");
- super.onEnabled(context, intent);
- }
- @Override
- publicvoid 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(); } }
<string name="device_admin_hint">刪除硬體管理裝置</string> <string name="device_admin_cancel">真的要刪除該硬體裝置管理嗎?</string> <string name="device_admin_title">硬體裝置管理</string>