Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background Process to scan the location of the user at regular intervals and update the local database even when the app is not open

I am creating an app that checks for user locations every half an hour and updates the location of the user in the local database and then runs CRUD queries based on the user's location even when the app is not running. How do i do it ?

I have referred to this http://techtej.blogspot.com.es/2011/03/android-thread-constructspart-4.html article and i am still confused about which is the correct approach for my result ?

There are 4 options according to the article for what i intend to achieve according to me

1) Service : But since i feel it would be a long operation with the local database, i feel i should ignore this one.

2) IntentService : This cannot perform multiple tasks, so i feel this one also should be avoided for me as i have to get the location of the user and scan the database , update the database (3 tasks)

3)Thread : I am not sure how to call this when the app is not open

4) AsyncTask : I am not sure how to call this when the app is not open.

Basically i looking for something like a CRON JOB that runs on a local database while working on the location data.

It would be great if you could link me up to some tutorials and answer with a simple example to make me understand the difference of all 4 methods.

// editted on 16 March : I have read something about a JobScheduler which is introduced in the API 21, but not sure if it also supports till Gingerbread and is it the right approach for my question

Thanx

like image 710
Sagar Devanga Avatar asked Mar 14 '15 08:03

Sagar Devanga


People also ask

How do I get a location background?

Background location access checklistVerify that your app requires these location permissions. If your app targets Android 10 (API level 29) or higher, also check for the ACCESS_BACKGROUND_LOCATION permission. Verify that your app has a feature that requires it.

How do you check location is on or off in android programmatically?

Checking for location manager: lm. getAllProviders(). contains(LocationManager. GPS_PROVIDER) (or NETWORK_PROVIDER ) would make sure that you do not throw the user to a settings page where there is no network option.


2 Answers

When recording the users position use a service with a notification. Just for the sake of creating a morally responsible app that informs the user the app is tracking them. The service by definition runs in the background.

A fused location provider with setinterval(long) 30 minutes gets the interval. Set fastestInterval() to a minute to receive GPS data when other apps are using the GPS.

like image 117
danny117 Avatar answered Nov 14 '22 22:11

danny117


Have you considered using a SyncAdapter. Its best to schedule jobs at fixed interval and also optimized for battery usage. Also, once started, it can run independently of the app. As per your requirements, I believe this is best suited for your need. You can read about this here. This also removes the corner case of starting the service (generally used) when your device is restarted. Your app will still continue running the scheduled job even if the device gets restarted.

In the SyncAdapter you have to use a ContentProvider so wrap your DB inside a ContentProvider. Also, preferably use a CursorLoader to run longrunning tasks on DB. You should read about CursorLoader. This is a great way to access your resources. Also, you can define an Observer Design Pattern which Observes for changes in a DB and will perform a task when changes are made in DB. This can also be used inside your application itself and also inside SyncAdapter. Cursor Loader is best preferred for background work on DB. You can perform all CRUD Operations using a CursorLoader and ContentProvider.

like image 30
Roadblock Avatar answered Nov 14 '22 22:11

Roadblock