ボタンを押しながらメソッド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メソッド
方法正確
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);
}
}
あなたは 'onClickListener'を使用しようとすることができ'onTouchListener'の代わりに。これは、ボタンクリックの通常のリスナーです。 – Marcel50506
@ Marcel50506どうすればその問題を解決できますか? – Dominic
まず、UIスレッドで時間のかかる操作をしないでください。別のスレッドで実行します。次に、無限ループをさらに調査します。メインスレッドの外側でさえ、これは起こりません。 – mhenryk