0

On Android Marshmallow service or background process be paused/hibernated and one way is to setup an alarm manager timer to wake up the service every 09 minutes. If I setup the Alarm from the activity it will only be called once. How do I setup a repeating SetAndAllowWhileIdle alarm? Thanks

 AlarmManager manager = (AlarmManager)GetSystemService(AlarmService);
 long triggerAtTime = SystemClock.ElapsedRealtime() + (9 * 60 * 1000);
 Intent alarmintent = new Intent(this, typeof(AlarmReceiver));
 PendingIntent pendingintent = PendingIntent.GetBroadcast(this, 0, alarmintent, 0);
 if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.M)
 {
      manager.Cancel(pendingintent);
      manager.SetAndAllowWhileIdle(AlarmType.ElapsedRealtimeWakeup, triggerAtTime, pendingintent);   
 }
 else
 {
      manager.Set(AlarmType.ElapsedRealtimeWakeup, triggerAtTime, pendingintent);
 }
Tom11
  • 2,419
  • 8
  • 30
  • 56
AlbertK
  • 429
  • 2
  • 8
  • 21
  • There is no support for repeated alarms in combination with an idling device. Possible duplicate of: http://stackoverflow.com/questions/35629268/alarm-manager-issue-in-android-6-0-doze-mode – Krumelur Aug 11 '16 at 06:51

2 Answers2

0

Create a sticky service in which you can define your AlarmManager. This service would be call by another AlarmManager by that the alarm will rescheduled after 9 mins.

Sunny
  • 1,504
  • 14
  • 22
0

I have done something like this. It seems to work but is it the right way to go about it?. It is sort of a recursive call and will it leak memory.

Service

namespace MyApp
{
    [Service(Name = "com.MyApp.simpleservice", Enabled = true, Process = ":bgservice") ]
    public class SimpleService : Service
    {
        private const string TAG = "MyApp.simpleservice";


        public override void OnCreate()
        {
            base.OnCreate();

        }


        public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
        {


            AlarmManager manager = (AlarmManager)GetSystemService(AlarmService);
            long triggerAtTime = SystemClock.ElapsedRealtime() + (5 * 60 * 1000);
            Intent alarmintent = new Intent(this, typeof(AlarmReceiver));
            PendingIntent pendingintent = PendingIntent.GetBroadcast(this, 0, alarmintent, 0);
            if (Android.OS.Build.VERSION.SdkInt >= BuildVersionCodes.M)
            {

                manager.Cancel(pendingintent);
                manager.SetAndAllowWhileIdle(AlarmType.ElapsedRealtimeWakeup, triggerAtTime, pendingintent);
            }
            else
            {
                manager.Set(AlarmType.ElapsedRealtimeWakeup, triggerAtTime, pendingintent);
            }

        DoWork();

            return StartCommandResult.Sticky;
        }

     public void DoWork()
     {
        //Do Some Serious Work here
     }
     public override IBinder OnBind(Intent intent)
         {
            // This example isn't of a bound service, so we just return NULL.
            return null;
        }
}

BroadCastReceiver

namespace MyApp
{
    [BroadcastReceiver(Enabled = true) ]
    class AlarmReceiver : BroadcastReceiver

    {
        public override void OnReceive(Context context, Intent intent)
        {
            Intent rintent = new Intent(context, typeof(SimpleService));

            rintent.PutExtra("AlarmReceiver","Broadcast Alarm");
            context.StartService(rintent);
        }
    }
}
AlbertK
  • 429
  • 2
  • 8
  • 21