Omer
Omer

Reputation: 335

Handle a link in the Android browser/webview to start directly an application

I want my application to be opened from a link, by following the directions of Make a link in the Android browser start up my app?. Well, it works fine (most of the time.... this is the reason I am asking for help).

My html link is

<a href="intent:#Intent;action=com.mark.MY_ACTION;end">Open Application</a>

My intent filter is in the form

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

So far so good (I hope). It works fine from m-o-s-t of the browsers.

To test the above I wrote a demo activity

final WebView webview = new WebView(this); 
setContentView(webview); 
webview.loadUrl("http://www.myhomepage.com/"); 

The page http://www.myhomepage.com/ has some custom links/hrefs (including the inent one).

Pressing regular links opens me a browser with those links, but pressing my "special link" (the intnet one) opens a 404 page

Web page not available
intent:#Intent;action=com.mark.MY_ACTION;end
might be temporarily down....

While Diane mentioned in The above link,

They allow you to direct the launch to only your app without the user having the option of instead going to the browser or any other app.

Upvotes: 3

Views: 6795

Answers (3)

Parag Chauhan
Parag Chauhan

Reputation: 35986

Add this code in Manifest.xml

<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.parag-chauhan.com"
        android:path="/test"
        android:scheme="http" />
</intent-filter>

For test create another project and run below code

Intent i = new Intent(
    Intent.ACTION_VIEW , Uri.parse("http://www.parag-chauhan.com/test")
);
startActivity(i);

You can also pass parameter in link. For more info, see my blog post Make a link in the Android browser start up my app?

Upvotes: 1

IRvanFauziE
IRvanFauziE

Reputation: 852

Try my simple trick:

        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if(url.startsWith("classRegister:")) {                  
                Intent MnRegister = new Intent(getApplicationContext(), register.class); startActivity(MnRegister);
            }               
            view.loadUrl(url);
            return true;
        }

and my html link:

<a href="classRegister:true">Go to register.java</a>

or you can make < a href="classRegister:true" > <- "true" value for class filename

however this script work for mailto link :)

        if (url.startsWith("mailto:")) {
            String[] blah_email = url.split(":");
            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
            emailIntent.setType("text/plain");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{blah_email[1]});
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, what_ever_you_want_the_subject_to_be)");
            Log.v("NOTICE", "Sending Email to: " + blah_email[1] + " with subject: " + what_ever_you_want_the_subject_to_be);
            startActivity(emailIntent);
        }

Upvotes: 1

AliDeo
AliDeo

Reputation: 169

your Intent Filter is some same as mentioned by Diana

<activity android:name=".AntonWorld"
      android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter>
        <data android:scheme="anton" />
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

So yours should be like:

<intent-filter> 
    <data android:scheme="com.mark.MY_ACTION"/>
    <category android:name="android.intent.category.DEFAULT"/>
    <category android:name="android.intent.category.BROWSABLE"/>
</intent-filter>

Did not test if android.intent.category.VIEW is required. Let me know if it works.

Upvotes: 1

Related Questions