2014-06-16 71 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