Created
August 31, 2017 21:19
-
-
Save brunoportess/d4c8eba155f812d57d102b62eb87a095 to your computer and use it in GitHub Desktop.
Exemplo de BroadcastReceiver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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