2011-12-12 90 views
2

我一直在试图调用上的Android 3.1(蜂巢)的浏览器我的应用程序的Android应用程序 我想这些都是链接,但这些都不工作:调用从浏览器

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

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

对于第三个,它显示应用程序市场与搜索: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> 

让,如果你发现我在做什么错我知道。

回答

6

这里是另一种方式来做到这一点:

要找出URI,这是我在的测试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> 

在测试活动中,您在下面的代码获得movie_id:

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

感谢i.toUri(Intent.URI_INTENT_SCHEME) – Quincy

+1

的想法谢谢你,美国航空航天局。谢谢你,美国宇航局!谢谢你,NASA!谢谢你,NASA!这是我在去年遇到的最有价值的网页。 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> 

我的清单文件看起来是这样的:在DetailActivity

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

代码:

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