adb和四大组件的恩怨情仇

2,168 阅读2分钟

Application

安装应用

adb install -r -t <apk文件路径> //-r:重新安装现有应用,并保留其数据;-t:允许安装测试 APK。

//等同于 adb shell pm install ... 具体规则查看 https://developer.android.com/studio/command-line/adb#pm

卸载应用

adb uninstall -k <pkg name> //-k:移除软件包后保留数据和缓存目录。

启动应用

adb shell am start <pkg name>

强制停止应用

adb shell am force-stop <pkg name>

Activity

启动activity

adb shell am start -n <pkg>/<Path from source root> //为什么要-n,https://developer.android.com/studio/command-line/adb#IntentSpec

想要带参数?

adb shell am start -n <pkg>/<Path from source root> --es title 标题 --ei type 1

具体规则同样是看intent参数的规范

查看顶层activity

adb shell dumpsys activity | grep "mResumedActivity"

adb shell dumpsys window windows | grep -E 'mCurrentFocus|mFocusedApp'

Service

启动service

adb shell am startservice -n <pkg>/<Path from source root>

查看service是否存在

adb shell dumpsys activity services

adb shell dumpsys activity services | grep <service name>

Broadcast

查看系统发送了什么广播

adb shell dumpsys activity broadcasts

发送广播

adb shell am broadcast -a <action>

//带参数?再重申一遍https://developer.android.com/studio/command-line/adb#IntentSpec

ContentProvider

没有在android开发者文档中找到adb shell content的用法,只能从adb中查看用法

usage: adb shell content [subcommand] [options]

#增
usage: adb shell content insert --uri <URI> [--user <USER_ID>] --bind <BINDING> [--bind <BINDING>...]
  <URI> a content provider URI.
  <BINDING> binds a typed value to a column and is formatted:
  <COLUMN_NAME>:<TYPE>:<COLUMN_VALUE> where:
  <TYPE> specifies data type such as:
  b - boolean, s - string, i - integer, l - long, f - float, d - double, n - null
  Note: Omit the value for passing an empty string, e.g column:s:
  Example:
  # Add "new_setting" secure setting with value "new_value".
  adb shell content insert --uri content://settings/secure --bind name:s:new_setting --bind value:s:new_value

#改
usage: adb shell content update --uri <URI> [--user <USER_ID>] [--where <WHERE>]
  <WHERE> is a SQL style where clause in quotes (You have to escape single quotes - see example below).
  Example:
  # Change "new_setting" secure setting to "newer_value".
  adb shell content update --uri content://settings/secure --bind value:s:newer_value --where "name='new_setting'"

#删
usage: adb shell content delete --uri <URI> [--user <USER_ID>] --bind <BINDING> [--bind <BINDING>...] [--where <WHERE>]
  Example:
  # Remove "new_setting" secure setting.
  adb shell content delete --uri content://settings/secure --where "name='new_setting'"

#查
usage: adb shell content query --uri <URI> [--user <USER_ID>] [--projection <PROJECTION>] [--where <WHERE>] [--sort <SORT_ORDER>]
  <PROJECTION> is a list of colon separated column names and is formatted:
  <COLUMN_NAME>[:<COLUMN_NAME>...]
  <SORT_ORDER> is the order in which rows in the result should be sorted.
  Example:
  # Select "name" and "value" columns from secure settings where "name" is equal to "new_setting" and sort the result by name in ascending order.
  adb shell content query --uri content://settings/secure --projection name:value --where "name='new_setting'" --sort "name ASC"

查看所有的ContentProvider

adb shell dumpsys activity | grep ContentProviderRecord

//dumpsys信息中能看到该ContentProvider的connections等信息

例子中的表结构比较简单,只是代入学习简单用法。删、查和改的bean都是一样的。

主键(int) _id

值 (long)timestamp

adb shell content insert --uri content://com.panda.eyeprotect.provider/close_range_table --bind timestamp:s:1626958478458

adb shell content delete --uri content://com.panda.eyeprotect.provider/close_range_table --where _id=2

adb shell content query --uri content://com.panda.eyeprotect.provider/close_range_table --projection _id:timestamp --where _id=2

adb shell content query --uri content://com.panda.eyeprotect.provider/close_range_table --projection timestamp --where _id=2 //这样是只查timestamp列

adb shell content update --uri content://com.panda.eyeprotect.provider/close_range_table --bind timestamp:s:7777 --where _id=1

SettingsProvider

SettingsProvider继承于ContentProvider,所以就像上面的usage一样,都是以SettingsProvider中作为例子。从frameworks/base/packages/SettingsProvider/AndroidManifest.xml中可以看到

<provider android:name="SettingsProvider"
 android:authorities="settings"
 android:multiprocess="false"
 android:exported="true"
 android:singleUser="true"
 android:initOrder="100"
 android:visibleToInstantApps="true" />

authorities为settings,分别有system、secure和global三个表。

这里只简单列举查的使用

adb shell content query --uri content://settings/system

adb shell content query --uri content://settings/secure

adb shell content query --uri content://settings/global

这样就可以快速看到表内所有的数据。

SettingsProvider还封装了更为容易使用的命令

读数据

adb shell settings get <table name> <column name>

e.g.adb shell settings get system screen_brightness

写数据

adb shell settings get <table name> <column name> <value>

e.g.adb shell settings put system screen_brightness 230