我正在尝试使用 Xamarin for Android 编写一个简单的 Activity ,URL 可以共享到(例如,Chrome 可以共享我的 Activity 的 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 ();
}
}
}
关于c# - 如何使用 Xamarin Intent 过滤器接收 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24236701/