Home » Android » web services – How to poll a webservice at finite interval from android?

web services – How to poll a webservice at finite interval from android?

Posted by: admin June 15, 2020 Leave a comment

Questions:

i am developing an android app to show notifications.How can i poll a webservice at finite interval (say 10 min interval) to check for any update and show as notification in android notification panel.

How to&Answers:

You do not need a service. I feel like a broken record. In this use case a service will sit there for 95% of the time doing nothing but still using system resources and more importantly drain the battery.

Please see my answer on a similar question that uses an Alarm from the AlarmManager:

Running task periodicaly(once a day/once a week)

Edit:

Look at this tutorial from the Android Development site for how to implement notifications: http://developer.android.com/guide/topics/ui/notifiers/notifications.html

Answer:

There is Cloud to Device Messaging service provided by Google C2DM . You can use this service to push message into all registered mobile devices. This not only improves performance but also makes sure that the Battery is not drained while continuously polling the server.

For your scenario, you can write in a java class to regularly poll the web service and use C2DM service to push the message to your Android phone which can be shown as a Notification. Check out this tutorial by Vogella on link. Or better, if you are using the web service just for fetching updated details, then you can avoid the web service and directly call the C2DM Push Service to push message to all registered devices whenever there is a change.

Hope this helped. 🙂

Answer:

use timer in which you can call to that web service after finite interval for e.x

Timer waitingTimer = new Timer();
    waitingTimer.schedule(new TimerTask() 
     {
        @Override
        public void run() 
        {
            runOnUiThread(new Runnable() 
            {
                public void run() 
                {
                    // code to hit xml after time interval
                }
            }); 
        }
    },0,20000); // mention time interval after which your xml will be hit.

Here 20000 means after every 20 sec it will be hit.

Answer:

Define a service, having a thread with a timer, on timeout invoke webservice.