4

For example, there is an uri example://activityone?foo=bar which can open up an application and launch one of the activity by this adb command

adb shell am start -W -a android.intent.action.VIEW -d "example://activityone?foo=bar" com.example.deeplinking

Are there any other ways to launch the android app through this uri (example://activityone?foo=bar)? How can I put this uri in an email, and when it is clicked, it will launch the app?

s-hunter
  • 24,172
  • 16
  • 88
  • 130

2 Answers2

11

First, you should read the documentation for this: Intents and Intent Filters. Specifically the section "Receiving an Implicit Intent".

As others have stated, using a custom scheme for this has issues.

  • They aren't always treated as links
  • You don't own them like you own a domain name (host).

So you should define an activity in you Manifest with an intent filter for your host and use a real scheme. See the "Action Test", "Category Test" and "Data Test" sections of the Intent & Intent Filters documentation to see how to configure this for your specific use case.

<activity android:name="com.example.app.ActivityOne">
    <intent-filter>
        <data android:scheme="http"/>
        <data android:host="example.com"/>
        <data android:path="/activityone"/>
        <action android:name="android.intent.action.VIEW"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <category android:name="android.intent.category.BROWSABLE"/>
    </intent-filter>
</activity>

Then you will be able to use a link like the following.

http://example.com/activityone?foo=bar

Since this is an http link the system will ask the user to choose which app to open it with (your app or their preferred browser). If they choose your app it will start ActivityOne which can get the query data like so.

public class ActivityOne extends Activity {

    @Override
    public void onResume() {
        super.onResume();
        Intent intent = getIntent();
        Uri uri = intent.getData();
        String foo = uri.getQueryParameter("foo");
    }

}

If you use a custom scheme in the intent filter then you will (most likely) be the only app registered to handle that intent and the user will not have to select your app. However, it will be much harder for you to include a link with a custom scheme in an email. Most email clients will not recognize anything with a custom scheme as a link and it will not be clickable.

mpkuth
  • 6,994
  • 2
  • 27
  • 44
  • How can I put this uri in an email, and when it is clicked, it will launch the app? – s-hunter Jul 24 '16 at 22:37
  • I've updated my answer to include just the link and more information about the app selection/launch process when it is clicked. You can send it to yourself in an email to test. – mpkuth Jul 26 '16 at 13:38
  • If you're using gmail (and maybe other email clients), you have to explicitly tell it that you want `http://example.com/activityone?foo=bar` to be treated as a link and not plain text. Highlight the text and click "Insert Link" in the toolbar down by the send button. – mpkuth Jul 26 '16 at 13:41
  • I knew the http will work and I did that before I posted this question. I am looking for how to open the custom uri (example://activityone?foo=bar), I want to be able to open the deep link even if the scheme is not http. – s-hunter Jul 26 '16 at 14:03
  • The problem is not the setup in your app. The problem is that any custom scheme will most likely NOT be clickable in any SMS or email client. See [this SO post](http://stackoverflow.com/a/20194192/4348328). – mpkuth Jul 26 '16 at 14:26
  • Yes, it is not the setup in the app itself, I am looking for how to launch the app through this custom uri (example://activityone?foo=bar) other than using the adb command line. – s-hunter Jul 26 '16 at 15:12
2

Non-standard URL schemes (which is what example:// is) aren't always treated as links. Your best option is to somehow wrap that URL inside a link that the app CAN recognize (http:// or https://), and then take care of opening your app later, usually by way of some sort of automatic redirect. This is how we handle things to make sure the app always launches no matter where the link is opened.

Toshinou Kyouko
  • 334
  • 9
  • 21
Alex Bauer
  • 13,147
  • 1
  • 27
  • 44