Repeat a task with a time delay?

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

    Try following example it works !!!

    Use [Handler] in onCreate() method which makes use of postDelayed() method that Causes the Runnable to be added to the message queue, to be run after the specified amount of time elapses that is 0 in given example. 1

    Refer this code :

    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
           setContentView(R.layout.main);
        //------------------
        //------------------
        android.os.Handler customHandler = new android.os.Handler();
                customHandler.postDelayed(updateTimerThread, 0);
    }
    
    private Runnable updateTimerThread = new Runnable()
    {
            public void run()
            {
                //write here whaterver you want to repeat
                customHandler.postDelayed(this, 1000);
            }
    };
    

提交回复
热议问题