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);
}
}
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 ();
}
}
}
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 })]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With