2012-02-19 72 views
1

我正在構建一個應用程序來搜索種子。所以當用戶選擇一個時,我想打開用戶安裝的客戶端(典型的基於意圖的活動)。Android意圖解析

在我的測試機,我已經transdroid安裝。 Transdroid宣佈,它打算是這樣的: http://code.google.com/p/transdroid/source/browse/android/AndroidManifest.xml

我開始練習這種方式:

final Intent intent = new Intent(Intent.ACTION_VIEW).setData(Uri.parse(url)); 
    activity.startActivity(intent); 

雖然我認爲這應該工作,在瀏覽器中得到,而不是推出。該URL是一個直接的URL,以「torrent」結尾,因此它應該與Transdroid的過濾器相匹配。

我也試過用setDataAndType但type="application/x-bittorrent"我得到一個異常「沒有找到處理這個意圖的活動」。

我知道Transdroid正確安裝,因爲之後的torrent文件被瀏覽器下載,我可以點擊它,打開Transdroid。

+0

這裏有幾個問題的答案,可以認爲是正確的。你可能遇到了一個問題是,[這](http://code.google.com/p/transdroid/issues/detail?id=453&colspec=ID%20Type%20Status%20Priority%20Component%20Owner%20Summary%20Reporter%20Stars )。 Transdroid本身沒有正確定義它的意圖過濾器,因此所有的洪流鏈接都未能打開它。 – 2013-01-28 16:18:26

回答

0

我試過代碼螞蟻的一些作品這個工作對我來說(即它lauches transdroid):

Intent i = new Intent(Intent.ACTION_VIEW); 
i.addCategory(Intent.CATEGORY_DEFAULT); 
i.setData(Uri.parse("http://releases.ubuntu.com/lucid/ubuntu-10.04.3-alternate-amd64.iso.torrent")); 
i.setType("application/x-bittorrent"); 

我不知道,但行爲是非常奇怪的。舉例來說,如果我改變只有兩行,然後它會啓動瀏覽器:

Intent i = new Intent(Intent.ACTION_VIEW); 
i.addCategory(Intent.CATEGORY_DEFAULT); 
i.setType("application/x-bittorrent"); 
i.setData(Uri.parse("http://releases.ubuntu.com/lucid/ubuntu-10.04.3-alternate-amd64.iso.torrent")); 
+0

謝謝你的回答。事實上,它啓動了transdroid,但有兩個問題:(1)它沒有將洪流添加到列表中;(2)如果我刪除了Transdroid,它會拋出「找不到活動」異常。我真正想要的是讓用戶在應用程序之間進行選擇,並且如果他沒有任何洪流客戶端回落到瀏覽器(也許我必須手動執行此回退)。 – erwan 2012-02-19 18:19:18

0
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://site.com/tor.torrent"))); 

注意,如果您鏈​​接到需要的任何文件具有的.torrent擴展名或是MIME類型application/x軸的BitTorrent的。否則,Android的意圖機制無法與Transdroid的Intent相匹配,而是會啓動瀏覽器。如果你使用瀏覽器下載文件並「打開」它,它將用Transdroid打開。

如果這不起作用,procide我們洪流不工作的一個例子URL。

如果你想趕上那Transdorid尚未安裝的情況下,你可以這樣做:

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://site.com/tor.torrent"); 
if (context.getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY) > 0) { 
    startActivity(i); 
} else { 
    // Alert that Transdroid is not available and link to the installer 
} 

或者使用意向選擇器:

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("http://site.com/tor.torrent"); 
startActivity(Intent.createChooser(i, getString(R.string.some_dialog_title))); 
0

於Android resurces可以找到一種方法來檢查一個意圖是否存在。

public static boolean isIntentAvailable(Context context, String action) { 
    final PackageManager packageManager = context.getPackageManager(); 
    final Intent intent = new Intent(action); 
    List<ResolveInfo> list = packageManager.queryIntentActivities(intent, 
      PackageManager.MATCH_DEFAULT_ONLY); 
    return list.size() > 0; 
} 

這裏有一個示例如何使用條碼掃描儀的意圖。

@Override 
public boolean onPrepareOptionsMenu(Menu menu) { 
    final boolean scanAvailable = isIntentAvailable(this, 
      "com.google.zxing.client.android.SCAN"); 

    MenuItem item; item = menu.findItem(R.id.menu_item_add); 
    item.setEnabled(scanAvailable); 

    return super.onPrepareOptionsMenu(menu); 
}