gpt4 book ai didi

android - Mac OS X 发送 adb shell am start intent string extra with space characters

转载 作者:太空狗 更新时间:2023-10-29 15:52:00 32 4
gpt4 key购买 nike

我遇到一个问题,涉及在 Mac OS X 终端上使用 Activity Manager 发送包含带有空格字符的额外字符串的 Intent 。

以下命令:

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”被传递给 Activity 。

有什么方法可以强制 Mac OS 终端像 Ubuntu 终端那样解释额外包含空格的字符串?

最佳答案

我在一位队友的帮助下弄明白了。实际上是 Ubuntu 终端出错了。在第一种情况下,在将命令发送到 adb shell 之前,本地 shell 会去除单引号。这就是它在设备外壳中执行的原因:

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

...结果如人们所料。

该问题的一个解决方案是将应该在设备 shell 中执行的整个命令用单引号括起来,并使用双引号将字符串括起来,如下所示:

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

这在 Ubuntu 和 Mac 终端上都按预期工作。

关于android - Mac OS X 发送 adb shell am start intent string extra with space characters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32470219/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com