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