如何使用Xamarin意图过滤器接收url
本文关键字:url 过滤器 意图 何使用 Xamarin | 更新日期: 2023-09-27 18:08:40
我正试图用Xamarin为Android编写一个简单的活动,URL可以共享到(例如,Chrome可以共享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);
}
}
明白了。我缺少的主要部分是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 ();
}
}
}
为什么您认为url在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 })]