Lork
Lork

Reputation: 145

How to make a simple timer without new class in Android?

I'm developing a Service for Android, that must run in background, executing a function each 100 seconds. That's the source code (example)

package com.example

import ....

public class Servizio extends Service {

    public IBinder onBind(Intent intent) {

    }

    public void onCreate() {

    }

    public void onDestroy() {

    //here put the code that stop the timer cycle

    }

    public void onStart(Intent intent, int startid) {

    //i want to begin here the timercycle that each 100 s call myCycle()

    }

    public void myCycle() {

    //code that i can't move on other class!!!

    }

}

How I can do that? Now the service execute myCycle() just one time, beacause I put a calling in onStart().

Upvotes: 3

Views: 9335

Answers (3)

James Black
James Black

Reputation: 41858

When I had this issue, where I call out to a webservice on a periodic basis this was my solution:

public class MyService extends Service {
    private Handler mHandler = new Handler();
    public static final int ONE_MINUTE = 60000;
    private Runnable periodicTask = new Runnable() {
        public void run() {
            mHandler.postDelayed(periodicTask, 100 * ONE_MINUTE);
            token = retrieveToken();
            callWebService(token);
        }
    };

I call the postDelayed early so that the delay from the webservice call doesn't cause the timing to shift.

The two functions are actually in the MyService class.

UPDATE:

You can pass a Runnable to postDelayed as shown here:

http://developer.android.com/reference/android/os/Handler.html#postDelayed(java.lang.Runnable, long)

public final boolean postDelayed (Runnable r, long delayMillis)

Since: API Level 1 Causes the Runnable r to be added to the message queue, to be run after the specified amount of time elapses. The runnable will be run on the thread to which this handler is attached.

Also, my code won't compile due to missing functions, this is shown as an example of what the OP can do.

Upvotes: 1

dogbane
dogbane

Reputation: 274622

Use a Timer with a TimerTask. To execute your method every 100 seconds, you can use the following in your onStart method. Be aware that this method creates a new thread.

new Timer().schedule(new TimerTask() {
     @Override
     public void run() {
         myCycle();
     }
}, 0, 100000);

Alternatively, use an android.os.Handler as described in this article: Updating the UI from a Timer. It is better than a Timer because it runs in the main thread, avoiding the overhead of a second thread.

private Handler handler = new Handler();
Runnable task = new Runnable() {
    @Override
    public void run() {
        myCycle();
        handler.postDelayed(this, 100000);
    }
};
handler.removeCallbacks(task);
handler.post(task);

Upvotes: 7

JPM
JPM

Reputation: 9296

I usually do this, if I understood correctly you wanted the myCycle to execute every 100 secs.

Timer myTimer = new Timer();
myTimer.schedule(new TimerTask() {
    @Override
    public void run() {
        myCycle();
    }
}, 0,100000);

Upvotes: 2

Related Questions