Monday, March 31, 2014

Android Tip - Intercepting Links on Browsers

Have you ever encountered this situation where you click on a YouTube link on the browser on your Android device and you are prompted to view the video either using the YouTube app or on the browser? How do you make your app respond to a particular type of links on the browser?

You do so, you just need to add in the necessary intent filter to your app’s AndroidManifest.xml file. The following code snippets shows how your app can be launched when the user taps on a link that points to

            <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:pathPrefix="/2014" />

In your activity, you can retrieve the URL that the user has tapped using the getDataString() method of the Intent object:

    protected void onCreate(Bundle savedInstanceState) {
        Intent i = getIntent();            
        Toast.makeText(this, i.getAction(),
        Toast.makeText(this, i.getDataString(),

The following URLs will trigger your app:

But the following URLs won’t:

No comments: