Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在10秒内每秒运行一个任务。_Java_Android_Timer_Android Handler - Fatal编程技术网

Java 如何在10秒内每秒运行一个任务。

Java 如何在10秒内每秒运行一个任务。,java,android,timer,android-handler,Java,Android,Timer,Android Handler,我有以下代码,它每秒运行一个任务,但我也希望任务在10秒后停止。这个逻辑可以用我正在使用的处理程序实现吗?我尝试过用while循环实现计数器,但无法使其工作 mHandler = new Handler(); mUpdateUI = new Runnable() { public void run() { mVistaInspectionDate = HousingFragment.getVistaInspectionDate(); mVistaInspe

我有以下代码,它每秒运行一个任务,但我也希望任务在10秒后停止。这个逻辑可以用我正在使用的处理程序实现吗?我尝试过用while循环实现计数器,但无法使其工作

mHandler = new Handler();
mUpdateUI = new Runnable() {
    public void run() {
        mVistaInspectionDate = HousingFragment.getVistaInspectionDate();
        mVistaInspectionDateTextView.setText(mVistaInspectionDate);     

        if (mVistaInspectionDate != null) {
            mHandler.removeCallbacks(mUpdateUI);
        }
            mHandler.postDelayed(mUpdateUI, 1000); // 1 second
    }
};  

mHandler.post(mUpdateUI); 

使用带有布尔变量的While循环,并将此变量设置为true,然后可以计算任务已运行的次数,并在每次运行后停止任务1秒,但由于线程退出,1秒的延迟可能不是1秒

因此,您可以使用时间来计数和停止while循环。
节省您当前的时间,每1秒您的任务就会执行一次。线程终止10次后,可以通过在while循环中终止线程,也可以将布尔变量设置为false。

如果可以使用外部依赖项,请尝试使用类或调度程序,而不是在自己的应用程序中编码调度和线程逻辑。

为了使用计数器,您可以将其包装在如下函数中:

private void postDelayedWrapped(final int counter, int delay) {
    if (counter <= 0) return;
    mUpdateUI = new Runnable() {
        public void run() {
            mVistaInspectionDate = HousingFragment.getVistaInspectionDate();
            mVistaInspectionDateTextView.setText(mVistaInspectionDate);

            if (mVistaInspectionDate != null) {
                mHandler.removeCallbacks(mUpdateUI); //necessary?
            }
            postDelayedWrapped(counter - 1, 1000);
        }
    };

    mHandler.postDelayed(mUpdateUI, delay);
}
mHandler = new Handler();
postDelayedWrapped(10,0);

如何子类化处理程序类并使用该类的实例跟踪调用postDelayed()的次数

   public class MyHandler extends Handler {
        private int maxTimes;
        private int currentTimes; 
        private Runnable runner;
        public Handler(int maxTimes) {
              this.maxTimes = maxTimes;
        }

        @Override
        public void post(Runnable runner) {
              this.runner = runner;
        }

        @Override
        public void postDelayed(Runnable runner,long ms) {
               if (currentTimes == maxTimes) {
                    this.removeCallbacks(runner);
               } else {
                    super.postDelayed(runner,ms);
                    currentTimes++;
               }
        } 


   }

   mHandler = new MyHandler(10);//using subclass instance
   //from here on is the same as the original code.
   mUpdateUI = new Runnable() {
   public void run() {
      mVistaInspectionDate = HousingFragment.getVistaInspectionDate();
      mVistaInspectionDateTextView.setText(mVistaInspectionDate);     

      if (mVistaInspectionDate != null) {
          mHandler.removeCallbacks(mUpdateUI);
      }
         mHandler.postDelayed(mUpdateUI, 1000); // 1 second
    }
  };  

  mHandler.post(mUpdateUI); 

请提供更多详细信息…只需使用计数器字段<代码>如果(mCounter<10){…}请参见