public override void OnDestroy() { // We need to shut things down. Log.Debug(TAG, GetFormattedTimestamp() ?? "The TimeStamper has been disposed."); Log.Info(TAG, "OnDestroy: The started service is shutting down."); // Stop the handler. handler.RemoveCallbacks(runnable); // Remove the notification from the status bar. var notificationManager = (NotificationManager)GetSystemService(NotificationService); notificationManager.Cancel(Constants.SERVICE_RUNNING_NOTIFICATION_ID); timestamper = null; isStarted = false; base.OnDestroy(); }
public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId) { if (intent.Action.Equals(Constants.ACTION_START_SERVICE)) { if (isStarted) { Log.Info(TAG, "OnStartCommand: The service is already running."); } else { Log.Info(TAG, "OnStartCommand: The service is starting."); RegisterForegroundService(); handler.PostDelayed(runnable, Constants.DELAY_BETWEEN_LOG_MESSAGES); isStarted = true; _smsSentBroadcastReceiver = new SMSSentReceiver(); _smsDeliveredBroadcastReceiver = new SMSDeliveredReceiver(); RegisterReceiver(_smsSentBroadcastReceiver, new IntentFilter("SMS_SENT")); RegisterReceiver(_smsDeliveredBroadcastReceiver, new IntentFilter("SMS_DELIVERED")); } } else if (intent.Action.Equals(Constants.ACTION_STOP_SERVICE)) { Log.Info(TAG, "OnStartCommand: The service is stopping."); timestamper = null; StopForeground(true); StopSelf(); isStarted = false; UnregisterReceiver(_smsSentBroadcastReceiver); UnregisterReceiver(_smsDeliveredBroadcastReceiver); } else if (intent.Action.Equals(Constants.ACTION_RESTART_TIMER)) { Log.Info(TAG, "OnStartCommand: Restarting the timer."); timestamper.Restart(); } // This tells Android not to restart the service if it is killed to reclaim resources. return(StartCommandResult.Sticky); }
public override void OnCreate() { base.OnCreate(); Log.Info(TAG, "OnCreate: the service is initializing."); timestamper = new UtcTimestamper(); handler = new Handler(); runnable = new Action(async() => { if (timestamper == null) { Log.Wtf(TAG, "Why isn't there a Timestamper initialized?"); } else { // TODO get all the pending sms, then send the sms, then update var items = new List <ShortMessage>(); //var url = "http://104.215.158.168/catering/"; var url = "http://batangas.southeastasia.cloudapp.azure.com/catering/"; using (var client = new HttpClient()) { // send a GET request var uri = $"{url}api/message/forSend"; var result = await client.GetStringAsync(uri); items.AddRange(JsonConvert.DeserializeObject <List <ShortMessage> >(result)); } using (var post = new HttpClient(new NativeMessageHandler())) { foreach (var item in items.Where(p => p.DateSent == null)) { try { // SEND SMS var piSent = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_SENT"), 0); var piDelivered = PendingIntent.GetBroadcast(this, 0, new Intent("SMS_DELIVERED"), 0); _smsManager.SendTextMessage(item.Receiver, null, item.Body, piSent, piDelivered); var uri = $"{url}api/message/sent2/?id={item.ShortMessageId}"; var resp = await post.GetAsync(new Uri(uri)); //resp.EnsureSuccessStatusCode(); Log.Debug(TAG, item.Body); } catch (Exception ex) { Log.Error(TAG, ex.ToString()); } } } string msg = timestamper.GetFormattedTimestamp(); Log.Debug(TAG, msg); Intent i = new Intent(Constants.NOTIFICATION_BROADCAST_ACTION); i.PutExtra(Constants.BROADCAST_MESSAGE_KEY, msg); Android.Support.V4.Content.LocalBroadcastManager.GetInstance(this).SendBroadcast(i); handler.PostDelayed(runnable, Constants.DELAY_BETWEEN_LOG_MESSAGES); } }); }