按下按钮时重复调用方法

时间:2016-08-26 14:16:00

标签: android performance

我想在按下按钮时调用方法sendDate。 但不幸的是,我得到一个无限循环,我的应用程序冻结。 也许我可以尝试使用处理程序,但我不确定。

controllerButton.setOnTouchListener(new OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            while (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                if (liftStatus == LIFT_IS_UP) {
                    sendData(3);
                } else if (liftStatus == LIFT_IS_DOWN) {
                    sendData(1);
                }
            }

            if (liftStatus == LIFT_IS_UP) {
                sendData(4);
                return true;
            } else if (liftStatus == LIFT_IS_DOWN) {
                sendData(2);
                return true;
            }

            return false;
        }
    }
};

sendData方法

方法是控制通过蓝牙LE连接的升降机

private void sendData(int command){
        if (myBluetoothGattCharacteristic != null) {
            switch(command) {
                case 1:
                    myBluetoothGattCharacteristic.setValue("Lift_UP_START");
                    break;
                case 2:
                    myBluetoothGattCharacteristic.setValue("Lift_UP_STOP");
                    break;
                case 3:
                    myBluetoothGattCharacteristic.setValue("Lift_DOWN_START");
                    break;
                case 4:
                    myBluetoothGattCharacteristic.setValue("Lift_DOWN_STOP");
                    break;
                default:
                    return;
            }
            myGatter.writeCharacteristic(myBluetoothGattCharacteristic);
        }
    }

3 个答案:

答案 0 :(得分:2)

问题出在while statement上,因为一旦您收到带有ACTION_DOWN事件的动作事件,motionEvent.getAction()的值将永远相同,直到此函数结束,因此您被困在你的循环中,事件的值永远不会在循环中改变,因此无限循环

while(motionEvent.getAction() == MotionEvent.ACTION_DOWN){ // unwanted while ,  remove this while loop , use if-else or switch ,
//like if(motionEvent.getAction() == MotionEvent.ACTION_DOWN){
// check your up and down}

        if (liftStatus == LIFT_IS_UP){
            sendData(3);
         }else if (liftStatus == LIFT_IS_DOWN ){
            sendData(1);
         }
     }

答案 1 :(得分:0)

完全。我也会用一个处理程序来做。

private Handler mHandler = new Handler();
private final Runnable mConnectionCheck = new Runnable() {
    @Override
    public void run() {
        mHandler.postDelayed(this, 1000);
    }
};

public void sendData(int value) {
    mHandler.post(mConnectionCheck);
}

答案 2 :(得分:0)

如前所述,您不能在触摸侦听器中使用while循环。这不允许回调返回并冻结你的ui。从您发布的代码开始,我会使用Handler继续执行Runnable

        btn.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
                    handler.post(new Runnable()
                    {
                        @Override
                        public void run()
                        {
                            handler.post(new MyRunnable({some_value}));
                        }
                    });

                    return false;
                }
                else
                {
                    handler.removeCallbacksAndMessages(null);
                    return false;
                }
            }
        });

并在您正在使用它的课程中:

private Handler handler;

public class MyRunnable implements Runnable
{
    private int value;

    public MyRunnable(int value)
    {
        this.value = value;
    }

    public void setValue(int value)
    {
        this.value = value;
    }

    @Override
    public void run()
    {
        sendData(value);
        <check if you need to change this.value>
        handler.post(this);
    }
}