简体   繁体   中英

call a function every 5 seconds

I'm trying to build a simple Android app and I need a function to be called every 5 seconds. I can't flipping figure it out. The function I need to call every 5 seconds is otherFunction()

   Handler mHideHandler = new Handler();

   Runnable mHideRunnable = new Runnable() {

    @Override
    public void run() {
        otherFunction();        
        mSystemUiHider.hide();


    }
};

You could refer to the following example:

Handler locationPrompt = new Handler(){

    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        if(msg.what==SUBJECT){
            onLocationChanged(location);
            sendEmptyMessageDelayed(SUBJECT, REFRESH);
        }
    }

};

when calling this method:

locationPrompt.sendEmptyMessage(SUBJECT);

where

final static long REFRESH = 10 * 1000;
final static int SUBJECT = 0;

So this method gets called every 10 seconds here.\\

Hope this helps.

看一下Timer类,特别是调度方法。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM