如何执行特定秒数的功能

时间:2013-11-28 02:37:26

标签: android bluetooth

所以基本上,我想从蓝牙流式传输数据,但是有开关模式。每个&关闭模式发生了一段特定的时间,即5秒。 所以, 我需要将一个函数执行5秒钟,然后在按钮单击方法中执行另一个函数。但是我的下面的代码不能正常工作。

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        btnOn = (Button) findViewById(R.id.btnOn);                  // button LED ON
        btnOff = (Button) findViewById(R.id.btnOff);                // button LED OFF
        btnStart = (Button) findViewById(R.id.btnStart);
        txtArduino = (TextView) findViewById(R.id.txtArduino);      // for display the received data from the Arduino

        h = new Handler() {
            public void handleMessage(android.os.Message msg) {
                switch (msg.what) {
                case RECIEVE_MESSAGE:                                                   // if receive massage
                    byte[] readBuf = (byte[]) msg.obj;
                    String strIncom = new String(readBuf, 0, msg.arg1);                 // create string from bytes array
                    sb.append(strIncom);                                                // append string
                    int endOfLineIndex = sb.indexOf("\r\n");                            // determine the end-of-line
                    if (endOfLineIndex > 0) {                                           // if end-of-line,
                        String sbprint = sb.substring(0, endOfLineIndex);               // extract string
                        sb.delete(0, sb.length());                                      // and clear
                        txtArduino.setText("Data from Arduino: " + sbprint);            // update TextView
                        btnOff.setEnabled(true);
                        btnOn.setEnabled(true); 
                    }
                    //Log.d(TAG, "...String:"+ sb.toString() +  "Byte:" + msg.arg1 + "...");
                    break;
                }
            };
        };

        btAdapter = BluetoothAdapter.getDefaultAdapter();       // get Bluetooth adapter
        checkBTState();

        btnOn.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
            btnOn.setEnabled(false);
            bluetoothConnect();
          }
        });

        btnOff.setOnClickListener(new OnClickListener() {
          public void onClick(View v) {
            btnOff.setEnabled(false);  
            bluetoothDisconnect();
          }
        });

        btnStart.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                bluetoothDisconnect();
                    //Starts from here the code doesn't work
                    //I want to perform these functions over and over
                while (true){
                    //I want method executed for 5 seconds here
                    h.postDelayed(new Runnable() {
                        @Override
                        public void run(){
                            bluetoothConnect();
                        }
                    }, 5000);
                    //I want method executed for 5 seconds here
                    h.postDelayed(new Runnable() {
                        @Override
                        public void run(){
                            bluetoothDisconnect();
                        }
                    }, 5000);
                }
              }
            });
      }

1 个答案:

答案 0 :(得分:0)

您可以尝试以下方式......

private boolean canExecute;

private void callYourMethod() {
    canExecute = true;
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            canExecute = false;
        }
    }, 5000);
    yourMethodToExecuteForFiveSeconds();
}

private void yourMethodToExecuteForFiveSeconds() {
    // Your code goes here

    if (canExecute) {
        //This will be true for five seconds only.
        yourMethodToExecuteForFiveSeconds();
    }
}