Android DeviceOwner

 

1:DeviceAdminReceiver

首先注册deviceAdminReceiver广播接收者。

public class MyDeviceAdminReceiver extends DeviceAdminReceiver {
    public static final String TAG="MyDeviceAdminReceiver";

}

可以空实现。

接着manifest中添加:

<receiver android:name=".utils.MyDeviceAdminReceiver"
            android:permission="android.permission.BIND_DEVICE_ADMIN"
            >
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/device_admin" />
            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED"/>
            </intent-filter>
        </receiver>

然后我们需要添加对应的device_admin.xml

<device-admin>
    <uses-policies>
        <limit-password/>
        <watch-login/>
        <reset-password/>
        <force-lock/>
        <wipe-data/>
        <expire-password/>
        <encrypted-storage/>
        <disable-camera/>
        <set-global-proxy/>
        <disable-keyguard-features/>
    </uses-policies>
</device-admin>

内部包含的就是对应的可操作的权限、

2:DevicePolicyManager

DevicePolicyManager就是我们可以操作的对象,利用其内部的api我们可以进行如:禁用相机,隐藏应用等操作。

首先我们需要获取到devicePolicyManager对象:

 devicePolicyManager = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
 componentName = new ComponentName(this, MyDeviceAdminReceiver.class);

2.1:获取设备管理器激活状态

  public boolean getAdminActiveStatus() {
        return devicePolicyManager.isAdminActive(componentName);
    }

2.2:激活设备管理器

 if (!devicePolicyManager.isAdminActive(componentName)){
            //激活
            Intent intent = new Intent(DevicePolicyManager.ACTION_ADD_DEVICE_ADMIN);
            intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, componentName);
            startActivity(intent);
}

2.3:重启

重启需要设置deviceOwner,可以使用adb命令来实现:

adb shell dpm set-device-owner {pkg}/{pkg}.xxx.DeviceOwnerReceiver
 if (getAdminActiveStatus()) {
                    devicePolicyManager.reboot(componentName);
}

2.4:锁屏 

锁屏不需要设置deviceOwner,只用激活设备管理器就可以了。

  //激活后就可以了
 if (getAdminActiveStatus()) {
       devicePolicyManager.lockNow();
}

2.5:获取设备管理器列表

if (isDeviceOwner) {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        List<ComponentName> activeAdmins = devicePolicyManager.getActiveAdmins();
                        if (activeAdmins != null) {
                            StringBuilder stringBuilder = new StringBuilder();
                            for (int i = 0; i < activeAdmins.size(); i++) {
                                stringBuilder.append("PackageName:" + activeAdmins.get(i).getPackageName() + "\n"
                                        + "ClassName:" + activeAdmins.get(i).getClassName() + "\n");
                            }
                            tvAdminList.setText(stringBuilder.toString());
                        }
                    }

                }

2.6:隐藏显示应用

  if (isDeviceOwner) {
                    devicePolicyManager.setApplicationHidden(componentName, AppConfig.TAOBAO, isHide);
                    Toast.makeText(this, isHide ? "隐藏淘宝" : "显示淘宝", Toast.LENGTH_LONG).show();
                    isHide = !isHide;
                }

首先需要deviceOwner权限。 另外参数二指代应用包名

2.7:禁用相机

   if (isDeviceOwner) {
                    isForbidCamera = !isForbidCamera;
                    devicePolicyManager.setCameraDisabled(componentName, isForbidCamera);
                    Toast.makeText(this, isForbidCamera ? "禁用相机" : "启用相机", Toast.LENGTH_LONG).show();
                }

获取到deviceOwner后可以设置禁用/使用相机。

2.8:获取wifiMac地址

 if (isDeviceOwner) {
                    String wifiMac = devicePolicyManager.getWifiMacAddress(componentName) == null ? "" : devicePolicyManager.getWifiMacAddress(componentName);
                    tvWifiMac.setText(wifiMac);
                }

2.9:禁用导航栏

  /**
     * @param admin
     * @param disabled
     * @return
     * 禁用启用顶部导航栏
     */
    @RequiresApi(api = Build.VERSION_CODES.M)
    private boolean setStatusBarDisabled(ComponentName admin, boolean disabled) {
        boolean res = false;
        if (isDeviceOwner) {
            res = devicePolicyManager.setStatusBarDisabled(admin, disabled);
        }

        Log.e(TAG, "setStatusBarDisabled" + res);
        return res;
    }

2.10:设置应用挂起

应用挂起,也就是禁用应用:

  /**
     * @param admin
     * @param packageNames
     * @param suspended
     * @return 设置应用挂起
     */
    @RequiresApi(api = Build.VERSION_CODES.N)
    private String[] setPackagesSuspended(ComponentName admin, String[] packageNames, boolean suspended) {
        String[] res = null;

        if (isDeviceOwner) {
            res = devicePolicyManager.setPackagesSuspended(admin, packageNames, suspended);
        }
        return res;
    }

2.11:禁止卸载

devicePolicyManager.setUninstallBlocked(componentName,AppConfig.TAOBAO,true);

这里禁止卸载,跟我们激活设备管理器后的效果是不一样的,而是在我们长按应用图标时弹出提示,无法卸载。

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值