简体   繁体   English

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

[英]Getting data to app with intent from browser link

I have defined an intent filter to listen for a custom scheme in my app but I must also send data to my app so I can act accordingly. 我已经定义了一个intent过滤器来监听我的应用程序中的自定义方案,但我还必须将数据发送到我的应用程序,以便我可以采取相应的行动。

What I want to accomplish - send a link to the user (in a browser) which he clicks and it takes him to my app where it will add some data to the database depending on the URL he clicked on. 我想要完成的任务 - 发送一个链接到用户(在浏览器中),他点击它,并将他带到我的应用程序,它将根据他点击的URL将一些数据添加到数据库。

You'll need to embed this data into the path that you append to the URI scheme. 您需要将此数据嵌入到附加到URI方案的路径中。 Let's say you set up your Activity with the following intent filter with the custom scheme of myapp : 假设您使用myapp的自定义方案使用以下intent过滤器设置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>

Now, create a link and append all of the data you want to the URI scheme in the form of query parameters: 现在,创建一个链接,并以查询参数的形式将所需的所有数据附加到URI方案:

myapp://open?custom_param1=val1

Then, in onCreate , you can parse the intent 然后,在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
    }
}

Or you can use a service like Branch that lets you bundle unlimited data in JSON format into a link, that is retrieved on link click and app open. 或者您可以使用像Branch这样的服务,它允许您将JSON格式的无限数据捆绑到链接中,该链接在链接点击和应用程序打开时检索。 It makes this process much easier. 它使这个过程更容易。

您必须将此类别放在intent过滤器中:

<category android:name="android.intent.category.BROWSABLE" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM