android - 从浏览器链接获取数据到应用程序

标签 android android-intent web

我已经定义了一个 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/

相关文章:

css - 使用 CSS 的 div 的 3d 翻转

html - 视口(viewport)大小通常如何作为参数传递?

java - boolean 值在 RecyclerView 中单击按钮时不起作用

android - MPPT 751 15 通过 VE.Direct 连接到 Raspberry Pi 3(Android of Things)但数据不可读

java - JsonObject 按键获取值

android - Jsoup.connect(url) 总是抛出异常

android - 无法启动服务 Intent : not found

java - MVP 中的 Android NFC 阅读器 - onNewIntent 未触发

android - 未找到处理 Intent 的 Activity

javascript - 绘制线条网页