0

I am developing an app built on this example: https://github.com/xamarin/mobile-samples/tree/master/BackgroundLocationDemo

The example works and the location updates are coming in as expected. However, Android keeps showing an notification that the service is running and draining battery. Now, all my users have a defined working schedule (list of Start to End DateTime per day e.g 8am-1pm, 4pm-8pm), and I want that the service is only running between those working times. This means that I need to start/stop the service whenever the schedule says the user is working or not.

I've asked this question before but wondering if anyone figured out an efficient and solid way to achieve this type of service that is operating from a time schedule?

ArturM
  • 171
  • 1
  • 11

1 Answers1

2

You can use AlarmManager to execute a task in specific time.

For example, I want my task running at the 10:51 am every day, I can use following code to execute it.

 public static void startAlarmBroadcastReceiver(Context context)
        {
            Intent _intent = new Intent(context, typeof( AlarmBroadcastReceiver));
            PendingIntent pendingIntent = PendingIntent.GetBroadcast(context, 0, _intent, 0);
            AlarmManager alarmManager = (AlarmManager)context.GetSystemService(Context.AlarmService);
            // Remove any previous pending intent.
            alarmManager.Cancel(pendingIntent);

            Calendar cal = Calendar.Instance;
            cal.Set( CalendarField.HourOfDay, 10);
            cal.Set(CalendarField.Minute, 51);
            cal.Set(CalendarField.Second, 0);

            alarmManager.SetRepeating(AlarmType.RtcWakeup, cal.TimeInMillis, AlarmManager.IntervalDay, pendingIntent);


       }

Here is code about AlarmBroadcastReceiver.

    [BroadcastReceiver(Enabled = true, Exported = false)]
    public class AlarmBroadcastReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
        }
    }

Do not forget to add following permissions.

<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

Here is running gif. enter image description here

Leon
  • 8,404
  • 2
  • 9
  • 52
  • Thanks for great reply and example Leon ! This is the most straightforward way to do it, but what if I have a more complex schedule? e.g. 8am-2pm and 4pm-8pm on Mondays and 9am-10am and 2pm-6pm on Tuesday? – ArturM Apr 22 '20 at 07:02
  • If you want to add te week day in `Calendar`, you can refer to this thread. https://stackoverflow.com/questions/19051622/set-calendar-to-next-thursday – Leon Apr 22 '20 at 09:58
  • The problem is not to calculate the weekday or times, but to input it to SetRepeating call. As I understand what I want to do cant be done with the SetRepeating function – ArturM Apr 22 '20 at 12:43
  • Is you application always running in foreground? – Leon Apr 23 '20 at 06:53
  • Yes, its a foreground service. The point is to receive location updates from the user during their work hours. – ArturM Apr 23 '20 at 08:50
  • Any tips of how to achieve this @Leon Lu? – ArturM Apr 28 '20 at 11:05
  • More tips? Would appreciate it – ArturM May 04 '20 at 21:33
  • @Leon Lu Is there a way to get this to run as a background service? If I schedule an alarm, and then close the app, it doesn't trigger it. – AdamMasters May 06 '20 at 16:38
  • Actually I take that back. It is working even closed. Maybe my emulator was having issues. I installed the app on my physical phone and it worked perfectly. Thanks! – AdamMasters May 06 '20 at 17:50