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