Skip to content

Instantly share code, notes, and snippets.

@donaldsteele
Last active August 19, 2017 14:05
Show Gist options
  • Save donaldsteele/62fedda23c5cf8c26c4d8a0476d3741a to your computer and use it in GitHub Desktop.
Save donaldsteele/62fedda23c5cf8c26c4d8a0476d3741a to your computer and use it in GitHub Desktop.
Xamarin example to receive url or text from other applications. For example when you are on the youtube app and click share.
using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content;
using System.Text;
using System;
namespace AndroidDataReceiver
{
[Activity(Label = "AndroidDataReceiver", MainLauncher = true, LaunchMode = Android.Content.PM.LaunchMode.SingleTop )]
[BroadcastReceiver]
[IntentFilter(new[] { Intent.ActionSend }, Categories = new[] {
Intent.CategoryDefault,
Intent.CategoryBrowsable
}, DataMimeType = "text/plain")]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
if (!String.IsNullOrEmpty(Intent.GetStringExtra(Intent.ExtraText)))
{
string url = Intent.GetStringExtra(Intent.ExtraText) ?? "url not available";
Toast.MakeText(this, url, ToastLength.Long).Show();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment