Post data to the server every 15 minutes using the sync adapter and alarms

Requirement - I need approximately every 15 minutes to get the coordinates of the user's location and send it to the server. It is necessary to place data at approximately these intervals.

Implementation - I made a sync adapter instead of using AlarmManager as it saves battery. I set ContentResolver.addPeriodicSync()to synchronize my application every 15 minutes, which roughly corresponds to the current location and messages on the server.

Problem - If there is no Internet connection, I want to keep visiting it every 15 minutes and store them in a local sqlite database. When the Internet returns again next time, I will send all saved locations at a time so that the server data remains consistent, and after that the synchronization will resume, as usual.

Main problem the fact is that when there is no Internet, the synchronization stops and I stop receiving periodic synchronization callbacks in my application, and I can’t save the data in a local database. Therefore, I want that even when there is no Internet, I regularly receive callbacks until the Internet returns and automatic synchronization starts again. Can a sync adapter do this?

One solution . I can think that I get a broadcast when the Internet stops, and at that moment I start using AlarmManager to start the service every 15 minutes, and get the location and save it to the local database. And when the Internet returns, I stop using AlarmManager and return to automatic synchronization.

Solution 2 - Provided by David Medenyak below. It is also effective because of the behavior of the AlarmManager setInexactRepeating(), which attempts to simulate the behavior of the Sync adapter by scheduling alarms for different applications together to reduce the number of times the processor wakes up. This also leads to a slightly simpler implementation. Will it be better than the previous solution comparing the pros and cons?

?

+4
2

:

  • 15

, , , , . , ( 15 , , ), .


, 15 , .

. 15 , .

, , , . ( ).


()

: SyncAdapter gps , , . x , .

, , gps - .

+1

IntentService - 15 ( AlarmManager) db .

SyncAdapter - 15 . . Android .

Edit:
( @David Medenjak):
1) db
2) .

FusedLocationProvider

requestLocationUpdates (GoogleApiClient client, LocationRequest request, PendingIntent callbackIntent)

.

, , .

LocationRequest , , .

, .

0

Source: https://habr.com/ru/post/1628311/


All Articles