从命令行(adb shell)查询Android内容提供程序

29

有一个基于意图启动活动的命令:am start。还有发送广播的命令:am broadcast

我认为可能应该有一个用于查询内容提供者的 shell 命令,可能类似于:

query content://com.myapp.authority/path --where 'column=?' --arg 1 --order 'column desc'

或类似的东西。

有吗?


如何使我的应用程序免受恶意数据注入的影响?即如何对“adb shell content insert --uri”具有免疫力。 - David Prun
3个回答

60

有一个content命令:

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
  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"

usage: adb shell content call --uri <URI> --method <METHOD> [--arg <ARG>]
       [--extra <BINDING> ...]
  <METHOD> is the name of a provider-defined method
  <ARG> is an optional string argument
  <BINDING> is like --bind above, typed data of the form <KEY>:{b,s,i,l,f,d}:<VAL>

如何使我的应用程序免受恶意数据注入的影响?即如何对“adb shell content insert --uri”具有免疫力? - David Prun
1
为了保护ContentProvider,请在AndroidManifest.xml中添加权限使用,例如:<permission android:name="com.yourApp.permission.WRITE_PROVIDER" android:protectionLevel="signature" /><uses-permission android:name="com.yourApp.permission.WRITE_PROVIDER" /><provider android:name="YourProvider" android:authorities="com.your_app.authority" ... android:writePermission="com.yourApp.permission.WRITE_PROVIDER" ... /> - dkneller
我正在尝试使用这个查询来获取屏幕方向,但它不起作用。我的命令是 adb shell content query --uri content://settings/system -projection name:value --where "name='user_rotation'" - kanna
@kanna 在你的情况下,将 "name='user_rotation'" 更改为 "name=\'user_rotation\'" - user4732674

0

看起来在 MacOS 上一些 shell(ZSH?)需要一些奇怪的转义才能使其正常工作:

adb shell content query --uri content://settings/global --where "name\ =\ \'airplane_mode_radios\'"

替代

adb shell content query --uri content://settings/global --where "name = 'airplane_mode_radios'"

0
adb shell content query --uri content://com.myapp.authority/path --where column=x --arg 1 --sort column_name DESC

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接