How to poll a webservice at finite interval from android?

坚强是说给别人听的谎言 提交于 2019-12-23 07:57:57

问题


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.


回答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




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




回答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.




回答4:


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



来源:https://stackoverflow.com/questions/9226927/how-to-poll-a-webservice-at-finite-interval-from-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!