2012-05-16 140 views
0

採取uri數據嗨,我必須做android應用程序和瀏覽器之間的連接。所以,當點擊瀏覽器上的按鈕時,它應該重定向到android應用程序。在Android的活動我已經寫通過意圖在android

Uri data = getIntent().getData(); 
     if (data.equals(null)) { 
      System.out.println("Data is null"); 
     } else { 
      String scheme = data.getScheme(); 
      System.out.println(scheme); 
      String host = data.getHost(); 
      int port = data.getPort(); 
      List<String> params = data.getPathSegments(); 
      String first = params.get(0); // "hello" 
      System.out.println(first); 

和表現我已經在按鈕的HTML已經給

<intent-filter> 
       <data android:scheme="Integration" /> 
       <action android:name="android.intent.action.VIEW" /> 
       <category android:name="android.intent.category.BROWSABLE" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
      </intent-filter> 

點擊<FORM METHOD="LINK" ACTION="Integration://1">

它扔indexoutofboundexception我已經給了。 請告訴我哪裏是錯誤

更新 *我是不必要的使用意圖在活動。通過在html5中刪除n參數,我的應用程序現在可以運行成功。 *

回答

1

從引用答案:How to listen for a custom URI

要在您的Android應用程序註冊一個協議,一個額外的塊添加到AndroidManifest.xml中

我修改了代碼一點,但想到我也會引用來源

<manifest> 
<application> 
    <activity android:name=".activityToCall"> 
      <intent-filter> 
       <action android:name="android.intent.action.VIEW" /> 
       <category android:name="android.intent.category.DEFAULT" /> 
       <category android:name="android.intent.category.BROWSABLE" /> 
       <data android:scheme="scheme" android:host="path"/> 
      </intent-filter> 
    </activity> 
</application> 
</manifest> 

然後,當與您的模式匹配的網址打開後,您的應用程序將呼籲處理它我承擔。 考慮到schemepath對應於此:

scheme://host/path 

之後,在您在清單設置爲處理這些東西的活動:

Uri data = getIntent().getData(); 
if (!data.equals(null)){ 
    String scheme = data.getScheme(); 
    //Or whatever you needed 
} 
+0

我更新的代碼。它給予索引超出限制的例外。我沒有y – Naina

+1

'params.get(0)'沒有參數意味着你不能得到第一個參數,因此'inderOutOfBounds' –

+0

好的。但在HTML ACTION =「Integration:// h \t」>我傳遞參數「h」並在活動代碼中,我正在採取String first = params.get(0);.如果它不起作用,那麼我可以單獨編寫ACTION =「Integration://」> – Naina