Search code examples
androidandroid-activityuricustom-url

Custom Android URI


I have read several custom URI posts here and attempted to create a working URI scheme. However, it is not working. Code below for XML:

<activity android:name="testhing.ResetPasswordActivity"
            android:label="@string/title_activity_reset_password"
            android:parentActivityName="testthing.LoginActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="restthing.LoginActivity" />
            <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="TESTTHING" android:host="ForgottenPassword" />
            </intent-filter>
        </activity>

If I attempt to navigate to 'TESTHING://ForgottenPassword' in the browser, it runs a google search but does not open the view. I have a link in an email that is 'TESTHING://ForgottenPassword?token=12345', but this link doesn't even show as clickable. It acts as if the custom URI scheme isn't registered. Is there something wrong in my syntax?


Solution

  • If I attempt to navigate to 'TESTHING://ForgottenPassword' in the browser, it runs a google search but does not open the view.

    There are many browsers for Android. Some might attempt to use PackageManager to see if this random series of characters happens to be a Uri for an ACTION_VIEW activity when you enter it in the address bar. There is no requirement that a browser do this.

    I have a link in an email that is 'TESTHING://ForgottenPassword?token=12345', but this link doesn't even show as clickable.

    There are many email clients for Android. Ideally, an email client, when seeing a link, will at least try the ACTION_VIEW operation with whatever the link claims to be linking to. Once again, there is no requirement that an email client do this.

    Is there something wrong in my syntax?

    I would recommend going with all lowercase for your scheme and host. For example, your email client might be applying a regex that assumes a lowercase scheme, as that's the normal way that URI schemes are written.

    On the whole, custom schemes have only one redeeming value on Android: they are less likely to collide with anything else and trigger a chooser. You are far more likely to have apps recognize your links if you use http as your scheme, going with the "this app handles these URLs" approach. Right now, though, that will trigger a chooser, where the user could choose a browser or your app. That's why in Android M, they are adding the ability to provide evidence on the server that you "own" the URL and that you want these links to drive straight to your app and bypass the chooser.