Search code examples
androidpowershellandroid-intentadb

ADB starting activity with extra string data


I am executing the following from ADB:

PS C:\> adb shell am start -a android.intent.action.MAIN -n boha.notify/.MainActivity -e primaryheader "One Two" -e primarytext "Primary text"
Starting: Intent { act=android.intent.action.MAIN pkg=Two cmp=boha.notify/.MainActivity (has extras) }
Warning: Activity not started, its current task has been brought to the front
PS C:\> 

My application is setup to properly handle the new intent request without restarting the already running activity. I've overridden onNewIntent() as required. It's shown below:

@Override
protected void onNewIntent(Intent intent)
{
    super.onNewIntent(intent);

    Log.d( "AGENT", "NEW INTENT Extra data = " + intent.getStringExtra("primaryheader"));

    String strPrimaryHeader = intent.getStringExtra("primaryheader");
    String strPrimaryText = intent.getStringExtra("primarytext");
    String strSecondayHeader = intent.getStringExtra("secondaryheader");
    String strSecondaryText = intent.getStringExtra("secondarytext");
}

When I check the logcat I see that the extra data strings I'm passing in aren't being received correctly. Only the first word of each string is correct.

See below:

09-13 18:30:36.583 12248-12248/? D/AGENT: NEW INTENT Extra data = One

Why is the activity new intent only receiving "One" instead of "One Two"?

Am I not using the correct ADB am command parameters?


Solution

  • I have explained the root cause multiple times before. It's because you have multiple sub shells. And when the outer shell (in your case powershell) parses the command it eats the " (i.e. uses them for properly splitting the command into separate command line parameters). But this information (that your multi word string is a single parameter) gets lost by the time the command reaches the inner shell (the /bin/sh of your android device). So as the result it launches the am command with every word being a separate parameter.

    The easiest way to mitigate that is to double-quote the whole adb shell subcommand and use single quotes for the internal strings:

    PS C:\> adb shell "am start -a android.intent.action.MAIN -n boha.notify/.MainActivity -e primaryheader 'One Two' -e primarytext 'Primary text'"