How to loop a service?

前端 未结 5 1224
深忆病人
深忆病人 2021-02-05 11:53

My service needs to check for something every minute and

while(true)
{
   Thread.sleep(60000) 
   //REST OF CODE HERE//
}

is not working. Makin

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 12:32

    As Janusz wrote, you can use handler to post delayed. Here is a sample:

    final Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            // Do repeatable stuff
            handler.postDelayed(this, DELAYED_TIME);
        }
    };
    handler.post(runnable);
    

    It will post runnable and then post it again and again after DELAYED_TIME.

提交回复
热议问题