简体   繁体   中英

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.

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.

You'll need to embed this data into the path that you append to the URI scheme. Let's say you set up your Activity with the following intent filter with the custom scheme of myapp :

<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:

myapp://open?custom_param1=val1

Then, in onCreate , you can parse the 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. It makes this process much easier.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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