从 URL 启动活动

2024-07-03

我试图在用户浏览到某个网址时启动我的应用程序。我找到了一些示例,它们在清单中都有相同的内容,但它对我不起作用。我已将意图过滤器放在活动和接收器下。

这是我的清单片段:

<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="www.urbandictionary.com" android:scheme="http"></data>
</intent-filter>

当在 Activity 下时,我尝试使用 onNewIntent,当它在 Receiver 下时,我尝试使用 onReceiveIntent,两者都通过简单的 Log.i 调用来查看它是否触发。我运气不太好。


我在我的manifest.xml 文件中使用它:

<activity android:name=".SomeName">
    <intent-filter>
        <category android:name="android.intent.category.ALTERNATIVE" />
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:host="google.com" android:scheme="http" />  
    </intent-filter>
</activity>

这将启动 SomeName 活动。我不在 android:host 部分使用 www 也许这会有所作为。

当活动开始时,您可以使用(例如)获取 .com 背后的数据:

Uri data = getIntent().getData();
if(data != null && data.getPathSegments().size() >= 2){
    List<String> params = data.getPathSegments();
    String somestuff = params.get(0);
}

编辑:如果您不希望能够从活动中检查主机,请使用以下方法:

data.getHost();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 URL 启动活动 的相关文章

随机推荐