【问题标题】:Invoke android application from browser从浏览器调用 android 应用程序
【发布时间】:2011-12-12 20:43:26
【问题描述】:

我一直在尝试从 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>

对于第一个和第二个 url,它给出“网页不可用”。

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

应用程序仍在开发中,因此尚未上市。

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

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

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

【问题讨论】:

    标签: android android-manifest android-intent


    【解决方案1】:

    这是另一种方法:

    要找出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");
    

    【讨论】:

    • 感谢 i.toUri(Intent.URI_INTENT_SCHEME) 的想法
    • 谢谢你,美国宇航局。谢谢你,美国宇航局!谢谢你,美国宇航局!!谢谢你,美国宇航局!!!这是我去年在 SO 遇到的最有价值的页面。 NiTiN,你帮我走出了一个非常深的洞。最有义务的。
    【解决方案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>
    

    我的清单文件如下所示:

    <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=", "");
    

    【讨论】:

      猜你喜欢
      • 2014-10-21
      • 1970-01-01
      • 2016-12-29
      • 1970-01-01
      • 2015-05-17
      • 2011-06-05
      • 2012-06-20
      • 1970-01-01
      相关资源
      最近更新 更多