/C2dmSharp.Client.Sample/DefaultActivity.cs

http://github.com/Redth/C2DM-Sharp · C# · 105 lines · 78 code · 19 blank · 8 comment · 2 complexity · 2ffe72faca887ef0d33d1c434f744e7c MD5 · raw file

  1. using System;
  2. using System.Text;
  3. using Android.App;
  4. using Android.Content;
  5. using Android.Runtime;
  6. using Android.Views;
  7. using Android.Widget;
  8. using Android.OS;
  9. using Android.Util;
  10. namespace C2dmSharp.Client.Sample
  11. {
  12. [Activity(Label = "C2DM-Sharp Sample", MainLauncher = true,
  13. LaunchMode= Android.Content.PM.LaunchMode.SingleTask)]
  14. public class DefaultActivity : Activity
  15. {
  16. //NOTE: You need to put your own email here!
  17. // Whichever one you registered as the 'Role' email with google
  18. public const string senderIdEmail = "redthc2dm@gmail.com";
  19. TextView textRegistrationStatus = null;
  20. TextView textRegistrationId = null;
  21. TextView textLastMsg = null;
  22. Button buttonRegister = null;
  23. bool registered = false;
  24. protected override void OnCreate(Bundle bundle)
  25. {
  26. base.OnCreate(bundle);
  27. // Set our view from the "main" layout resource
  28. SetContentView(Resource.Layout.main);
  29. textRegistrationStatus = FindViewById<TextView>(Resource.Id.textRegistrationStatus);
  30. textRegistrationId = FindViewById<TextView>(Resource.Id.textRegistrationId);
  31. textLastMsg = FindViewById<TextView>(Resource.Id.textLastMessage);
  32. buttonRegister = FindViewById<Button>(Resource.Id.buttonRegister);
  33. Log.Info("C2DM-Sharp-UI", "Hello World");
  34. this.buttonRegister.Click += delegate
  35. {
  36. if (!registered)
  37. {
  38. Log.Info("C2DM-Sharp", "Registering...");
  39. C2dmSharp.Client.C2dmClient.Register(this, senderIdEmail);
  40. }
  41. else
  42. {
  43. Log.Info("C2DM-Sharp", "Unregistering...");
  44. C2dmSharp.Client.C2dmClient.Unregister(this);
  45. }
  46. RunOnUiThread(() =>
  47. {
  48. //Disable the button so that we can't click it again
  49. //until we get back to the activity from a notification
  50. this.buttonRegister.Enabled = false;
  51. });
  52. };
  53. }
  54. protected override void OnResume()
  55. {
  56. base.OnResume();
  57. updateView();
  58. }
  59. void updateView()
  60. {
  61. //Get the stored latest registration id
  62. var registrationId = C2dmClient.GetRegistrationId(this);
  63. //If it's empty, we need to register
  64. if (string.IsNullOrEmpty(registrationId))
  65. {
  66. registered = false;
  67. this.textRegistrationStatus.Text = "Registered: No";
  68. this.textRegistrationId.Text = "Id: N/A";
  69. this.buttonRegister.Text = "Register...";
  70. Log.Info("C2DM-Sharp", "Not registered...");
  71. }
  72. else
  73. {
  74. registered = true;
  75. this.textRegistrationStatus.Text = "Registered: Yes";
  76. this.textRegistrationId.Text = "Id: " + registrationId;
  77. this.buttonRegister.Text = "Unregister...";
  78. Log.Info("C2DM-Sharp", "Already Registered: " + registrationId);
  79. }
  80. var prefs = GetSharedPreferences("c2dm.client.sample", FileCreationMode.Private);
  81. this.textLastMsg.Text = "Last Msg: " + prefs.GetString("last_msg", "N/A");
  82. //Enable the button as it was normally disabled
  83. this.buttonRegister.Enabled = true;
  84. }
  85. }
  86. }