Home » Android » Receiving data for custom url scheme in Android

Receiving data for custom url scheme in Android

Posted by: admin June 15, 2020 Leave a comment

Questions:

Currently in my app, I have my own URI scheme to detect when user clicks on a particular URI.

Code used in Manifest file is as below:

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

If a user clicks on a link which has my custom URI in browser, It will now popup a options for user to choose my app.

But how do I pass this data which is the link to my app when started for further processing? Basically how do I pass data from browser to my app?

Thanks in Advance

How to&Answers:

Suppose that your url is: http://twitter.com/status/1234
You can get the data using the following method.

  // http://twitter.com/status/1234
    Uri data = getIntent().getData();
    Log.d(TAG, data.toString());
    String scheme = data.getScheme(); // "http"
    Log.d(TAG, scheme);
    String host = data.getHost(); // "twitter.com"
    Log.d(TAG, host);
    String inurl = data.toString();
    List<String> params = data.getPathSegments();
    String first = params.get(0); // "status"
    String second = params.get(1); // "1234"