从adb向BroadcastReceiver发送意图

81

我有一个BroadcastReceiver类:

public class IntentReceiver extends BroadcastReceiver {

    final String tag = "Intent Intercepter";

    @Override
    public void onReceive(Context context, Intent intent) {
        try {
            String data = intent.getStringExtra("sms_body");
            Log.i(tag, data);
            Toast.makeText(context, data.subSequence(0, data.length()), Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Toast.makeText(context, "Intercepted", Toast.LENGTH_LONG).show();
        }
    }
}

同时在清单文件中也要添加:

<receiver android:name="com.whereismywifeserver.IntentReceiver" android:enabled="true">
    <intent-filter android:priority="999">
        <action android:name="com.whereismywifeserver.intent.TEST"/>
    </intent-filter>
</receiver>

但是,当我尝试从adb发送意图时,会收到错误:

$ adb shell am start 
-a com.whereismywifeserver.intent.TEST
--es sms_body "test from adb" 
-c android.intent.category.HOME 
-n com.whereismywifeserver/.IntentReceiver
Starting: Intent { act=com.whereismywifeserver.intent.TEST t=[android.intent.category.HOME] cmp=com.whereismywifeserver/.IntentReceiver (has extras) }
Error type 3
Error: Activity class {com.whereismywifeserver/com.whereismywifeserver.IntentReceiver} does not exist.

当我在代码中创建意图时,一切都运行正常。那么我怎样才能从adb发送意图?

可能是在模拟器中发送Intent到应用程序的重复问题。 - janot
209
你终于找到她了吗? - Jmorvan
9个回答

112

你不需要指定接收者。你可以使用adb。

adb shell am broadcast -a com.whereismywifeserver.intent.TEST 
--es sms_body "test from adb"

要了解更多参数,如整数附加项,请参阅文档


4
如果字符串 extra(例如上面的“test from adb”)包含空格,广播就无法到达接收器,原因不明。否则它能正常工作。 - user2137020
1
我和@user2137020有相同的问题。 - Xi Wei
7
如果你想要这个字符串 extra,那么你需要使用 -n com.whereismywifeserver/.IntentReceiver - Xi Wei
有没有办法像这样发送广播并带有标志? - iammrmehul
1
@iammrmehul - 文档中说要使用-f与标志一起使用,但我不确定它的样子是什么...https://developer.android.com/studio/command-line/adb#IntentSpec - SteveS
显示剩余2条评论

43

使用ADB命令发送广播的正确方法是:

adb shell am broadcast -a com.whereismywifeserver.intent.TEST --es sms_body "test from adb"

同时, -a 表示 ACTION, --es 表示发送一个字符串(String)类型的附加参数.


附注: 你可以通过指定不同的参数来发送其他数据类型(data type).

[-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
[--esn <EXTRA_KEY> ...]
[--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
[--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
[--el <EXTRA_KEY> <EXTRA_LONG_VALUE> ...]
[--ef <EXTRA_KEY> <EXTRA_FLOAT_VALUE> ...]
[--eu <EXTRA_KEY> <EXTRA_URI_VALUE> ...]
[--ecn <EXTRA_KEY> <EXTRA_COMPONENT_NAME_VALUE>]
[--eia <EXTRA_KEY> <EXTRA_INT_VALUE>[,<EXTRA_INT_VALUE...]]
    (mutiple extras passed as Integer[])
[--eial <EXTRA_KEY> <EXTRA_INT_VALUE>[,<EXTRA_INT_VALUE...]]
    (mutiple extras passed as List<Integer>)
[--ela <EXTRA_KEY> <EXTRA_LONG_VALUE>[,<EXTRA_LONG_VALUE...]]
    (mutiple extras passed as Long[])
[--elal <EXTRA_KEY> <EXTRA_LONG_VALUE>[,<EXTRA_LONG_VALUE...]]
    (mutiple extras passed as List<Long>)
[--efa <EXTRA_KEY> <EXTRA_FLOAT_VALUE>[,<EXTRA_FLOAT_VALUE...]]
    (mutiple extras passed as Float[])
[--efal <EXTRA_KEY> <EXTRA_FLOAT_VALUE>[,<EXTRA_FLOAT_VALUE...]]
    (mutiple extras passed as List<Float>)
[--esa <EXTRA_KEY> <EXTRA_STRING_VALUE>[,<EXTRA_STRING_VALUE...]]
    (mutiple extras passed as String[]; to embed a comma into a string,
     escape it using "\,")
[--esal <EXTRA_KEY> <EXTRA_STRING_VALUE>[,<EXTRA_STRING_VALUE...]]
    (mutiple extras passed as List<String>; to embed a comma into a string,
     escape it using "\,")
[-f <FLAG>]

例如,您可以通过以下方式发送一个整数值:

--ei int_key 0

34

我发现命令是错误的,正确的命令包含“broadcast”而不是“start”:

adb shell am broadcast -a com.whereismywifeserver.intent.TEST --es sms_body "test from adb" -n com.whereismywifeserver/.IntentReceiver

9

如果使用 Android 8 或更高版本并添加了隐式接收器限制,则应在终端命令的末尾添加你的应用程序包名称:

adb shell am broadcast -a my.app.package.TEST my.app.package

如果您的软件包在调试模式下具有后缀,请改用my.app.package.debug


谢谢!我在命令的末尾添加了包名,它开始工作了))) - Alexey Kolosov

8

正如许多人已经注意到的那样,只有当额外的字符串包含空格时才会出现问题。

根本原因是OP的主机操作系统/ shell(即Windows/cmd.exe)破坏了输入的命令——"字符丢失,--es sms_body "test from adb" 变成了 --es sms_body test from adb。这导致sms_body字符串被赋予test的值,其余的字符串变成了<URI>|<PACKAGE>|<COMPONENT>。

为避免这一切,您可以使用:

adb shell "am broadcast -a com.whereismywifeserver.intent.TEST --es sms_body 'test from adb' -n com.whereismywifeserver/.IntentReceiver"

或者首先启动交互式的 adb shell 会话,然后从其中运行 am broadcast 命令。


7

6
记录下我的情况可能对某些人有用,
我需要在Android P中向广播接收器发送带有多个意图扩展的自定义意图,
具体细节如下:
接收器名称:com.hardian.testservice.TestBroadcastReceiver 意图action = "com.hardian.testservice.ADD_DATA" 意图扩展为:
  1. "text"="测试消息",
  2. "source"= 1,
在命令行中运行以下命令。
adb shell "am broadcast -a com.hardian.testservice.ADD_DATA --es text 'test msg' --es source 1 -n com.hardian.testservice/.TestBroadcastReceiver"

希望这可以帮助到您。

5

我曾遇到同样的问题,后来发现你必须在extra字段中转义空格:

adb shell am broadcast -a com.whereismywifeserver.intent.TEST --es sms_body "test\ from\ adb"

所以,不应该写成“test from adb”,而是应该写成“test\ from\ adb”。

2

我不确定是否有人在获取完整字符串“test from adb”时遇到了问题。在空格前面使用转义字符对我有用。

adb shell am broadcast -a com.whereismywifeserver.intent.TEST --es sms_body "test\ from\ adb" -n com.whereismywifeserver/.IntentReceiver

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