【问题标题】:Thread.sleep() skips to last method in Java/AndroidThread.sleep() 跳到 Java/Android 中的最后一个方法
【发布时间】:2013-09-13 00:34:26
【问题描述】:

虽然必须有更好的策略来完成我在应用程序中设置的倒计时,但我似乎很清楚在每个数字之间使用Thread.sleep(1000) 会起作用。它没有,我仍然没有任何其他解决方案。当我运行应用程序时,倒计时从 5 秒变为 1 秒。

这是我的代码:

bBegin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

           bBegin.setText("5");

            try{
                Thread.sleep(1000);
            }catch(InterruptedException ie){

            }


            bBegin.setText("4");

            try{
                Thread.sleep(1000);
            }catch(InterruptedException ie){

            }

            bBegin.setText("3");

            try{
                Thread.sleep(1000);
            }catch(InterruptedException ie){

            }

            bBegin.setText("2");

            try{
                Thread.sleep(1000);
            }catch(InterruptedException ie){

            }

            bBegin.setText("1");

            try{
                Thread.sleep(1000);
            }catch(InterruptedException ie){

            }
}

谢谢,我希望这个问题可以帮助像我这样的其他菜鸟(不用担心,我确实检查了该网站以获取以前的解决方案)

【问题讨论】:

    标签: android multithreading sleep


    【解决方案1】:

    Thread.sleep(1000); 糟糕!你在UI thread 上打电话给sleep(),如果有的话,你几乎不想这样做。 UI 将在这段时间内休眠。请改用CountDownTimer

    CountDownTimer Docs

    当然还有其他方法,但这个应该可以很好地完成你想要的。

    示例

    在您的情况下,您可能会使用 CountDownTimer 类似的东西。

    创建一个内部类

    private class MyCountDown extends CountDownTimer
    {
        long duration, interval;
        public MyCountDown(long millisInFuture, long countDownInterval) 
       {
            super(millisInFuture, countDownInterval);
            duration = millisInFuture;
            interval = countDownInterval;
            int secs = 5;  // varaible for counter to be placed in TextView
            start();
        }
    
               @Override
        public void onTick(long duration) 
       {
            bBegin.setText(String.valueOf(secs));           
            secs = secs - 1;            
        }
    
        @Override
        public void onFinish() {
            secs = 5; // reset counter if needed or do anything else you need when countdown is over
        }
    

    并在onClick() 中调用它

    MyCountDown timer = new MyCountDown(5000, 1000);  // local variable but might want to make it a member variable
    

    【讨论】:

      【解决方案2】:

      睡眠不会“跳过”任何内容 - 相反,此代码阻止 UI 更新,直到方法结束(在它全部睡眠 5 次之后)。

      在某个点之后 onClick 方法结束 - 并且 UI 调度可以恢复操作 - UI 被更新。当此更新发生时,显示将更新为分配的最后一个值,这会导致错误的结论是代码被“跳过”。但是,方法中的所有代码都运行了

      这将我们引向一条非常重要的规则:不要在主 UI 线程上休眠。这样做会阻塞用户交互并且它阻塞显示更新,因为它阻塞 UI事件/调度队列。所有标准的 UI 回调,例如点击侦听器,都发生在 UI 线程上

      需要睡眠 的地方 - 比如在游戏和自定义画布的情况下 - 睡眠是在不同的(非 UI)线程上完成的,因此它不会导致这种主要的 UI 阻塞行为.

      【讨论】:

        【解决方案3】:
        **As in the attached code snippet it is showing user thread is trying to update UI Thread.which is not a good practice..
        i have done same using the concept of Handler.
        the code snipper is ..**
        
        
        public class MainActivity extends Activity {
            TextView tv;
            Button b1;
            Handler handler;
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
                tv=(TextView) findViewById(R.id.textView1);
                b1=(Button) findViewById(R.id.button1);
                handler= new Handler();
        
        
                b1.setOnClickListener(new OnClickListener() {
        
                    @Override
                    public void onClick(View arg0) {
        
                        Runnable r=new Runnable() {
                            int   mynumber=5;
                            @Override
                            public void run() {
                                while(mynumber >0){
                                    try {
                                        Thread.sleep(1000);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                    handler.post(new Runnable() {
                                        public void run() {
                                            tv.setText("remaining time  is "+mynumber );
                                        }
                                    });
        
                                    mynumber--;
                                }
                            }
                        };
        
                        new Thread(r).start();
                    }
                });
        
        
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 2014-11-12
          • 2016-06-21
          • 1970-01-01
          • 1970-01-01
          • 2019-01-30
          • 1970-01-01
          • 1970-01-01
          • 2012-07-26
          • 1970-01-01
          相关资源
          最近更新 更多