Sending intent to BroadcastReceiver from adb

前端 未结 8 1743
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-29 20:26

I\'ve got BroadcastReceiver class:

public class IntentReceiver extends BroadcastReceiver {

    final String tag = \"Intent Intercepter\";

    @Override
            


        
相关标签:
8条回答
  • 2020-11-29 20:49

    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.

    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.

    0 讨论(0)
  • 2020-11-29 20:51

    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 shell am broadcast -a com.whereismywifeserver.intent.TEST --es sms_body "test\ from\ adb" -n com.whereismywifeserver/.IntentReceiver
    
    0 讨论(0)
  • 2020-11-29 20:59

    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
    
    0 讨论(0)
  • 2020-11-29 21:00

    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"

    0 讨论(0)
  • 2020-11-29 21:05

    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

    0 讨论(0)
  • 2020-11-29 21:13

    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
    
    0 讨论(0)
提交回复
热议问题