postDelayed阻止ui线程

时间:2015-01-28 20:10:31

标签: android button motionevent postdelayed

我想要的是什么:用户点击按钮的时间为1900毫秒。如果他在1900ms之前捡起手指,手机会停止振动。如果他将手指放在按钮上超过1900秒,则 calculate()方法会运行。 我正在使用:postDelayed因为我读它不会干扰ui线程。 我试图检查是否已经过了1900秒用户没有选择他的手指,甚至比计算方法运行。 错误Happenin g:如果用户在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 个答案:

答案 0 :(得分:3)

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

要使用其他线程,请创建一个:

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

答案 1 :(得分:0)

因为在mHandler.postDelayed中调用check(),调用calculate()调用relativeLayout.setBackgroundResource()。 UI只能从UIThread更改。

制作功能:

public void changeBackgroundColor(final int color) {
   runOnUiThread(new Runnable(){
        public void run() {
           relativeLayout.setBackgroundResource(color);
          }
    });
}

并从您的calculate()函数调用它。

答案 2 :(得分:0)

您可以创建一个HandlerThread并使用它的循环程序来创建处理程序,然后将内容发布到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;
    }
}