我已经定义了一个 Intent 过滤器来监听我的应用程序中的自定义方案,但我还必须向我的应用程序发送数据,以便我可以采取相应的行动。
我想要完成的 - 向用户发送一个链接(在浏览器中),他点击该链接,然后将他带到我的应用程序,根据他点击的 URL,将一些数据添加到数据库中。
最佳答案
您需要将此数据嵌入到附加到 URI 方案的路径中。假设您使用以下 intent 过滤器和 myapp 的自定义方案设置您的 Activity:
<intent-filter>
<data android:scheme="myapp" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
现在,创建一个链接并将您想要的所有数据以查询参数的形式附加到 URI 方案:
myapp://open?custom_param1=val1
然后,在onCreate
中,您可以解析 Intent
Uri data = this.getIntent().getData();
if (data != null && data.isHierarchical() && activity != null) {
if (data.getQueryParameter("custom_param1") != null) {
String param1 = data.getQueryParameter("custom_param1");
// do some stuff
}
}
或者您可以使用类似 Branch 的服务它允许您将 JSON 格式的无限数据 bundle 到一个链接中,该链接在单击链接和打开应用程序时检索。它使这个过程变得更加容易。
关于android - 从浏览器链接获取数据到应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31764916/