postDelayed 阻塞UI线程

3
我想要的:用户长按按钮1900毫秒。如果他在1900毫秒之前抬起手指,手机就会停止震动。而如果他将手指放在按钮上超过1900毫秒,calculate()方法就会运行。 我正在使用:postDelayed,因为据我所读,它不会干扰ui线程。 我试图检查是否已经过了1900毫秒,并且用户没有拿起手指,即使如此,calculate方法也会运行。 发生的错误:如果用户在1900秒之前抬起手指或者只是触摸并立即拿起手指,手机会持续震动。虽然我正在使用MotionEvent.ACTION_UP进行检查,但这不应该发生。请帮忙!
  int flag = 0;
  int aborted_flag = 0;
 @Override
public boolean onTouch(View v, MotionEvent event) {

    Handler mHandler = new Handler();

    if(event.getAction()==MotionEvent.ACTION_DOWN){
        scanning();
        t1 = System.currentTimeMillis();
        vibrator.vibrate(1900);

        mHandler.postDelayed(new Runnable() {
            public void run() {
               check();
            }
        }, 1901);
    }

    if(event.getAction()==MotionEvent.ACTION_UP){
        if(flag == 0){
            t2 = System.currentTimeMillis();
            vibrator.cancel();
            calculate();
            aborted_flag = 1;
        }
    }

    return true;
}

private void check() {
    t2 = System.currentTimeMillis();
    Log.e("Hello","Inside Check");
    Log.e("Hello",""+aborted_flag);
    vibrator.cancel();
    if(aborted_flag==0){
        calculate();
        flag = 1;
    }
}

private void scanning() {
    textView.setText("Scanning");
}

private void calculate() {
    Log.e("t2-t1 ", t2-t1+"");

    if(t2-t1>=1900){
        Random r = new Random();
        int k = r.nextInt((5 - 0) + 1) + 0;
        textView.setText(str[k]);

        ////////////animation library code/////////////
        YoYo.with(Techniques.StandUp)
                .duration(700)
                .playOn(findViewById(R.id.text_view));
        ////////////////////////////////////////

        changeBackgroundColor(k);

        //textView.setTextColor(Color.parseColor("#00ff00"));
         flag = 0;
    }
    else{
        textView.setText("Aborted\n Try Again");
        relativeLayout.setBackgroundResource(R.color.red);
    }
}
public void changeBackgroundColor(final int k) {
    runOnUiThread(new Runnable(){
        public void run() {
            switch(k){
                case 0: relativeLayout.setBackgroundResource(R.color.blue);
                    break;
                case 1: relativeLayout.setBackgroundResource(R.color.pink);
                    break;
                case 2:;
                case 3: relativeLayout.setBackgroundResource(R.color.green);
                    break;
                default:relativeLayout.setBackgroundResource(R.color.yellow);
            }
        }
    });
}
3个回答

5

如果您从UI线程调用postDelayed,则您的代码将在UI线程上执行。

要使用不同的线程,请创建一个新线程:

Thread t = new Thread(new Runnable(){});
t.start();

但是如何等待1900毫秒呢? - Prashant Skywalker
线程 t = new Thread(new Runnable(){ @Override public void run() { try{ Thread.sleep(1900); check(); } catch(Exception e){ e.printStackTrace(); } } }); t.start(); 但这并没有帮助。实际上,如果他一直按住屏幕,calculate 方法就不会运行。 - Prashant Skywalker

1
你可以创建一个HandlerThread并使用它的looper来创建一个handler,然后使用postpostdelayed将内容发布到它上面。
public class myActivity extends Activity
{
    private HandlerThread loaderThread;
    private Handler loader;

    @override
    public void onResume()
    {
        super.onResume();

        if (loaderThread == null)
        {
            loaderThread = new HandlerThread("MyActivitySeparateThread");
            loaderThread.start();
            loader = new Handler(loaderThread.getLooper());
        }
    }

    @override
    public void onDestroy()
    {
        //Stop and release handler
        try {
            loader.removeCallbacksAndMessages(null);
            loader.dispose();
        } finally {
            loader = null;
        }

        //Stop and release Thread
        try {
            loaderThread.quit();
            loaderThread.dispose();
        } finally {
            loaderThread = null;
        }

        super.onDestroy();
    }

    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        switch(event.getAction())
        {
            case MotionEvent.ACTION_DOWN:
                {
                    scanning();
                    t1 = System.currentTimeMillis();
                    vibrator.vibrate(1900);

                    loader.postDelayed(new Runnable()
                    {
                        public void run()
                        {
                            check();
                        }
                    }, 1901);
                    break;
                }
            case MotionEvent.ACTION_UP:
                {
                    if (flag == 0)
                    {
                        t2 = System.currentTimeMillis();
                        vibrator.cancel();
                        calculate();
                        aborted_flag = 1;
                    }
                    break;
                }
        }

        return true;
    }
}

0
因为在mHandler.postDelayed中调用了check(),check()又调用了calculate(),最终调用了relativeLayout.setBackgroundResource()。UI只能从UIThread更改。
创建函数:
public void changeBackgroundColor(final int color) {
   runOnUiThread(new Runnable(){
        public void run() {
           relativeLayout.setBackgroundResource(color);
          }
    });
}

然后从您的calculate()函数中调用它。


我按照你说的修改了代码,并加上了一个小小的改动,即添加了aborted_flag。我的意愿是,如果用户在1900毫秒后仍未触摸屏幕,那么calculate()方法将运行并更改屏幕颜色。但现在发生的是,如果用户一直触摸屏幕,calculate方法就不会运行。 - Prashant Skywalker

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接