如何使用Xamarin意向过滤器接收URL [英] How to receive URLs with Xamarin Intent Filters

查看:64
本文介绍了如何使用Xamarin意向过滤器接收URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Xamarin for Android编写一个可以与URL共享的简单活动(例如,Chrome可以为我的活动共享URL).

I am trying to write a simple activity with Xamarin for Android that URL's can be shared to (for example, Chrome could share a URL to my activity).

这是到目前为止我得到的:

Here's what I've got so far:

[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的列表中.我想念什么吗?

Unfortunately, my app doesn't show up in Chrome's list when I try to Share. And I missing something?

将更新的代码编辑为我在下面发布的内容.当我从Chrome转到共享"时,仍然没有显示为目标.

Edit, updated code to what I've posted below. Still doesn't show up as a target when I go to Share from 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.

Figured it out. The main part I was missing was the 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 ();
            }
        }
    }

这篇关于如何使用Xamarin意向过滤器接收URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