Sending intent to BroadcastReceiver from adb
I've got BroadcastReceiver class:
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 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?
You need not specify receiver. You can use adb instead.
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.
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
The true way to send a broadcast from ADB command is :
adb shell am broadcast -a com.whereismywifeserver.intent.TEST --es sms_body "test from adb"
And, -a
means ACTION, --es
means to send a String extra.
PS. There are other data type you 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:
--ei int_key 0