-1

I'm making an android app and I want to activate an alarm every 10 minutes.

The problem is that the alarm is activated only one time.

Here is the code who launch the BroadcastReceiver :

public void CallAlarmReceiver(bool isRepeating, bool destroy)
    {

        Intent i = new Intent(this, typeof(AlarmReceiver));
        i.SetAction("android.intent.action.NOTIFY");

        PendingIntent pi = PendingIntent.GetBroadcast(this, 0, i, PendingIntentFlags.UpdateCurrent);

        AlarmManager alarmManager = this.GetSystemService(Context.AlarmService) as AlarmManager;

        AlarmManagerCompat.SetExactAndAllowWhileIdle(alarmManager, (int)AlarmType.RtcWakeup, (long)(Calendar.GetInstance(Android.Icu.Util.TimeZone.Default).TimeInMillis + TimeSpan.FromSeconds(10).TotalMilliseconds), pi);
    }

2 Answers2

0

when broadcast received. call this method again.

Abdul Rafay
  • 131
  • 5
0

A easy way is to use a timer.

Device.StartTimer(TimeSpan.FromSeconds(10), () =>
{
    // Do something

    return true; // True = Repeat again, False = Stop the timer
});
Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17
  • You could use the Android Lifecyle to catch the app operations. https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/activity-lifecycle/ – Wendy Zang - MSFT Jul 29 '21 at 05:57