Repeat a task with a time delay?

后端 未结 12 1570
小蘑菇
小蘑菇 2020-11-22 02:14

I have a variable in my code say it is \"status\".

I want to display some text in the application depending on this variable value. This has to be done with a speci

12条回答
  •  耶瑟儿~
    2020-11-22 02:55

    There are 3 ways to do it:

    Use ScheduledThreadPoolExecutor

    A bit of overkill since you don't need a pool of Thread

       //----------------------SCHEDULER-------------------------
        private final ScheduledThreadPoolExecutor executor_ =
                new ScheduledThreadPoolExecutor(1);
         ScheduledFuture schedulerFuture;
       public void  startScheduler() {
           schedulerFuture=  executor_.scheduleWithFixedDelay(new Runnable() {
                @Override
                public void run() {
                    //DO YOUR THINGS
                    pageIndexSwitcher.setVisibility(View.GONE);
                }
            }, 0L, 5*MILLI_SEC,  TimeUnit.MILLISECONDS);
        }
    
    
        public void  stopScheduler() {
            pageIndexSwitcher.setVisibility(View.VISIBLE);
            schedulerFuture.cancel(false);
            startScheduler();
        }
    

    Use Timer Task

    Old Android Style

        //----------------------TIMER  TASK-------------------------
    
        private Timer carousalTimer;
        private void startTimer() {
            carousalTimer = new Timer(); // At this line a new Thread will be created
            carousalTimer.scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {
                    //DO YOUR THINGS
                    pageIndexSwitcher.setVisibility(INVISIBLE);
                }
            }, 0, 5 * MILLI_SEC); // delay
        }
    
        void stopTimer() {
            carousalTimer.cancel();
        }
    

    Use Handler and Runnable

    Modern Android Style

        //----------------------HANDLER-------------------------
    
        private Handler taskHandler = new android.os.Handler();
    
        private Runnable repeatativeTaskRunnable = new Runnable() {
            public void run() {
                //DO YOUR THINGS
            }
        };
    
       void startHandler() {
            taskHandler.postDelayed(repeatativeTaskRunnable, 5 * MILLI_SEC);
        }
    
        void stopHandler() {
            taskHandler.removeCallbacks(repeatativeTaskRunnable);
        }
    

    Non-Leaky Handler with Activity / Context

    Declare an inner Handler class which does not leak Memory in your Activity/Fragment class

    /**
         * Instances of static inner classes do not hold an implicit
         * reference to their outer class.
         */
        private static class NonLeakyHandler extends Handler {
            private final WeakReference mActivity;
    
            public NonLeakyHandler(FlashActivity activity) {
                mActivity = new WeakReference(activity);
            }
    
            @Override
            public void handleMessage(Message msg) {
                FlashActivity activity = mActivity.get();
                if (activity != null) {
                    // ...
                }
            }
        }
    

    Declare a runnable which will perform your repetitive task in your Activity/Fragment class

       private Runnable repeatativeTaskRunnable = new Runnable() {
            public void run() {
                new Handler(getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
    
             //DO YOUR THINGS
            }
        };
    

    Initialize Handler object in your Activity/Fragment (here FlashActivity is my activity class)

    //Task Handler
    private Handler taskHandler = new NonLeakyHandler(FlashActivity.this);
    

    To repeat a task after fix time interval

    taskHandler.postDelayed(repeatativeTaskRunnable , DELAY_MILLIS);

    To stop the repetition of task

    taskHandler .removeCallbacks(repeatativeTaskRunnable );

    UPDATE: In Kotlin:

        //update interval for widget
        override val UPDATE_INTERVAL = 1000L
    
        //Handler to repeat update
        private val updateWidgetHandler = Handler()
    
        //runnable to update widget
        private var updateWidgetRunnable: Runnable = Runnable {
            run {
                //Update UI
                updateWidget()
                // Re-run it after the update interval
                updateWidgetHandler.postDelayed(updateWidgetRunnable, UPDATE_INTERVAL)
            }
    
        }
    
     // SATART updating in foreground
     override fun onResume() {
            super.onResume()
            updateWidgetHandler.postDelayed(updateWidgetRunnable, UPDATE_INTERVAL)
        }
    
    
        // REMOVE callback if app in background
        override fun onPause() {
            super.onPause()
            updateWidgetHandler.removeCallbacks(updateWidgetRunnable);
        }
    

提交回复
热议问题