找回密码
 立即注册
首页 业界区 业界 每天一个安卓测试开发小知识之 (五)---常用的adb shel ...

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

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

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

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

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

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

2.  获取app的启动activity

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

小米天气的启动activity就是 com.miui.weather2/.ActivityWeatherMain,打开activity的命令就是 adb shell am start 启动activity
3.png

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

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

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

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.png

6. 授予应用权限

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

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

adb shell pm dump-package  packageName 命令过滤出关键字 permission
例如 获取天气应用声明的权限
  1. adb shell pm dump-package com.miui.weather2 | grep -i permission
复制代码
  1. mi@mi-ThinkCentre-M760t:~$ adb shell pm dump-package com.miui.weather2 | grep -i permission
  2.         1ddc1d3 com.miui.weather2/.backup.cloud.WeatherCloudBackupService filter 8ac0110 permission com.xiaomi.permission.CLOUD_MANAGER
  3.         1ddc1d3 com.miui.weather2/.backup.cloud.WeatherCloudBackupService filter 8ac0110 permission com.xiaomi.permission.CLOUD_MANAGER
  4. Permissions:
  5.   Permission [com.miui.weather2.permission.MIPUSH_RECEIVE] (8f2e443):
  6.     perm=PermissionInfo{58b4f95 com.miui.weather2.permission.MIPUSH_RECEIVE}
  7. Permissions:
  8.   Permission [com.miui.weather2.permission.WRITE_DB] (fc9cac0):
  9.     perm=PermissionInfo{e295c9b com.miui.weather2.permission.WRITE_DB}
  10.     installPermissionsFixed=false
  11.     declared permissions:
  12.       com.miui.weather2.permission.WRITE_DB: prot=signature
  13.       com.miui.weather2.permission.MIPUSH_RECEIVE: prot=signature
  14.     requested permissions:
  15.       android.permission.POST_NOTIFICATIONS
  16.       android.permission.ACCESS_FINE_LOCATION
  17.       miui.permission.USE_INTERNAL_GENERAL_API
  18.       android.permission.FOREGROUND_SERVICE
  19.       android.permission.READ_MEDIA_VISUAL_USER_SELECTED
  20.       android.permission.RECEIVE_BOOT_COMPLETED
  21.       android.permission.FOREGROUND_SERVICE_SPECIAL_USE
  22.       android.permission.GET_TASKS
  23.       android.permission.INTERNET
  24.       com.miui.securitycenter.permission.SYSTEM_PERMISSION_DECLARE
  25.       android.permission.ACCESS_COARSE_LOCATION
  26.       com.xiaomi.mihomemanager.permission.receiveNormalBroadcast
  27.       com.miui.weather2.permission.MIPUSH_RECEIVE
  28.       android.permission.READ_MEDIA_IMAGES
  29.       android.permission.CHANGE_WIFI_STATE
  30.       android.permission.ACCESS_NETWORK_STATE
  31.       android.permission.SCHEDULE_EXACT_ALARM
  32.       com.miui.weather2.permission.WRITE_DB
  33.       android.permission.VIBRATE
  34.       com.miui.securitycenter.permission.PERM_USE_INFO
  35.       android.permission.ACCESS_WIFI_STATE
  36.       android.permission.QUERY_ALL_PACKAGES
  37.       android.permission.WAKE_LOCK
  38.       miui.permission.EXTRA_NETWORK
  39.       android.permission.ACCESS_BACKGROUND_LOCATION
  40.     install permissions:
  41.       android.permission.FOREGROUND_SERVICE: granted=true
  42.       android.permission.RECEIVE_BOOT_COMPLETED: granted=true
  43.       android.permission.FOREGROUND_SERVICE_SPECIAL_USE: granted=true
  44.       android.permission.GET_TASKS: granted=true
  45.       android.permission.INTERNET: granted=true
  46.       com.xiaomi.mihomemanager.permission.receiveNormalBroadcast: granted=true
  47.       com.miui.weather2.permission.MIPUSH_RECEIVE: granted=true
  48.       android.permission.CHANGE_WIFI_STATE: granted=true
  49.       android.permission.ACCESS_NETWORK_STATE: granted=true
  50.       com.miui.weather2.permission.WRITE_DB: granted=true
  51.       android.permission.VIBRATE: granted=true
  52.       com.miui.securitycenter.permission.PERM_USE_INFO: granted=true
  53.       android.permission.ACCESS_WIFI_STATE: granted=true
  54.       android.permission.QUERY_ALL_PACKAGES: granted=true
  55.       android.permission.WAKE_LOCK: granted=true
  56.       runtime permissions:
  57.         android.permission.POST_NOTIFICATIONS: granted=true, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
  58.         android.permission.ACCESS_FINE_LOCATION: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
  59.         android.permission.READ_MEDIA_VISUAL_USER_SELECTED: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
  60.         android.permission.ACCESS_COARSE_LOCATION: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
  61.         android.permission.READ_MEDIA_IMAGES: granted=false, flags=[ USER_SENSITIVE_WHEN_GRANTED|USER_SENSITIVE_WHEN_DENIED]
  62.         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
今天先介绍到这里,每天进步一点点!!

来源:豆瓜网用户自行投稿发布,如果侵权,请联系站长删除

相关推荐

您需要登录后才可以回帖 登录 | 立即注册