c# - 如何使用 Xamarin Intent 过滤器接收 URL

标签 c# android xamarin xamarin.android

我正在尝试使用 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/

相关文章:

android - 为什么我没有收到已安装的应用程序广播?

c# - 如何选择多张照片?

c# - 你如何从 xamarin 中的绝对 NSURL 加载文件

c# - 创建 Controller 时的可测试性考虑

C# FileNotFoundException 未被用户代码处理

android - 如何使用通配符域实现 Android 应用程序链接?

android - Android Q 中的 Landroid/view/LayoutInflater 类中没有字段 mConstructorArgs

c# - 命名使用 Log4net 生成的文件

c# - 分区的主实例或无状态实例在辅助副本上的地址无效

java - 使用 n-gram 在 Java 中预测下一个单词的理想数据结构