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:
C:\Users\i.yesilevsky>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.whereismywifeser
ver.IntentReceiver} does not exist.
When I create intent in code, everything works fine. So how can I send intent from adb?
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 insms_body
string extra getting assigned the value oftest
and the rest of the string becoming<URI>|<PACKAGE>|<COMPONENT>
specifier.To avoid all that you could use:
or just start the interactive
adb shell
session first and run theam broadcast
command from inside of it.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,
The details are,
Receiver name:
com.hardian.testservice.TestBroadcastReceiver
Intent
action = "com.hardian.testservice.ADD_DATA"
intent extras are,
Run the following in command line.
Hope this helps.
Another thing to keep in mind: Android 8 limits the receivers that can be registered via manifest (e.g., statically)
https://developer.android.com/guide/components/broadcast-exceptions
No need to specify Receiver. Try this:
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":
The true way to send a broadcast from ADB command is :
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:
For example, you can send an int value by: