Praveen
Praveen

Reputation: 91175

Android - launching My APP from Hyperlink in the email?

I need to wake up my application while clicking on the hyperlink that i got via email.

Any Idea? Please help on this.

Thanks in Advance.

Upvotes: 0

Views: 228

Answers (1)

alex.zherdev
alex.zherdev

Reputation: 24164

This can be done by using a custom URI scheme (like those market: urls that are handled by the Market app), or by using a custom action with the intent: scheme.

In both cases you should create an activity that will be started when the user clicks your link.

Let's first go through the first case:

1. A custom scheme

Start by declaring the activity in the manifest:

<activity android:name="LinkHandler">
  <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="SACPK" android:host="www.anyhost.com" />
  </intent-filter>
</activity>

In this case, the link should look like SACPK://www.anyhost.com/anything-goes-here.

Your activity will receive the whole link in the intent, so you can process it and decide what to do next, based on query parameters or path:

public class LinkHandler extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Uri uri = getIntent().getData();
        // this is the URI containing your link, process it
    }
}

2. intent: scheme

This time the link should have the following format:

intent:#Intent;action=com.sacpk.CUSTOM_ACTION;end

And the intent-filter should contain a corresponding action that you will check in your activity:

<intent-filter>
  <action android:name="com.sacpk.CUSTOM_ACTION" />
  <category android:name="android.intent.category.DEFAULT" />
  <category android:name="android.intent.category.BROWSABLE" />
</intent-filter>

And in your onCreate method:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if ("com.sacpk.CUSTOM_ACTION".equals(getIntent().getAction()) {
         // then you really know you got here from the link
    }
}

The downside to this method is that you won't get any data with your intent.

This whole answer is based on commonsware's brilliant book, The Busy Coder's Guide to Advanced Android Development.

Upvotes: 2

Related Questions