在听众中等待1秒钟?

时间:2012-03-27 11:49:42

标签: android multithreading wait

我该怎么做? 我尝试了一些线程但没有发生任何事情。

        myView.setOnTouchListener(new OnTouchListener()
        {
            public boolean onTouch(View arg0, MotionEvent arg1) 
            {
                myButton.setClickable(false);

                //Here i want to wait 1 second then:

                myButton.setClickable(true);

                return false;
            }

        });

请问有人在我的代码中等待一秒钟吗?

6 个答案:

答案 0 :(得分:7)

如果您在那里等待,您将阻止UI线程。您的用户会因此而讨厌您。如果您更详细地描述了您想要实现的目标,我们可能会帮助您找到更好的解决方案。

更好的解决方案是使用postDelayed方法

    myView.setOnTouchListener(new OnTouchListener()
    {
        public boolean onTouch(View arg0, MotionEvent arg1) 
        {
            myButton.setClickable(false);

            //wait 1 second
            myButton.postDelayed(new Runnable() {

                @Override
                public void run() {
                    myButton.setClickable(true);                        
                }
            }, 1000);


            return false;
        }

    });

答案 1 :(得分:6)

你绝对不需要通过调用 Thread.sleep(1000)来阻止UI线程,所以你需要通过在UI线程上1秒后运行所需操作的Handler来实现它:< / p>

//initialize it in your activity so that the handler is bound to UI thread
Handler handlerUI = new Handler();

myView.setOnTouchListener(new OnTouchListener()
        {
            public boolean onTouch(View arg0, MotionEvent arg1) 
            {
                myButton.setClickable(false);

                //Here i want to wait 1 second then:

                handlerUI.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                      myButton.setClickable(true);
                    }
                }, 1000);

                return false;
            }

        });

答案 2 :(得分:1)

您可以像这样使用CountDownTimer一秒钟:

public boolean onTouch(View arg0, MotionEvent arg1) 
            {
                myButton.setClickable(false);

                //Here i want to wait 1 second then:
                new CountDownTimer(1000,1000) {

            @Override
            public void onTick(long arg0) {}

            @Override
            public void onFinish() {
                myButton.setClickable(true);
            }
        }.start();
                return false;
            }

注意:如果您使用了Thread.sleep(int miliseconds ) ;,您的用户界面将暂停一段时间

答案 3 :(得分:0)

    final  CountDownTimer gps_timer;
    isLocationAvailable=false;
    m_forsms=forsms;

    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

    if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0f, this);
        else
    {
        sendLocation_network(forsms);
        return;
    }

    gps_timer  = new CountDownTimer(15000,1000) {

      int nn=0;
        @Override
        public void onTick(long arg0) {
            nn++;
            Toast.makeText(context, "gps counter :" + nn, Toast.LENGTH_SHORT).show();
            if (isLocationAvailable) {
                locationManager.removeUpdates(SendSMS.this);
                gps_h.sendEmptyMessage(0);

            }
        }


        @Override
        public void onFinish() {
            if (isLocationAvailable) {
                locationManager.removeUpdates(SendSMS.this);
                //Toast.makeText(context, "gps finish ok :" + nn, Toast.LENGTH_LONG).show();
            }
            else
            {
                //Toast.makeText(context, "GPS Fail  :" + nn, Toast.LENGTH_LONG).show();
                sendLocation_network(m_forsms);

            }
        }
    };

      gps_h=new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            gps_timer.cancel();
            return true;
        }
    });

    gps_timer.start();

答案 4 :(得分:-1)

Thread.sleep(1000);

Thread.currentThread().sleep(1000);

答案 5 :(得分:-3)

getThread().sleep(1000)使用正确的try catch阻止。