Mac OS X发送adb shell am开始意图字符串extra with space characters

时间:2015-09-09 02:56:49

标签: android terminal adb

我遇到一个问题,涉及使用Mac OS X终端上的Activity Manager发送包含空格字符的字符串附加内容。

以下命令:

adb shell am start -n com.example.package/.Activity -e 'KEY' 'String with spaces'

...给出:

Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] pkg=with cmp=com.example.package/.Activity (has extras) }

...只有“String”实际传递给Activity。

我可以通过使用反斜杠转义空格来解决这个问题:

adb shell am start -n com.example.package/.Activity -e 'KEY' 'String\ with\ spaces'

...给出:

Starting: Intent { cmp=com.example.package/.Activity (has extras) }

...和“带空格的字符串”被传递给Activity,因此它按预期工作。

问题是转义空间会使我的脚本与Ubuntu终端兼容。在Ubuntu的情况下,“String \ with \ spaces”被传递给活动。

有没有办法强制Mac OS终端解释包含空格的字符串,如Ubuntu终端那样?

1 个答案:

答案 0 :(得分:8)

I figured it out with the help from one of my teammates. It's Ubuntu terminal that has it wrong actually. In first case single quotes are stripped by local shell before sending the command to adb shell. That's why in device shell it executes:

am start -n com.example.package/.Activity -e KEY String with spaces

...and the result is as one may expect.

One solution to the problem can be wrapping the whole command supposed to be executed in device shell with single quotes and use double quotes to wrap strings, like this:

adb shell 'am start -n com.example.package/.Activity -e "KEY" "String with spaces"'

This works as expected on both Ubuntu and Mac terminal.