从浏览器调用android应用程序

时间:2011-12-12 20:43:27

标签: android android-manifest android-intent

我一直试图在 android 3.1(Honeycomb)上从浏览器调用我的应用程序 这些是我尝试过的链接,但这些链接都不起作用:

<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>

对于第1和第2个网址,它会显示“网页不可用”。

对于第三个,它显示带有搜索的应用市场:pname:com.nk.myapp - &gt; “没有找到结果”。

应用程序仍处于开发阶段,因此尚未进入市场。

这就是我的清单文件的样子:

<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>

如果你发现我做错了,请告诉我。

2 个答案:

答案 0 :(得分:6)

这是另一种方法:

要找出uri,这就是我在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));

哪个产生这个字符串:

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

Html页面有以下链接:

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

然后,将清单文件更改为

<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>

在TestActivity中,您可以在下面的代码中获得movie_id:

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

答案 1 :(得分:2)

前一段时间解决了这个问题,万一有人需要它:

这就是我的html页面的样子(从设备上的浏览器下载):

<!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>

我的Manifest文件看起来像这样:

<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>

DetailActivity中的代码:

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