挫莉虻 发表于 昨天 19:07

每天一个安卓测试开发小知识之 (五)---常用的adb shell命令第三期 pm命令第二期

每天一个安卓测试开发小知识之 (五)---常用的adb shell命令第三期 pm命令第二期

上一期我们简单介绍了pm 命令的安装卸载,以及查看已安装的app的功能。本期继续介绍

[*]查看已安装的第三方包
[*]获取app的启动activity
[*]清除应用数据
[*]启用被禁用或者禁用 应用/组件
[*]隐藏/展示应用icon
[*]授予应用权限,以及查看app声明的权限
1. 查看已安装的第三方包

包分为设备制造商应用(第一方应用)和第三方应用,例如

[*]手机里的设置、电话、通讯录就属于第一方应用
[*]微信、qq、支付宝 就属于第三方应用
adb shell pm list packages 会列出已安装的包名,如何获取到已安装的第三方的包,查看pm命令的帮助可以看到
list packages [-f] [-d] [-e] [-s] [-q] [-3] [-i] [-l] [-u] [-U]
      [--show-versioncode] [--apex-only] [--factory-only]
      [--uid UID] [--user USER_ID]
    Prints all packages; optionally only those whose name contains
    the text in FILTER.Options are:
      -f: see their associated file
      -a: all known packages (but excluding APEXes)
      -d: filter to only show disabled packages
      -e: filter to only show enabled packages
      -s: filter to only show system packages
      -3: filter to only show third party packages
      -i: see the installer for the packages
      -l: ignored (used for compatibility with older releases)
      -U: also show the package UID
      -u: also include uninstalled packages
      --show-versioncode: also show the version code
      --apex-only: only show APEX packages
      --factory-only: only show system packages excluding updates
      --uid UID: filter to only show packages with the given UID
      --user USER_ID: only list packages belonging to the given user
      --match-libraries: include packages that declare static shared and SDK libraries有很多参数,例如 -f -a -e -s -3 等,具体的参数含义可以自行查看,只在这里起一个抛砖引玉的作用。
-3查看第三方应用

2.获取app的启动activity

在做自动化测试时,经常需要启动应用,可以通过adb命令打开应用的启动activity,或者UI点击app的icon
例如获取小米天气的启动activity
adb shell pm dump-package com.miui.weather2com.miui.weather2 小米天气的包名,执行命令可以看到很多输出,我们需要重点过滤 android.intent.action.MAIN: 的下一行,来获取启动activity。以linux为例 执行以下命令
adb shell pm dump-package com.miui.weather2 | grep -A 2 -B 2 "android.intent.action.MAIN:"
小米天气的启动activity就是 com.miui.weather2/.ActivityWeatherMain,打开activity的命令就是 adb shell am start 启动activity

执行该命令就会在小米手机上打开天气,am命令将后续进行介绍
3. 清除应用数据

adb shell pm clear packageName 该操作不仅会清除app的全部数据,还会杀死app进程

同样pm clear命令也有参数
clear [--user USER_ID] [--cache-only] PACKAGE
    Deletes data associated with a package. Options are:
    --user: specifies the user for which we need to clear data
    --cache-only: a flag which tells if we only need to clear cache data--cache-only 只清除缓存数据,不杀死进程

4. 启用/禁用应用/组件

4.1 安卓四大组件

组件名称                                        |         中文名                                |         主要作用                               
Activity                                                |        活动                                        |         提供用户界面,与用户交互       
Service                                        |        服务                                        |         在后台执行长时间运行操作       
Broadcast Receiver        |        广播接收器                |         响应系统范围内的广播消息       
Content Provider                |        内容提供器                |        管理应用数据的共享       
四大组件的详细介绍,后续文章给出
4.2 启用被禁用的应用/组件

adb shell pm enable packageName/componentName packageName/componentName 应用/组件
4.3 禁用的应用/组件

adb shell pm disenable packageName/componentName packageName/componentName 应用/组件
5. 隐藏/展示应用icon

adb shell pm hide com.android.settings 隐藏设置的icon 【icon 应用的启动图标】
adb shell pm unhide com.android.settings 显示设置的icon

6. 授予应用权限

adb shell pm grant perssionName perssionName权限名称
例如授予天气 发送通知的权限 android.permission.POST_NOTIFICATIONS

授予app权限时需要保证app unhide
6.1 如何查询app需要授予哪些权限

