BroadCastReceiver and AlarmManager Usage

101 views Asked by At

I have an android project that uses parse for its backend. To cut the number of api calls, I had been pinning "not so important" data to the local data store and syncing it once in a day using AlarmManager and BroadcastReceiver.

Now I want to sync the data often, every 2/3 minutes only when the app is running. How do I go about it? Do I use the same AlarmManager and BroadCastReceiver(changing the frequency and cancelling alarm on exit) to implement this? Or is there a more efficient way?

P.S : Parse.com users, what are the best ways to reduce the api call count in such a scenario? Are there any elegant ways?

I have a Posts table, which has texts posted by users and a votes table. Whenever a user creates a post I create a Posts object and save it to parse. And when another user uses the app, he gets to vote. The vote count for every Post object is saved in a column in the Posts table as and when the voting is done. But the user specific choices are saved locally. This is to reduce the number of API calls, I accumulate the user choices over time and sync it periodically.

2

There are 2 answers

3
Franzé Jr. On BEST ANSWER

I use Parse.com in some applications, I think the best way it depends on your application. For example, you can sync every "Start" in your application and also have a kind of AlarManager to be fired each hour,for example.

I don't know about your application, but I used to synchronize the data in each change on the server. So, once I have a change on my server, I send a notification and update my data on mobile, but it depends on how your application needs to sync that, if it will be a sync from mobile to server or server to mobile. I would like to hear from you more specific about your application and we can try a good approach for you.

0
Remy On

The simpelest way would probably Alarm Reciver filter "ACTION_TIME_TICK" How ever I'm not sure about the performence (it would tick even if the app hasn't start). Anyway I think you could work with treads, make a service which would run a Handler every 2-3 minutes, and start it when you application is up, and ofcourse terminate it when you application is being closed.

You might also look HERE.