Android 从 adb 向 BroadcastReceiver 发送意图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22634446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending intent to BroadcastReceiver from adb
提问by user2106655
I've got BroadcastReceiver class:
我有 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();
}
}
}
And also in manifest:
并且也在清单中:
<receiver android:name="com.whereismywifeserver.IntentReceiver" android:enabled="true">
<intent-filter android:priority="999">
<action android:name="com.whereismywifeserver.intent.TEST"/>
</intent-filter>
</receiver>
But when I try to send intent from adb, I receive error:
但是当我尝试从 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.
When I create intent in code, everything works fine. So how can I send intent from adb?
当我在代码中创建意图时,一切正常。那么如何从 adb 发送意图?
回答by Zohra Khan
You need not specify receiver. You can use adb instead.
您无需指定接收者。您可以改用 adb。
adb shell am broadcast -a com.whereismywifeserver.intent.TEST
--es sms_body "test from adb"
For more arguments such as integer extras, see the documentation.
有关更多参数(例如整数附加值),请参阅文档。
回答by user2106655
I've found that the command was wrong, correct command contains "broadcast" instead of "start":
我发现命令是错误的,正确的命令包含“广播”而不是“开始”:
adb shell am broadcast -a com.whereismywifeserver.intent.TEST --es sms_body "test from adb" -n com.whereismywifeserver/.IntentReceiver
回答by CalvinChe
The true way to send a broadcast from ADB command is :
从 ADB 命令发送广播的真正方法是:
adb shell am broadcast -a com.whereismywifeserver.intent.TEST --es sms_body "test from adb"
And, -a
means ACTION, --es
means to send a Stringextra.
并且,-a
意味着ACTION,--es
意味着发送额外的字符串。
PS. There are other data typeyou can send by specifying different params like:
附注。您可以通过指定不同的参数来发送其他数据类型,例如:
[-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>]
For example, you can send an int value by:
例如,您可以通过以下方式发送 int 值:
--ei int_key 0
回答by Yang
Another thing to keep in mind: Android 8 limits the receivers that can be registered via manifest (e.g., statically)
要记住的另一件事:Android 8 限制了可以通过清单注册的接收器(例如,静态)
https://developer.android.com/guide/components/broadcast-exceptions
https://developer.android.com/guide/components/broadcast-exceptions
回答by Alex P.
As many already noticed, the problem manifests itself only if the extra string contains whitespaces.
正如许多人已经注意到的那样,只有当额外的字符串包含空格时才会出现问题。
The root cause is that OP's host OS/shell (i.e. Windows/cmd.exe) mangles the entered command - the "
characters get lost, --es sms_body "test from adb"
becomes --es sms_body test from adb
. Which results in sms_body
string extra getting assigned the value of test
and the rest of the string becoming <URI>|<PACKAGE>|<COMPONENT>
specifier.
根本原因是 OP 的主机 OS/shell(即 Windows/cmd.exe)破坏了输入的命令 -"
字符丢失,--es sms_body "test from adb"
变成--es sms_body test from adb
. 这导致sms_body
字符串 extra 被分配了 的值,test
并且字符串的其余部分成为<URI>|<PACKAGE>|<COMPONENT>
说明符。
To avoid all that you could use:
为了避免您可以使用的所有内容:
adb shell "am broadcast -a com.whereismywifeserver.intent.TEST --es sms_body 'test from adb' -n com.whereismywifeserver/.IntentReceiver"
or just start the interactive adb shell
session first and run the am broadcast
command from inside of it.
或者只是先启动交互式adb shell
会话并am broadcast
从其中运行命令。
回答by melbic
I had the same problem and found out that you have to escape spaces in the extra:
我遇到了同样的问题,发现你必须在额外的空格中转义:
adb shell am broadcast -a com.whereismywifeserver.intent.TEST --es sms_body "test\ from\ adb"
So instead of "test from adb" it should be "test\ from\ adb"
所以,而不是“从亚行测试”,它应该是“测试\从\亚行”
回答by sadat
I am not sure whether anyone faced issues with getting the whole string "test from adb". Using the escape character in front of the space worked for me.
我不确定是否有人在获取整个字符串“来自 adb 的测试”时遇到问题。在空格前使用转义字符对我有用。
adb shell am broadcast -a com.whereismywifeserver.intent.TEST --es sms_body "test\ from\ adb" -n com.whereismywifeserver/.IntentReceiver
回答by Hardian
Noting down my situation here may be useful to somebody,
在这里记下我的情况可能对某人有用,
I have to send a custom intent with multiple intent extras to a broadcast receiver in Android P,
我必须向 Android P 中的广播接收器发送一个带有多个 Intent Extras 的自定义 Intent,
The details are,
详情是,
Receiver name: com.hardian.testservice.TestBroadcastReceiver
收件者姓名: com.hardian.testservice.TestBroadcastReceiver
Intent action = "com.hardian.testservice.ADD_DATA"
意图 action = "com.hardian.testservice.ADD_DATA"
intent extras are,
额外的意图是,
- "text"="test msg",
- "source"= 1,
- "text"="测试消息",
- “来源”= 1,
Run the following in command line.
在命令行中运行以下命令。
adb shell "am broadcast -a com.hardian.testservice.ADD_DATA --es text 'test msg' --es source 1 -n com.hardian.testservice/.TestBroadcastReceiver"
Hope this helps.
希望这可以帮助。