Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive URLs with Xamarin Intent Filters

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";
    }
}

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

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);
        }
     }
like image 380
mason Avatar asked Jun 16 '14 04:06

mason


2 Answers

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 ();
            }
        }
    }
like image 108
mason Avatar answered Nov 04 '22 05:11

mason


Why do you think that the url is in MyData? That is not where ActionSend is putting the data into the extras.

According to the Android docs ActionSend supplies the data in android.intent.extra.TEXT through the Intent.ExtraText. So:

var text = Intent.GetStringExtra(Intent.ExtraText);

Also the two categories you have provided are defined wrongly in your IntentFilter. It should look like:

[IntentFilter(new[] { Intent.ActionSend }, 
    Categories = new[] { Intent.CategoryBrowsable, Intent.CategoryDefault })]
like image 30
Cheesebaron Avatar answered Nov 04 '22 06:11

Cheesebaron