NiTiN
NiTiN

Reputation: 1022

Invoke android application from browser

I have been trying to invoke my application from browser on android 3.1 (Honeycomb) These are the links I tried, but none of these worked:

<a href="com.nk.myapp://movieid=95319">click me 1</a>

<a href="intent:#Intent;action=com.nk.myapp.detail;end">click me 2</a>

<a href=intent:#Intent;action=com.nk.myapp.detail;category=android.intent.category.DEFAULT;category=android.intent.category.BROWSABLE;package=com.nk.myapp;end>click me 3</a>

For 1st and 2nd url, it gives "Webpage not available".

For 3rd one, it shows Apps-market with search: pname:com.nk.myapp --> "No Result found".

Application is still in development, so its not in the market yet.

This is how my manifest file looks like:

<activity
    android:label="@string/app_name"
    android:launchMode="singleTop"
    android:name=".MainActivity"
    android:screenOrientation="landscape" >
    <intent-filter >
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
    <intent-filter >
        <action android:name="com.nk.myapp.action.DIALOG" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    <intent-filter >
        <action android:name="android.intent.action.SEARCH" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>
<activity
    android:name=".WebViewActivity"
    android:screenOrientation="landscape" >
    <intent-filter>
        <data android:scheme="com.nk.myapp" />
        <action android:name="com.nk.myapp.detail" />
        <category android:name="android.intent.category.BROWSABLE" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Let me know if you find what I am doing wrong.

Upvotes: 3

Views: 7248

Answers (2)

NiTiN
NiTiN

Reputation: 1022

Here is Another way to do it:

To find out uri, this is what I added in Test Apk:

Intent i = new Intent();
i.setAction("com.appname.NK_CUSTOM_ACTION");
i.putExtra("movie_id", "123456");
Log.e("IntentTest", i.toUri(Intent.URI_INTENT_SCHEME));

Which produces this string:

intent:#Intent;action=com.appname.NK_CUSTOM_ACTION;S.movie_id=123456;end

Html page has this link:

<a href="intent:#Intent;action=com.appname.NK_CUSTOM_ACTION;S.movie_id=123456;end">click to load</a>

And, change manifest file to

<activity
    android:name=".TestActivity"
    android:screenOrientation="sensorLandscape" >
    <intent-filter>
        <action android:name="com.appname.NK_CUSTOM_ACTION" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
    </intent-filter>
</activity>

In TestActivity, you get movie_id in code below:

Intent intent = getIntent();
String data = intent.getStringExtra("movie_id");

Upvotes: 6

NiTiN
NiTiN

Reputation: 1022

Solved the problem while ago, just in case anyone needs it:

This is how my html page looks like (which get downloaded from browser on device):

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Andriod Sample</title>
    <script type="text/javascript">

        function getQuerystring(key, default_) {
            if (default_ == null) default_ = "";
            key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
            var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
            var qs = regex.exec(window.location.href);
            if (qs == null)
                return default_;
            else
                return qs[1];
        }

        function invokeApp(xyz) {
            var id = getQuerystring('id');
            var contentType = getQuerystring('contentType');
            var url = "myAppSceme://movie?id=" + id ;    
            document.getElementById('ref').href = url;                
        }

    </script>
</head>
<body>
<div>
<a href="#" id="ref" onclick="invokeApp(this);" >click me!</a>  

</div>
</body>
</html>

And my Manifest file looks like this:

<activity android:name=".DetailActivity" android:screenOrientation="sensorLandscape" >
  <intent-filter >
    <data android:scheme="myAppSceme" />
    <action android:name="android.intent.action.VIEW" />
    <category android:name="android.intent.category.BROWSABLE" /> 
    <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>
</activity>

Code in DetailActivity:

String id = getIntent().getDataString().replace("myAppSceme://movie?id=", "");

Upvotes: 2

Related Questions