前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >让Android自动化辅助APP成为设备所有者(一)

让Android自动化辅助APP成为设备所有者(一)

作者头像
岛哥的质量效能笔记
发布2021-08-18 15:09:50
1.7K0
发布2021-08-18 15:09:50
举报
文章被收录于专栏:岛哥的质量效能笔记

阅读本文大约需要1.1分钟。

背景

我们之所以需要将Android自动化测试的辅助APP设置成设备所有者是为了更好的控制系统的一些行为从而让整个测试过程更稳定。

DeviceOwner简介

DeviceOwner 是指在设备上以管理员身份运行的应用程序,该应用程序可以使用 DevicePolicyManager 类中的 API 来控制设备的一些行为,例如:重启设备、设置锁屏方式、设置密码、强制清除密码、设置状态栏、设置系统更新策略等。

Android 提供了三种设备管理方案:DeviceAdmin(设备管理员)、ProfileOwner(配置文件所有者) 和 DeviceOwner(设备所有者),这三种设备管理方案的权限大小分别为:DeviceAdmin < ProfileOwner < DeviceOwner。应用需要最大的授权才能成为DeviceOwner,DeviceOwner具有设备的最高权限。

创建DeviceOwner

基本配置

首先在res/xml目录下新建device_admin.xml文件,如下:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<device-admin xmlns:android="http://schemas.android.com/apk/res/android" >
    <uses-policies>
        <!-- 设置密码规则 -->
        <limit-password />
        <!-- 监视屏幕解锁尝试次数 -->
        <watch-login />
        <!-- 更改解锁密码 -->
        <reset-password />
        <!-- 锁定屏幕 -->
        <force-lock />
        <!-- 清除数据,恢复出厂模式,在不发出警告的情况下 -->
        <wipe-data />
        <!-- 锁屏密码有效期 -->
        <expire-password />
        <!-- 对存储的应用数据加密 -->
        <encrypted-storage />
        <!-- 禁用锁屏信息 -->
        <disable-keyguard-features/>
     <!-- 禁用摄像头 -->
        <disable-camera />
    </uses-policies>
</device-admin>

注册一个自定义广播接收器继承自DeviceAdminReceiver

代码如下:

代码语言:javascript
复制
package com.android.jarvis.receivers

import android.app.admin.DeviceAdminReceiver
import android.app.admin.DevicePolicyManager
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.os.Build
import android.util.Log
import androidx.annotation.RequiresApi

/**
 * adb shell dpm set-device-owner com.android.jarvis/.receivers.JarvisAdminReceiver
 */
class JarvisAdminReceiver : DeviceAdminReceiver() {

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onEnabled(context: Context, intent: Intent) {
        Log.d("JarvisAdminReceiver", "onEnabled")
        val devicePolicyManager =
            context.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
        //设置应用不可卸载
        devicePolicyManager.setUninstallBlocked(
            getComponentName(context),
            context.packageName,
            true
        )
        super.onEnabled(context, intent)
    }

    /**
     * 获取ComponentName,DevicePolicyManager的大多数方法都会用到
     */
    private fun getComponentName(context: Context): ComponentName {
        return ComponentName(
            context.applicationContext,
            JarvisAdminReceiver::class.java
        )
    }
}

在AndroidManifest.xml中注册广播

代码如下:

代码语言:javascript
复制
        <receiver
            android:name=".receivers.JarvisAdminReceiver"
            android:permission="android.permission.BIND_DEVICE_ADMIN">
            <meta-data
                android:name="android.app.device_admin"
                android:resource="@xml/device_policies" />

            <intent-filter>
                <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
                <action android:name="android.app.action.DEVICE_ADMIN_DISABLE_REQUESTED" />
                <action android:name="android.app.action.DEVICE_ADMIN_DISABLED" />
            </intent-filter>
        </receiver>

激活DeviceOwner

先安装应用,然后在命令行中执行:

代码语言:javascript
复制
adb shell dpm set-device-owner com.android.jarvis/.receivers.JarvisAdminReceiver

移除DeviceOwner

当一个APP成为DeviceOwner后,这个APP是不能被卸载的,也无法在设置中关闭其权限,要想卸载这个APP就必须移除DeviceOwner权限,首先需要在AndroidManifest.xml文件中的<application/>节点添加android:testOnly="true",然后可以通过如下命令移除:

代码语言:javascript
复制
adb shell dpm remove-active-admin com.android.jarvis/.receivers.JarvisAdminReceiver

但是在有些机型上即使设置了testOnly=true也是无法移除,会报以下错误:

代码语言:javascript
复制
java.lang.SecurityException: Attempt to remove non-test admin ComponentInfo{....AppAdminReceiver} 0

这个时候就需要通过代码的方式来移除了,我们可以新建一个广播接收器:

代码语言:javascript
复制
package com.android.jarvis.receivers

import android.app.admin.DevicePolicyManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.annotation.RequiresApi

/**
 *adb shell am broadcast -a com.android.jarvis.action.DEVICE_ADMIN_DISABLED
 */
class JarvisDeviceReceiver(
    private val ACTION_DEVICE_ADMIN_DISABLED: String = "com.android.jarvis.action.DEVICE_ADMIN_DISABLED"
) :
    BroadcastReceiver() {

    @RequiresApi(Build.VERSION_CODES.LOLLIPOP)
    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action
        if (action == ACTION_DEVICE_ADMIN_DISABLED) {
            val devicePolicyManager =
                context.getSystemService(Context.DEVICE_POLICY_SERVICE) as DevicePolicyManager
            devicePolicyManager.clearDeviceOwnerApp(context.packageName)
        }
    }
}

然后在AndroidManifest.xml文件中注册:

代码语言:javascript
复制
        <receiver android:name=".receivers.JarvisDeviceReceiver">
            <intent-filter>
                <action android:name="com.android.jarvis.action.DEVICE_ADMIN_DISABLED" />
            </intent-filter>
        </receiver>

最后在命令行中执行:

代码语言:javascript
复制
adb shell am broadcast -a com.android.jarvis.action.DEVICE_ADMIN_DISABLED

这样就可以成功移除DeviceOwner权限了。

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-06-23,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 岛哥的质量效能笔记 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档