【问题标题】:Launch app from URL containing question mark从包含问号的 URL 启动应用程序
【发布时间】:2015-07-14 05:09:34
【问题描述】:

我知道如何处理 URL,例如:http://example.com/xyz。但是,我的网址包含如下问号:https://example.com/?xyz

所以当我执行getIntent().getData().getPath() 时,我得到的输出为/。问号在某种程度上限制了处理。我该如何处理?

这是我的manifest 中的intent-filter

<intent-filter>
    <action android:name="android.intent.action.VIEW"></action>
    <category android:name="android.intent.category.DEFAULT"></category>
    <category android:name="android.intent.category.BROWSABLE"></category>
    <data android:host="example.com" android:scheme="https"></data>
</intent-filter>

这是我Activity的代码:

if (getIntent() != null && data != null && data.getPathSegments().size() >= 1) {
    Log.d("tagit", getIntent().getData().getPath().toString());
}

现在,日志显示为空白。

【问题讨论】:

  • 你是在bundle中传递信息吗?
  • 无捆绑。该链接通过 Whatsapp 等第三方应用程序共享。
  • @DanielNugent 添加。

标签: android url android-intent android-activity


【解决方案1】:

好吧,我通过使用以下内容获取完整链接使其正常工作:

data.getEncodedSchemeSpecificPart();

感谢这个答案:https://stackoverflow.com/a/25295636/3739412

【讨论】:

  • 将您的答案标记为正确,以便对其他人有所帮助
  • 我说2天后我可以接受,所以我会这样做:)
【解决方案2】:

您必须对网址进行编码才能处理问号/空格/特殊字符。我正在分享一个简单的代码 sn-p。它可能会帮助你。

String urlStr = "http://example.com/?xyz";
URL url = new URL(urlStr);
URI uri = new URI(url.getProtocol(), url.getUserInfo(), url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
url = uri.toURL();

另外一个更简单的解决方案可以是:

private static final String ALLOWED_URI_CHARS = "@#&=*+-_.,:!?()/~'%";
String urlEncoded = Uri.encode(path, ALLOWED_URI_CHARS);

【讨论】:

  • URI() 中的那些参数有什么作用?此外,我希望 urlStr 字符串在通过 SMS 等应用程序发送给用户时保持原样。否则,如果用户没有安装我的应用,他/她将无法通过浏览器打开链接。
  • 我分享了所有这些作为通用解决方案。您可能不需要所有这些参数。
  • urlEncoded 还是和path("example.com/?xyz")一样。所以我的问题还没有解决。
猜你喜欢
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多