2016-08-26 7 views
1

ボタンを押しながらメソッド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); 
     } 
    } 
+0

あなたは 'onClickListener'を使用しようとすることができ'onTouchListener'の代わりに。これは、ボタンクリックの通常のリスナーです。 – Marcel50506

+0

@ Marcel50506どうすればその問題を解決できますか? – Dominic

+0

まず、UIスレッドで時間のかかる操作をしないでください。別のスレッドで実行します。次に、無限ループをさらに調査します。メインスレッドの外側でさえ、これは起こりません。 – mhenryk

答えて

0

を接続したBluetooth LEを介してであるリフトを制御することです。私はハンドラでもやります。

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

あなたはACTION_DOWNイベントにモーションイベントを受信したら、motionEvent.getAction()の値は、この関数の最後まで永遠に同じになるため、問題がループとの値ながら、したがって、あなたはあなたの中に閉じ込められているあなたのwhile statementですループ内でイベントが変更されることはありません。したがって、無限ループ

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

前述したように、タッチリスナー内でwhileループを使用することはできません。それはコールバックがあなたのUIを返してフリーズすることを許可しません。私はRunnableを実行し続けるHandlerを使用することになり、あなたが投稿したコード最低料金:

 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); 
    } 
} 
関連する問題