How to poll a webservice at finite interval from android?
Asked Answered
L

4

8

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.

Lysippus answered 10/2, 2012 at 11:16 Comment(0)
A
10

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

Airflow answered 10/2, 2012 at 11:44 Comment(2)
You can put all your code into the single project and deploy as a single APK.Airflow
Many devices will create trouble for periodic background tasks that are not services because of battery saving tweaks they incorporate.Waal
E
6

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

Evaginate answered 10/2, 2012 at 12:1 Comment(2)
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.htmlLauraine
For further future references, GCM is now FCM (Firebase Cloud Messaging). Here is a migration guide: developers.google.com/cloud-messaging/android/…Selwyn
S
2

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.

Stillness answered 10/2, 2012 at 11:35 Comment(1)
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 insteadAlbuminoid
C
0

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

Countable answered 10/2, 2012 at 11:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.