I'm building an Android client for an Internet discussion board: the app downloads the discussions from the server and displays them using the native Android UI. It was quite easy to build the basics such as getting and displaying the content, and posting the replies back to the server.
Now I want to bring it to the next level: the app should store all the data locally on the device and sync it with the server periodically, getting the recent changes and updating the local DB. I don't want it to check for the changes on demand; the periodic updates are better because this allows some nice features like subscribing to the updates.
Unfortunately the server is not GCM-compliant (and it will never be), it is a good old simple web server so I have to implement the sync myself.
I've found a comment to another question where it's said that a timer-based check is a bad idea because the device will have to wake up and connect to the Internet. It would be much better to catch when the device begins its own data sync, but its there a way to handle this without a perioic check?
I've looked over many discussions on this issue; most of them discuss the ContentProviders, protocols, services like GCM/C2DM and so on. I've found nothing about the power efficiency.
So how to do the sync properly so my app wouldn't drain the battery?
it would be better if you use GCM as the server can push updates if available that would be power efficient than polling as network will only be used when updates are available its far better than timely polling as it will check and wake the phone just to check for updates
Important: C2DM has been officially deprecated as of June 26, 2012. This means that C2DM has stopped accepting new users and quota requests. No new features will be added to C2DM. However, apps using C2DM will continue to work. Existing C2DM developers are encouraged to migrate to the new version of C2DM, called Google Cloud Messaging for Android (GCM). See the C2DM-to-GCM Migration document for more information. Developers must use GCM for new development.
but as you cant get with GCM you will have to go for polling itself you can use it in a power efficiant way by using alarm manager and inexact repeating
i think that is the best power efficient way to poll periodically
giving a sample code
public class MyScheduleReceiver extends BroadcastReceiver {
// Restart service every 30 sec
private static final long REPEAT_TIME = 1000 * 30 ;
@Override
public void onReceive(Context context, Intent intent) {
AlarmManager service = (AlarmManager) context
.getSystemService(Context.ALARM_SERVICE);
Intent i = new Intent(context, MyStartServiceReceiver.class);
PendingIntent pending = PendingIntent.getBroadcast(context, 0, i,
PendingIntent.FLAG_CANCEL_CURRENT);
Calendar cal = Calendar.getInstance();
// Start 30 seconds after boot completed
cal.add(Calendar.SECOND, 30);
//
// Fetch every 30 seconds
// InexactRepeating allows Android to optimize the energy consumption
service.setInexactRepeating(AlarmManager.RTC_WAKEUP,
cal.getTimeInMillis(), REPEAT_TIME, pending);
// service.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
// REPEAT_TIME, pending);
}
}
(There is a more detailed explanation that includes the necessary manifest items.)