简体   繁体   English

每5秒调用一次函数

[英]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. 我正在尝试构建一个简单的Android应用,并且我需要每5秒调用一次函数。 I can't flipping figure it out. 我无法弄清楚。 The function I need to call every 5 seconds is otherFunction() 我需要每5秒调用一次的函数是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.\\ 因此,这里每10秒调用一次此方法。

Hope this helps. 希望这可以帮助。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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