locked
Alarm Manager RRS feed

  • Question

  • User9253 posted

    I am trying to implement a notification to fire at a fixed point in the future, but I am having difficulties getting it working right. It fires immediately rather than at the specified time in the future. Below is my receiver class. I am calling SetAlarm from another class and passing a time interval, which is alertTime. Any thoughts? Thanks!!!

    using System.Linq; using System.Text; using Android.App; using Android.Content; using Android.OS; using Android.Runtime; using Android.Views; using Android.Widget; using Android.Util;

    namespace Test_Android { [BroadcastReceiver] class NotificationAlertReceiver : BroadcastReceiver {

        public NotificationAlertReceiver()
        {
    
        }
    
        public override void OnReceive(Context context, Intent intent)
        {
            PowerManager pm = (PowerManager)context.GetSystemService(Context.PowerService);
            PowerManager.WakeLock w1 = pm.NewWakeLock (WakeLockFlags.Partial, "NotificationReceiver");
            w1.Acquire ();
            var nMgr = (NotificationManager)context.GetSystemService (Context.NotificationService);
            var notification = new Notification (Resource.Drawable.loadIcon, "Arrival");
            var pendingIntent = PendingIntent.GetActivity (context, 0, new Intent (context, typeof(MainActivity)), 0);
            notification.SetLatestEventInfo (context, "Arrival", "Arrival", pendingIntent);
            nMgr.Notify (0, notification);
            w1.Release ();
        }
    
        public void CancelAlarm(Context context){ 
            Intent intent = new Intent(context, this.Class);
            PendingIntent sender = PendingIntent.GetBroadcast (context, 0, intent, 0);
            AlarmManager alarmManager = (AlarmManager) context.GetSystemService (Context.AlarmService);
            alarmManager.Cancel (sender);
        }
    
        public void SetAlarm(Context context, int alertTime){
            long now = SystemClock.CurrentThreadTimeMillis();
            AlarmManager am = (AlarmManager) context.GetSystemService (Context.AlarmService);
            Intent intent = new Intent(context, this.Class);
            PendingIntent pi = PendingIntent.GetBroadcast (context, 0, intent, 0);
            am.Set (AlarmType.RtcWakeup, now + ((long)(alertTime*10000)), pi);
        }
    }
    

    }

    Thursday, August 29, 2013 4:52 PM

All replies

  • User188 posted

    I think it's because you're using AlarmType.RtcWakeup, which is UTC time, with SystemClock.CurrentThreadTimeMillis(), which is the time elapsed in the current thread. The time elapsed, when converted to UTC, will be very far in the past so the alarm goes off immediately.

    If you use AlarmType.ElapsedRealtimeWakeup with SystemClock.ElapsedRealtime() it should work correctly.

    Tuesday, September 10, 2013 8:37 PM
  • User9253 posted

    This was absolutely right, Thanks!

    Thursday, September 12, 2013 3:26 PM
  • User46127 posted

    hello, im trying to do the same thing too. how i can convert a datetime variable to the int alerttime? my notification it may be after 1 year, is this the correct approach?

    Tuesday, July 15, 2014 12:20 AM