RobertEves92
RobertEves92

Reputation: 145

Launch android app activity from web link that both starts and ends in a specific way

Is there a way to launch a web link with an application activity where the web link starts and ends in a specific manner?

I want to launch an activity if a user clicks on a web link to a particular website, for example that has been shared on facebook or via email, and that starts with the website address but only if it ends with "story.html". So for example http://www.storyofmathematics.com/ would not open the app but http://www.storyofmathematics.com/story.html would

Upvotes: 1

Views: 964

Answers (1)

RobertEves92
RobertEves92

Reputation: 145

This can be achieved by adding the activity to the manifest with the appropriate intent filter

    <activity
        android:name=".WebActivity"
        android:label="@string/app_name" >
        <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:host="www.storyofmathematics.com"
                android:scheme="http" />
            <data
                android:host="storyofmathematics.com"
                android:scheme="http" />
        </intent-filter>
    </activity>

Then by running a regex from within the activity

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class WebActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();

        if (intent.getDataString().matches(
                "http:\\/\\/((www.)?)storyofmathematics.com\\/.*story.html")) {
            // is match - do stuff
        } else {
            // is not match - do other stuff
        }
    }
}

Upvotes: 1

Related Questions