How to call asyncTasks periodically

前端 未结 1 1927
野的像风
野的像风 2021-01-02 23:45

I have two AsyncTasks doing network operations. I want to call them periodically (like after one min.). How do I do that? I dont think I can do it on the UI thread. Do i nee

相关标签:
1条回答
  • 2021-01-03 00:26

    Just use a timer.

    final Handler handler = new Handler();
    Timer timer = new Timer();
    TimerTask task = new TimerTask() {       
         @Override
         public void run() {
           handler.post(new Runnable() {
              public void run() {  
                 new UploadAsyncTask().execute();
                 new DownloadAsyncTask().execute();
              }
            });
          }
    };
    timer.schedule(task, 0, 1000); //it executes this every 1000ms
    
    0 讨论(0)
提交回复
热议问题