Abhinav
Abhinav

Reputation: 1740

How to poll using IntentService implementation after fixed time interval?

I have a requirement where I need to do polling to a server,fetch some events from the server and perform action over the events.All these have to be done asynchronously and on regular time interval.

So I wrote one class extending IntentService and started my thread inside its onHandleIntent method.

The code is as below:-

MyService.java

public class MyService extends IntentService{
    private String tag = "MyService";
    public MyService() {
        super("MyService");

    }

    @Override
    protected void onHandleIntent(Intent intent) {

        Log.d(tag, "onHandleIntent");

        TestThread.startThread();

    }

}

TestThread.java

public class TestThread {

    private static Timer threadTimer = new Timer();
    private static long pollingInterval = 5000;//in milliseconds
    private static boolean pollingStarted = false;
    public static void startThread() {
        threadTimer.schedule(new TestTimer(), 0, pollingInterval);
        pollingStarted= true;
    }

    private static class TestTimer extends TimerTask {


        @Override
        public void run() {
            //get event lists
            //do some work on the even list
        }

    }

}

In TestThread.java, it gets some event lists from server and performs tasks based upon events.

At android website they say

the service is started as needed, handles each Intent in turn using a worker thread, and stops itself when it runs out of work.

So my question is , will the polling continue in the above implementation ? If not what can be a correct implementation for the above requirement .

Upvotes: 1

Views: 1599

Answers (1)

JanBo
JanBo

Reputation: 2933

For background tasks that need to be done in services at regular time intervals you should use an AlarmManager to start your Service at desired intervals. The Service shuts down after the work being done and is started again with the alarm. You can also schedule or disable the alarm from within the service and so on....try google-ing for the answer in that direction.

A long running thread is not a safe or reliable way to do server polling especially in an IntentService. You can never know what will happen with your service, and keeping a Thread alive and running in a background service with timer is not considered a good programming practice to say the least.

Why? for ex. : How do I keep the Thread of an IntentService alive?

So the right way is a "regular" Service + AlarmManager.

Android Polling from a Server periodically


Edit:

***** There are some answers that say you dont need a service for periodical tasks:**

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

How to poll a webservice at finite interval from android?

And its try you can do your code in your Alarm Manager onReceive method, but i would advise against such actions if you plan on doing some extensive work in your code, since the android doc says that BroadCasts onReceive methods should be used for longer operations, rather you should start a service in there and let it do the work.

Also maybe take a look at this http://developer.android.com/google/gcm/index.html & http://developer.android.com/google/gcm/index.html it maybe useful.

Upvotes: 1

Related Questions