Java Android定时器概念

Java Android定时器概念,java,android,timer,Java,Android,Timer,很抱歉问了这么一个基本的问题,实际上我需要在一定的时间间隔后调用一个方法,这个方法实际上是在android中为textView分配一个文本,这应该会改变。所以请建议我最好的方法。 提前感谢你 { int splashTime=3000; int waited = 0; while(waited < splashTime) { try { ds.open();

很抱歉问了这么一个基本的问题,实际上我需要在一定的时间间隔后调用一个方法,这个方法实际上是在android中为textView分配一个文本,这应该会改变。所以请建议我最好的方法。 提前感谢你

{
         int splashTime=3000;
         int waited = 0;
         while(waited < splashTime)
         {
             try {
                  ds.open();
                  String quotes=ds.getRandomQuote();
                  textView.setText(quotes);
                  ds.close();


              }
              catch(Exception e)
              {
                  e.printStackTrace();
              }
         }
         waited+=100;
     }
{
时间=3000;
int=0;
while(等待时间)
{
试一试{
ds.open();
字符串引号=ds.getRandomQuote();
setText(引号);
ds.close();
}
捕获(例外e)
{
e、 printStackTrace();
}
}
平均值+=100;
}

你考虑过倒计时吗?例如,类似这样的事情:

     /**
     * Anonymous inner class for CountdownTimer
     */
    new CountDownTimer(3000, 1000) { // Convenient timing object that can do certain actions on each tick

        /**
         * Handler of each tick.
         * @param millisUntilFinished - millisecs until the end
         */
        @Override
        public void onTick(long millisUntilFinished) {
            // Currently not needed
        }

        /**
         * Listener for CountDownTimer when done.
         */
        @Override
        public void onFinish() {
             ds.open();
              String quotes=ds.getRandomQuote();
              textView.setText(quotes);
              ds.close(); 
        }
    }.start();
    long delayInMillis = 3000; // 3s
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            // you need to update UI on UIThread
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ds.open();
                    String quotes=ds.getRandomQuote();
                    textView.setText(quotes);
                    ds.close();
                }
            });
        }
    }, delayInMillis);

当然,你可以把它放在一个循环中。

你考虑过倒计时吗?例如,类似这样的事情:

     /**
     * Anonymous inner class for CountdownTimer
     */
    new CountDownTimer(3000, 1000) { // Convenient timing object that can do certain actions on each tick

        /**
         * Handler of each tick.
         * @param millisUntilFinished - millisecs until the end
         */
        @Override
        public void onTick(long millisUntilFinished) {
            // Currently not needed
        }

        /**
         * Listener for CountDownTimer when done.
         */
        @Override
        public void onFinish() {
             ds.open();
              String quotes=ds.getRandomQuote();
              textView.setText(quotes);
              ds.close(); 
        }
    }.start();
    long delayInMillis = 3000; // 3s
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            // you need to update UI on UIThread
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ds.open();
                    String quotes=ds.getRandomQuote();
                    textView.setText(quotes);
                    ds.close();
                }
            });
        }
    }, delayInMillis);

当然,您可以将其放在循环中。

您可以使用计时器以如下方式延迟更新UI:

     /**
     * Anonymous inner class for CountdownTimer
     */
    new CountDownTimer(3000, 1000) { // Convenient timing object that can do certain actions on each tick

        /**
         * Handler of each tick.
         * @param millisUntilFinished - millisecs until the end
         */
        @Override
        public void onTick(long millisUntilFinished) {
            // Currently not needed
        }

        /**
         * Listener for CountDownTimer when done.
         */
        @Override
        public void onFinish() {
             ds.open();
              String quotes=ds.getRandomQuote();
              textView.setText(quotes);
              ds.close(); 
        }
    }.start();
    long delayInMillis = 3000; // 3s
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            // you need to update UI on UIThread
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ds.open();
                    String quotes=ds.getRandomQuote();
                    textView.setText(quotes);
                    ds.close();
                }
            });
        }
    }, delayInMillis);

您可以使用计时器以如下方式延迟更新UI:

     /**
     * Anonymous inner class for CountdownTimer
     */
    new CountDownTimer(3000, 1000) { // Convenient timing object that can do certain actions on each tick

        /**
         * Handler of each tick.
         * @param millisUntilFinished - millisecs until the end
         */
        @Override
        public void onTick(long millisUntilFinished) {
            // Currently not needed
        }

        /**
         * Listener for CountDownTimer when done.
         */
        @Override
        public void onFinish() {
             ds.open();
              String quotes=ds.getRandomQuote();
              textView.setText(quotes);
              ds.close(); 
        }
    }.start();
    long delayInMillis = 3000; // 3s
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            // you need to update UI on UIThread
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    ds.open();
                    String quotes=ds.getRandomQuote();
                    textView.setText(quotes);
                    ds.close();
                }
            });
        }
    }, delayInMillis);

使用处理程序并将其置于可运行状态:

int splashTime = 3000;
Handler handler = new Handler(activity.getMainLooper());
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        try {
            ds.open();
            String quotes=ds.getRandomQuote();
            textView.setText(quotes);
            ds.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}, splashTime);

使用处理程序并将其置于可运行状态:

int splashTime = 3000;
Handler handler = new Handler(activity.getMainLooper());
handler.postDelayed(new Runnable() {
    @Override
    public void run() {
        try {
            ds.open();
            String quotes=ds.getRandomQuote();
            textView.setText(quotes);
            ds.close();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
}, splashTime);

这个代码怎么了?有什么错误吗?您是否调用了threadname.start()?此代码有什么问题?有什么错误吗?您是否调用了threadname.start()?