mas*_*son 6 c# android xamarin.android xamarin
我正在尝试使用Xamarin for 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";
}
}
Run Code Online (Sandbox Code Playgroud)
不幸的是,当我尝试分享时,我的应用不会显示在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);
}
}
Run Code Online (Sandbox Code Playgroud)
弄清楚了.我缺少的主要部分是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 ();
}
}
}
Run Code Online (Sandbox Code Playgroud)