Skip to content

Instantly share code, notes, and snippets.

@brunoportess
Created August 31, 2017 21:19
Show Gist options
  • Save brunoportess/d4c8eba155f812d57d102b62eb87a095 to your computer and use it in GitHub Desktop.
Save brunoportess/d4c8eba155f812d57d102b62eb87a095 to your computer and use it in GitHub Desktop.
Exemplo de BroadcastReceiver
[Activity(Label = "Cronometro", MainLauncher = true)]
public class MainActivity : Activity
{
Receiver1 receiver;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
receiver = new Receiver1();
RegisterReceiver(receiver, new IntentFilter("com.xamarin.example.TEST"));
Intent intent = new Intent("com.xamarin.example.TEST");
intent.PutExtra("recado", "Funcionou comigo!");
SendBroadcast(intent);
}
protected override void OnResume()
{
base.OnResume();
RegisterReceiver(receiver, new IntentFilter("com.xamarin.example.TEST"));
// Code omitted for clarity
}
}
[BroadcastReceiver]
[IntentFilter(new[] { "com.xamarin.example.TEST" })]
public class Receiver1 : BroadcastReceiver
{
public override void OnReceive(Context context, Intent intent)
{
string recado = intent.GetStringExtra("recado");
Toast.MakeText(context, "Received intent! RECADO: "+recado, ToastLength.Short).Show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment