2014-06-16 16 views
5

我想用Xamarin for Android編寫一個簡單的活動,該URL可以共享給(例如,Chrome可以共享我的活動的URL)。如何使用Xamarin Intent過濾器接收URL

這裏是我到目前爲止有:

[Activity (Label = "LinkToDesktop", MainLauncher = true)] 
[IntentFilter (new[] { 
    Intent.ActionSend, 
    Intent.CategoryBrowsable, 
    Intent.CategoryDefault, 
    })] 

public class MainActivity : Activity 
{ 
    protected override void OnCreate (Bundle bundle) 
    { 
     base.OnCreate (bundle); 
     string text = Intent.GetStringExtra ("MyData") ?? "Data not available"; 
    } 
} 

不幸的是,我的應用程序不會在Chrome的列表中顯示出來,當我試圖共享。我錯過了什麼?

編輯,更新後的代碼,我以下發布。當我從Chrome瀏覽器共享時,仍然不會顯示爲目標。

[Activity (Label = "LinkToDesktop", MainLauncher = true)] 
    [IntentFilter (new[] { Intent.ActionSend }, 
    Categories = new[] { Intent.CategoryBrowsable, Intent.CategoryDefault }) 
] 
public class MainActivity : Activity 
    { 
    protected override void OnCreate (Bundle bundle) 
     { 
     base.OnCreate (bundle); 
     string text = Intent.GetStringExtra ("MyData") ?? "Data not available"; 
     } 

    protected override void OnNewIntent (Intent intent) 
     { 
     base.OnNewIntent (intent); 
     } 
    } 

回答

7

想通了。我缺少的主要部分是DataMimeType。

[Activity (Label = "LinkToDesktop", MainLauncher = true)] 
[IntentFilter (new[] { Intent.ActionSend }, Categories = new[] { 
    Intent.CategoryDefault, 
    Intent.CategoryBrowsable 
}, DataMimeType = "text/plain")] 
public class MainActivity : Activity 
    { 
    protected override void OnCreate (Bundle bundle) 
     { 
     base.OnCreate (bundle); 
     if (!String.IsNullOrEmpty (Intent.GetStringExtra (Intent.ExtraText))) 
      { 
      string subject = Intent.GetStringExtra (Intent.ExtraSubject) ?? "subject not available"; 
      Toast.MakeText (this, subject, ToastLength.Long).Show(); 
      } 
     } 
    } 
1

爲什麼你認爲該網址在MyData?這不是ActionSend將數據放入臨時演員的地方。

根據Android文檔ActionSend通過Intent.ExtraText提供android.intent.extra.TEXT中的數據。所以:

var text = Intent.GetStringExtra(Intent.ExtraText); 

而且你提供的是你的錯IntentFilter定義的兩大類。它應該看起來像:

[IntentFilter(new[] { Intent.ActionSend }, 
    Categories = new[] { Intent.CategoryBrowsable, Intent.CategoryDefault })] 
+0

我並不擔心MyData,因爲它甚至沒有達到。 – mason

+0

與修正的'IntentFilter'是否達到?我知道用NFC你需要在OnNewIntent方法中獲取數據,所以試着重寫一下,看看這裏是否也是這種情況。 – Cheesebaron

+0

不,仍然不起作用。我用我試過的代碼更新了我的問題。 – mason