How to poll a webservice at finite interval from android?

12,725

Solution 1

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

Solution 2

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. :)

Solution 3

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.

Share:
12,725
soumitra chatterjee
Author by

soumitra chatterjee

Updated on June 13, 2022

Comments

  • soumitra chatterjee
    soumitra chatterjee almost 2 years

    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.

  • njzk2
    njzk2 about 12 years
    a thread is definitely not the way to go for this in android. It will sit there in memory doing nothing, that is not good. use the alarmmanager instead
  • Graham Smith
    Graham Smith about 12 years
    You can put all your code into the single project and deploy as a single APK.
  • paul
    paul over 11 years
    For future searchers: C2DM has been deprecated and replaced by GCM: developer.android.com/guide/google/gcm/index.html; Migration guide: developer.android.com/guide/google/gcm/c2dm.html
  • Viv
    Viv over 6 years
    For further future references, GCM is now FCM (Firebase Cloud Messaging). Here is a migration guide: developers.google.com/cloud-messaging/android/…
  • elig
    elig over 3 years
    Many devices will create trouble for periodic background tasks that are not services because of battery saving tweaks they incorporate.