0

i want to have alarm set once a day in 20:00 o'clock, when i debug the application in the emulator everything works fine, but in my phone sometimes it works and sometimes not. this is my code

 public class AlarmUtility
{
    private static TimeSpan AlarmTime = new TimeSpan(20, 00, 0);
    private static long CurrentAlarm;

    public static void StartAlarm()
    {          
        AlarmManager manager = (AlarmManager)Application.Context.GetSystemService(Context.AlarmService);
        Intent myIntent;
        PendingIntent pendingIntent;
        myIntent = new Intent(Application.Context, typeof(DailyNotificationReciever));
        pendingIntent = PendingIntent.GetBroadcast(Application.Context, 0, myIntent, PendingIntentFlags.UpdateCurrent);

        long date;

        //check if alarm date has passed
        if(DateTime.Now.TimeOfDay < AlarmTime)
        {
            date = getAlarmJavaTime(false);
        }
        else
        {
            date = getAlarmJavaTime(true);
        }

        if (CurrentAlarm != date)
        {
            manager.SetInexactRepeating(AlarmType.RtcWakeup, date, AlarmManager.IntervalDay, pendingIntent);
            CurrentAlarm = date;
        }

    }
   private static long getAlarmJavaTime(bool addDay)
    {
        Calendar cal = Calendar.Instance;
        cal.TimeInMillis = Java.Lang.JavaSystem.CurrentTimeMillis();
        cal.Set(CalendarField.HourOfDay, AlarmTime.Hours);
        cal.Set(CalendarField.Minute, AlarmTime.Minutes);
        cal.Set(CalendarField.Second, AlarmTime.Seconds);
        if (addDay)
        {
            cal.Add(CalendarField.DayOfMonth, 1);
        }

        SimpleDateFormat dateFormat = new SimpleDateFormat("EEE MMM dd hh:mm:ss 'GMT'Z yyyy");
        string a = dateFormat.Format(cal.Time);
        return cal.TimeInMillis;
    }       
}

am i doing something wrong? maybe the alarm doesn't set when the app in not active ? update my DailyNotificationReciever code as requested

[BroadcastReceiver(Enabled = true)]
public class DailyNotificationReciever : BroadcastReceiver
{
    public override void OnReceive(Context context, Intent intent)
    {
        Toast.MakeText(context, "DailyNotificationReciever", ToastLength.Long).Show();

         //create nofification id
        Random rnd = new Random();
        int notificationNumber = rnd.Next(1000);

        //create the intents
        //user click yes
        Intent IntentYes = new Intent("UserClickedYes");

        PendingIntent pendingIntentYes = PendingIntent.GetBroadcast(context, (int)DateTime.Now.Ticks, IntentYes, PendingIntentFlags.CancelCurrent);

        //user click no
        Intent IntentNo = new Intent("UserClickedNo");

        PendingIntent pendingIntentNo = PendingIntent.GetBroadcast(context, (int)DateTime.Now.Ticks, IntentNo, PendingIntentFlags.CancelCurrent);



        NotificationCompat.Builder builder = new NotificationCompat.Builder(context);

        builder.SetAutoCancel(true)
            .SetDefaults((int)NotificationDefaults.All)                
            .SetContentTitle("test")
            .SetContentText("test")
            .SetAutoCancel(true)                
            .AddAction(Resource.Drawable.Test_32px, "Yes", pendingIntentYes)
            .AddAction(Resource.Drawable.Test_32px, "No", pendingIntentNo);


        NotificationManager manager = (NotificationManager)context.GetSystemService(Context.NotificationService);
        manager.Notify(
            notificationNumber, builder.Build());
    }       
}

Thanks

royi
  • 49
  • 1
  • 6

2 Answers2

1

Since the Broadcast Receiver is one component of your application, if the app is stopped, it won't receive any alarm or events. This is the possible reason why your code sometimes works and sometimes not, besides, just on code level, I didn't find any problem with your code.

If you want to check this possibility, you may use the following adb command:

adb shell dumpsys package "com.package.name" and check "stopped=true" 

to check if the application is in the stopped state.

If it is confirmed, I can only say, this behavior is by design.

Grace Feng
  • 16,564
  • 2
  • 22
  • 45
  • Hi, from what you are saying, maybe replacing the Broadcast Receiver with service will solve the problem – royi Oct 26 '17 at 07:55
  • @royi, not that simple, if your problem is to keep your application alive in background, there should be a lot of blogs and so on, you may search for it. Finally, if you find this answer helpful, could you please mark this answer? Thx! – Grace Feng Oct 26 '17 at 08:08
0

There are a few things that you should know:

1) All alarms are clear after device reboot and you have to schedule its again: does Alarm Manager persist even after reboot?

2) If you schedule alarm with the same PendingIntent (as you do - the same requestCode, empty Intent and the same flags) this alarm will overwrite previous one.

Try to analyze your problem based on above informations.

Leśniakiewicz
  • 874
  • 1
  • 10
  • 21