Search code examples
androidgpsandroid-servicealarmmanagerschedule

Schedule a GPS location check daily at a predefined time in Android


I'm from an iOS background with intermediate Android knowledge. Anyways for this task, I need to know what exactly I need to work with. My app needs to check if the user is at or close to the predefined location at a predefined time. Getting user location is not an issue.

My guess is working with a combination of AlarmManager, BroadcastReceiver and Service ? I haven't worked with either of these three.

Any help is appreciated. :)


Solution

  • These are the ingredients you need for you recipe:

    An Intent that targets your BroadcastReceiver:

    Intent intent = new Intent(context, YourBroadCastReceiver.class); 
    

    A PendingIntent that gets triggered by the AlarmManager and fires your already defined Intent:

    PendingIntent pIntent = PendingIntent.getBroadcast(context, requestCode, intent, flags);
    

    The AlarmManager that periodically activates your PendingIntent:

    AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    am.setRepeating(AlarmManager.RTC_WAKEUP, triggerAtMillis, intervalMillis, pIntent);
    

    The BroadCastReceiver that handles the event:

    @Override
    protected void handleReceive(Context context, Intent intent) {
         // handle GPS
    }