How do you include ampersands in URLs for adb shel

2019-03-14 08:21发布

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.

标签: android adb
5条回答
Rolldiameter
2楼-- · 2019-03-14 08:38

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 ..."
查看更多
疯言疯语
3楼-- · 2019-03-14 08:39

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'
查看更多
时光不老,我们不散
4楼-- · 2019-03-14 08:40

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.

查看更多
男人必须洒脱
5楼-- · 2019-03-14 08:47

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

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

查看更多
爷的心禁止访问
6楼-- · 2019-03-14 08:49

use escape character \:

$ adb shell am start "http://www.example.com?param1=1\&param2=2"
查看更多
登录 后发表回答