How to implement my very own URI scheme on Android

后端 未结 5 692
借酒劲吻你
借酒劲吻你 2020-11-22 06:12

Say I want to define that an URI such as:

myapp://path/to/what/i/want?d=This%20is%20a%20test

must be handled by my own application, or serv

相关标签:
5条回答
  • 2020-11-22 06:52

    As the question is asked years ago, and Android is evolved a lot on this URI scheme.
    From original URI scheme, to deep link, and now Android App Links.

    Android now recommends to use HTTP URLs, not define your own URI scheme. Because Android App Links use HTTP URLs that link to a website domain you own, so no other app can use your links. You can check the comparison of deep link and Android App links from here

    Now you can easily add a URI scheme by using Android Studio option: Tools > App Links Assistant. Please refer the detail to Android document: https://developer.android.com/studio/write/app-link-indexing.html

    0 讨论(0)
  • 2020-11-22 07:02

    This is very possible; you define the URI scheme in your AndroidManifest.xml, using the <data> element. You setup an intent filter with the <data> element filled out, and you'll be able to create your own scheme. (More on intent filters and intent resolution here.)

    Here's a short example:

    <activity android:name=".MyUriActivity">
        <intent-filter>
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="myapp" android:host="path" />
        </intent-filter>
    </activity>
    

    As per how implicit intents work, you need to define at least one action and one category as well; here I picked VIEW as the action (though it could be anything), and made sure to add the DEFAULT category (as this is required for all implicit intents). Also notice how I added the category BROWSABLE - this is not necessary, but it will allow your URIs to be openable from the browser (a nifty feature).

    0 讨论(0)
  • 2020-11-22 07:10

    Another alternate approach to Diego's is to use a library:

    https://github.com/airbnb/DeepLinkDispatch

    You can easily declare the URIs you'd like to handle and the parameters you'd like to extract through annotations on the Activity, like:

    @DeepLink("path/to/what/i/want")
    public class SomeActivity extends Activity {
      ...
    }
    

    As a plus, the query parameters will also be passed along to the Activity as well.

    0 讨论(0)
  • 2020-11-22 07:14

    Complementing the @DanielLew answer, to get the values of the parameteres you have to do this:

    URI example: myapp://path/to/what/i/want?keyOne=valueOne&keyTwo=valueTwo

    in your activity:

    Intent intent = getIntent();
    if (Intent.ACTION_VIEW.equals(intent.getAction())) {
      Uri uri = intent.getData();
      String valueOne = uri.getQueryParameter("keyOne");
      String valueTwo = uri.getQueryParameter("keyTwo");
    }
    
    0 讨论(0)
  • 2020-11-22 07:16

    I strongly recommend that you not define your own scheme. This goes against the web standards for URI schemes, which attempts to rigidly control those names for good reason -- to avoid name conflicts between different entities. Once you put a link to your scheme on a web site, you have put that little name into entire the entire Internet's namespace, and should be following those standards.

    If you just want to be able to have a link to your own app, I recommend you follow the approach I described here:

    How to register some URL namespace (myapp://app.start/) for accessing your program by calling a URL in browser in Android OS?

    0 讨论(0)
提交回复
热议问题