Home > database >  It is mandatory to specify the "android:host" for all url in android for Deep Link?
It is mandatory to specify the "android:host" for all url in android for Deep Link?

Time:12-07

I have to filter two URL for an activity. I'm using Deep Links to App Content by specifying a url for the Deep Link Screen.

These are my urls

  • appdemo://deeplink
  • native://

I'm already added these two scheme to my Android Manifest file, looks like

            <data android:scheme="appdemo" android:host="deeplink" />
            <data android:scheme="native" />

my question is that, by providing scheme and host on the Android Manifest file, native:// this link does not work. it requires the android:host name also (native://deeplink).

It is mandatory to specify the "android:host" for all url in android? If no, how can i specify the different scheme.

CodePudding user response:

The idea beside deep links is to use the same structure as normal links:

scheme://host/path

The main benefit is that you can teach your app even to open some Internet links, e.g. to open youtube (Android will ask, if you want to open in browser or YouTube app or your app):

    <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="http"/>
        <data android:scheme="https"/>
        <data android:host="youtube.com"/>
        <data android:host="www.youtube.com"/>
        <data android:host="m.youtube.com"/>
        <data android:host="youtu.be"/>
        <data android:pathPattern=".*"/>
    </intent-filter>

So, answering your questions:

  1. Yes, it is mandatory to specify both scheme and host.
  2. A good practice of handling two different links in one activity is to use different paths. Like this:
    <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="appdemo" android:host="deeplink" android:pathPrefix="/path1/"/>
    </intent-filter>
    <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="appdemo" android:host="deeplink" android:pathPrefix="/path2/"/>
    </intent-filter>

Then you can get your path in onNewIntent:

Uri data = intent.getData();
String path = data == null ? null : data.getPath();

...and build some logic depending on this path.

CodePudding user response:

The above answer is acceptable and, in my case i want to navigate a new activity through this intent filter, and i got some information to the above answer and made some changes in my code and fix the issue. Also I'm not specifying the host name for my second intent.

        <activity
        android:name=".DeepLinkActivity"
        android:exported="true"
        android:screenOrientation="portrait"
        android:theme="@style/AppTheme.Launcher">
        <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="appdemo" android:host="deeplink" />
        </intent-filter>

        <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="native" />
        </intent-filter>

    </activity>

Now my both url navigate to DeepLinkActivity.

appdemo://deeplink
native://

Do you think this is not an optimized way? Any other suggestions for that please mention.

  • Related