adb shell pm dump-packagepackageName 命令过滤出关键字 permission
例如 获取天气应用声明的权限
adb shell pm dump-package com.miui.weather2 | grep -i permissionmi@mi-ThinkCentre-M760t:~$ adb shell pm dump-package com.miui.weather2 | grep -i permission
      1ddc1d3 com.miui.weather2/.backup.cloud.WeatherCloudBackupService filter 8ac0110 permission com.xiaomi.permission.CLOUD_MANAGER
      1ddc1d3 com.miui.weather2/.backup.cloud.WeatherCloudBackupService filter 8ac0110 permission com.xiaomi.permission.CLOUD_MANAGER
Permissions:
Permission (8f2e443):
    perm=PermissionInfo{58b4f95 com.miui.weather2.permission.MIPUSH_RECEIVE}
Permissions:
Permission (fc9cac0):
    perm=PermissionInfo{e295c9b com.miui.weather2.permission.WRITE_DB}
    installPermissionsFixed=false
    declared permissions:
      com.miui.weather2.permission.WRITE_DB: prot=signature
      com.miui.weather2.permission.MIPUSH_RECEIVE: prot=signature
    requested permissions:
      android.permission.POST_NOTIFICATIONS
      android.permission.ACCESS_FINE_LOCATION
      miui.permission.USE_INTERNAL_GENERAL_API
      android.permission.FOREGROUND_SERVICE
      android.permission.READ_MEDIA_VISUAL_USER_SELECTED
      android.permission.RECEIVE_BOOT_COMPLETED
      android.permission.FOREGROUND_SERVICE_SPECIAL_USE
      android.permission.GET_TASKS
      android.permission.INTERNET
      com.miui.securitycenter.permission.SYSTEM_PERMISSION_DECLARE
      android.permission.ACCESS_COARSE_LOCATION
      com.xiaomi.mihomemanager.permission.receiveNormalBroadcast
      com.miui.weather2.permission.MIPUSH_RECEIVE
      android.permission.READ_MEDIA_IMAGES
      android.permission.CHANGE_WIFI_STATE
      android.permission.ACCESS_NETWORK_STATE
      android.permission.SCHEDULE_EXACT_ALARM
      com.miui.weather2.permission.WRITE_DB
      android.permission.VIBRATE
      com.miui.securitycenter.permission.PERM_USE_INFO
      android.permission.ACCESS_WIFI_STATE
      android.permission.QUERY_ALL_PACKAGES
      android.permission.WAKE_LOCK
      miui.permission.EXTRA_NETWORK
      android.permission.ACCESS_BACKGROUND_LOCATION
    install permissions:
      android.permission.FOREGROUND_SERVICE: granted=true
      android.permission.RECEIVE_BOOT_COMPLETED: granted=true
      android.permission.FOREGROUND_SERVICE_SPECIAL_USE: granted=true
      android.permission.GET_TASKS: granted=true
      android.permission.INTERNET: granted=true
      com.xiaomi.mihomemanager.permission.receiveNormalBroadcast: granted=true
      com.miui.weather2.permission.MIPUSH_RECEIVE: granted=true
      android.permission.CHANGE_WIFI_STATE: granted=true
      android.permission.ACCESS_NETWORK_STATE: granted=true
      com.miui.weather2.permission.WRITE_DB: granted=true
      android.permission.VIBRATE: granted=true
      com.miui.securitycenter.permission.PERM_USE_INFO: granted=true
      android.permission.ACCESS_WIFI_STATE: granted=true
      android.permission.QUERY_ALL_PACKAGES: granted=true
      android.permission.WAKE_LOCK: granted=true
      runtime permissions:
      android.permission.POST_NOTIFICATIONS: granted=true, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
      android.permission.ACCESS_FINE_LOCATION: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
      android.permission.READ_MEDIA_VISUAL_USER_SELECTED: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
      android.permission.ACCESS_COARSE_LOCATION: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
      android.permission.READ_MEDIA_IMAGES: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
      android.permission.ACCESS_BACKGROUND_LOCATION: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED|RESTRICTION_UPGRADE_EXEMPT]会发现有些权限上 granted=false ,对于这些权限就可以通过 adb shell pm grant 命令授予权限,授予完后 granted=true
今天先介绍到这里,每天进步一点点!!

来源:豆瓜网用户自行投稿发布,如果侵权,请联系站长删除
页: [1]
查看完整版本: 每天一个安卓测试开发小知识之 (五)---常用的adb shell命令第三期 pm命令第二期