I need some suggestions for approaches to take…
Here’s some background info:
Right now I have an Android app and a separate java program running on my server.
The java program continuously go out and gets information from different sites and stores them in 14 different entries in an SQL database on the server.
The Android app then queries the databases to retrieve the info to be displayed.
My goal:
I need suggestions on how to have the app handle checking for updates from the database, and then letting the user know that there is new information.
My first thought is that maybe I need to start a separate thread that queries the database for a time modified. Then if it finds updates, it would pop up on the screen that there is new information.
I’m not too well educated with the way threads or services work, so I guess I’m looking for how to implement this, or whether there is a completely different way to go about update checking that would be better.
Thanks in advance, I appreciate any feedback, input, or suggestions.
Hi Ryan
I have also implemented a similar thing in my android app and surprisingly I also had 14 tables in my PostgreSQL Server.
First of all, you would want to poll the server periodically even when the app is not in the foreground. For that you need to run a background Service – here you will have to manually create a thread in the service, because Service by default runs on the UI thread OR use an IntentService – you don’t have to create a separate thread. Whatever code you write in the intent service will be handled in a different thread automatically
Now you have to make this service execute periodically. For that use an AlarmManager and use the
setRepeating()function. In the arguments you have to give a PendingIntent to your Service or IntentService. But don’t use an alarm manager if you are going to poll the server for every less than 1 minute. Because the battery will be wasted a lot.Here is some code that might give you an idea :
This is Intent_Service of type IntentService :
But if you want instantaneous updates, then use Google Cloud Messaging Services. To know more about how it works see this
Hope this helps you.