Hello I am building a news application in android, so that every time a new data is entered into database user must receive a notification in the notification bar. So after research I found that I have to use broadcast receiver and alarm manager. But what I am not able to understand is that when the application is closed how it will know that a new data is entered? and how to make the alarm manager activate the application for checking new data let’s say every 5 minutes? Thank you.
Share
If I understand you correctly, you don’t need to run the whole application in order to check for new data, as it should be done in a separate background task.
What you need is an alarm registered within AlarmManager and a BroadcastReceiver that will get invoked when the alarm is fired. The receiver checks for new data, and if the condition is met, it executes the intent that will start the notification activity.
Please keep in mind, that if the database access requires more time, it’d better be done in a service started by the receiver.
You can save some coding (and solve some common problems) by using cwac-wakeful ( https://github.com/commonsguy/cwac-wakeful ), which will also make sure that your database-checking service stays awake as long as it does its job. It’s very well documented and has several examples available.
Some theory worth reading can be found at: http://www.vogella.com/articles/AndroidServices/article.html . It is mostly about services, but has some explanations about alarms too.