You have a website and an android application and you want it so that an android user will be redirected to the application once they open a link from their email. Good thing android have an easy way to do this using Intents and Intent filters.

The first step is to add an information in your applications AndroidManifest.xml to tell the system that you want to received an intent regarding opened links. Open your AndroidManifest and add the following under the activity that you want to receive the intent.

<activity
 android:name=".MainActivity">
 <intent-filter>
 <action android:name="android.intent.action.MAIN" />
 <category android:name="android.intent.category.LAUNCHER" />
 </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="http" android:host="www.jamesbaltar.com"/>
 </intent-filter>
</activity>

What this does is tell android that your activity can handle and process links that was opened by users. Now everytime a user opens a link to www.jamesbaltar.com a popup window will appear showing a list of applications that can process the link this includes browsers installed, your application and some application that have also registered for this.

Handling Intent

After a user decides to open a link through your application Android will launch your app with an Intent containing the information of the link that was opened. Using these data you will be able to customize the behaviour of your application depeneding on the URL.

@Override
protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 
 //check if the activity was opened with an Intent()
 Intent intent = getIntent();
 if (intent != null) {
 //if there is an Intent() available then check if we can handle it by checking the action and the category
 String action = intent.getAction();
 if (action.equalsIgnoreCase(Intent.ACTION_VIEW) &&
 intent.getCategories().contains(Intent.CATEGORY_BROWSABLE)) {
 //then get the URL and handle it properly
 String url = intent.getDataString();
 handleUrl(url);
 }
 }
}

Now every time your user opens a link through your app you can handle it properly. There is a problem though if you have set your activity as a single instance by declaring it in the manifest. If your activity is declared like this in your Android manifest then you also need to override the onNewIntent() of you activity and process the intent from there aside from onCreate(). This make sure that if you activity is already running it will still handle the link properly and not ignore it.