Brijesh Patel
Brijesh Patel

Reputation: 676

Repeatedly execute functions in app at regular intervals

I have created an android app that is now required to upload latitude and longitude every 6 minutes. This needs to start after log in and end upon closing the app or logging out, and remain running when the phone goes into idle mode or other apps are visited.

Google suggests the following options;

  1. Create a service and put a timer task in it.
  2. Use an alarm manager.

Which of these two options are better? Are there any other options?

Upvotes: 0

Views: 84

Answers (2)

Jayy
Jayy

Reputation: 14808

Firstly, you will definitely need a Service. Activitys cannot be relied on, as once paused, (e.g. another app is visited) there is absolutely no guarantee they will not be aborted without even onDestroy() being called.

AlarmManager is the most reliable way of triggering the 6-minute polling you need, as this will be executed even if the phone is sleeping for a long time. Use this to trigger your service (which may have been stopped during the 6-minute lull). This is the only option I know of that will be the most reliable.

TimerTask will not work if the Service, Activity or process is ended.

Upvotes: 1

Akash Moradiya
Akash Moradiya

Reputation: 3322

you have to create master activity in which call locationmanager on uithread,that gives you lat-long at your position changed and at time of logout just remove all updates of location manager

Upvotes: 0

Related Questions