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.