Home » Android » android – How do you include ampersands in URLs for adb shell am start?

android – How do you include ampersands in URLs for adb shell am start?

Posted by: admin April 23, 2020 Leave a comment

Questions:

Using

$ adb shell am start some://url

I can launch URLs using activity manager. However if I include multiple URL parameters, all but the first parameter gets stripped out.

Example:

$ adb shell am start http://www.example.com?param1=1&param2=2

Returns:

$ Starting: Intent { act=android.intent.action.VIEW dat=http://www.example.com?param1=1 }

and param2 disappears as anything after an ampersand gets ignored. I’m wondering if there’s some encoding/escape character for the & that will prevent this.

How to&Answers:

use escape character \:

$ adb shell am start "http://www.example.com?param1=1\&param2=2"

Answer:

The following format seems to work. Note the quotes format ' ":

$ adb shell am start -d '"http://www.example.com?param1=1&param2=2"'

Answer:

The accepted solution does not work because of a bug in the android build tools which you can track here: https://code.google.com/p/android/issues/detail?id=76026 . A workaround is the below:

echo 'am broadcast -a com.android.vending.INSTALL_REFERRER -n <your package>/<broadcast-receiver> --es "referrer" "utm_source=test_source&utm_medium=test_medium&utm_term=test_term&utm_content=test_content&utm_campaign=test_name";exit'|adb shell

To integrate it in gradle you can use the commandLine statement

commandLine "bash","-c","echo ..."

Answer:

I have already posted a workaround here: https://code.google.com/p/android/issues/detail?id=76026

So, here is the recipe that involves instrumentation.
Register a BroadcastReceiver within the instrumentation that listens to action com.example.action.VIEW.

IntentFilter intentFilter = new IntentFilter("com.example.action.VIEW");
intentFilter.addDataScheme("myschema");
intentFilter.addCategory(Intent.CATEGORY_BROWSABLE);
Context.registerReceiver(new MyBroadcastReceiver(), intentFilter);

Replace ampersand with %26 (use can replace it with anything you want) and send an intent com.example.action.VIEW.
Once received intent BroadcastReceiver converts %26 back to ampersand and sends a new intent with desired action to your app.

public final void onReceive(final Context context, final Intent intent) {
    intent.setAction(Intent.ACTION_VIEW);
    intent.setData(Uri.parse(intent.getDataString().replaceAll("%26", "&")));
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(intent);
}

Basically it acts as a BroadcastReceiver proxy.

Answer:

Quote the am... command!
Something like the following should work (if it doesn’t, try double-quote):

adb shell 'am start http://www.example.com?param1=1&param2=2'