Repeat a task with a time delay?

后端 未结 12 1572
小蘑菇
小蘑菇 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:43

    To anyone interested, here's a class I created using inazaruk's code that creates everything needed (I called it UIUpdater because I use it to periodically update the UI, but you can call it anything you like):

    import android.os.Handler;
    /**
     * A class used to perform periodical updates,
     * specified inside a runnable object. An update interval
     * may be specified (otherwise, the class will perform the 
     * update every 2 seconds).
     * 
     * @author Carlos Simões
     */
    public class UIUpdater {
            // Create a Handler that uses the Main Looper to run in
            private Handler mHandler = new Handler(Looper.getMainLooper());
    
            private Runnable mStatusChecker;
            private int UPDATE_INTERVAL = 2000;
    
            /**
             * Creates an UIUpdater object, that can be used to
             * perform UIUpdates on a specified time interval.
             * 
             * @param uiUpdater A runnable containing the update routine.
             */
            public UIUpdater(final Runnable uiUpdater) {
                mStatusChecker = new Runnable() {
                    @Override
                    public void run() {
                        // Run the passed runnable
                        uiUpdater.run();
                        // Re-run it after the update interval
                        mHandler.postDelayed(this, UPDATE_INTERVAL);
                    }
                };
            }
    
            /**
             * The same as the default constructor, but specifying the
             * intended update interval.
             * 
             * @param uiUpdater A runnable containing the update routine.
             * @param interval  The interval over which the routine
             *                  should run (milliseconds).
             */
            public UIUpdater(Runnable uiUpdater, int interval){
                UPDATE_INTERVAL = interval;
                this(uiUpdater);
            }
    
            /**
             * Starts the periodical update routine (mStatusChecker 
             * adds the callback to the handler).
             */
            public synchronized void startUpdates(){
                mStatusChecker.run();
            }
    
            /**
             * Stops the periodical update routine from running,
             * by removing the callback.
             */
            public synchronized void stopUpdates(){
                mHandler.removeCallbacks(mStatusChecker);
            }
    }
    

    You can then create a UIUpdater object inside your class and use it like so:

    ...
    mUIUpdater = new UIUpdater(new Runnable() {
             @Override 
             public void run() {
                // do stuff ...
             }
        });
    
    // Start updates
    mUIUpdater.startUpdates();
    
    // Stop updates
    mUIUpdater.stopUpdates();
    ...
    

    If you want to use this as an activity updater, put the start call inside the onResume() method and the stop call inside the onPause(), so the updates start and stop according to the activity visibility.

提交回复
热议问题