How to run an async task for every x mins in android?

前端 未结 9 1062
长发绾君心
长发绾君心 2020-11-27 12:28

how to run the async task at specific time? (I want to run it every 2 mins)

I tried using post delayed but it\'s not working?

    tvData.postDelayed         


        
9条回答
  •  爱一瞬间的悲伤
    2020-11-27 13:18

    Execute multiple messages(Runnables) then he should use the Looper class which is responsible for creating a queue in the thread. For example, while writing an application that downloads files from the internet, we can use Looper class to put files to be downloaded in the queue. This will help you to perform async task in android...

    HandlerThread hThread = new HandlerThread("HandlerThread");
      hThread.start();
      Handler handler = new Handler(hThread.getLooper());
      final Handler handler1 = new Handler(hThread.getLooper());
      final long oneMinuteMs = 60 * 1000;
    
      Runnable eachMinute = new Runnable() { 
       @Override
       public void run() {
        Log.d(TAG, "Each minute task executing");
        handler1.postDelayed(this, oneMinuteMs);
        sendPostRequest();
       }
      };
     // sendPostRequest();
      // Schedule the first execution
      handler1.postDelayed(eachMinute, oneMinuteMs);
    

提交回复
热议问题