Android 如何在特定的秒数内执行函数

Android 如何在特定的秒数内执行函数,android,bluetooth,Android,Bluetooth,所以,基本上,我想通过蓝牙传输数据,但要使用开关模式。每个打开和关闭模式都有一段特定的时间,即5秒。 所以 我需要将一个函数执行5秒钟,然后在一个按钮单击方法中执行另一个函数。但我下面的代码并没有正常工作 public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);

所以,基本上,我想通过蓝牙传输数据,但要使用开关模式。每个打开和关闭模式都有一段特定的时间,即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);
                }
              }
            });
      }

你可以试试下面的方法

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();
    }
}

这个的用例是什么?在任何情况下,postDelayed的意思是“在5000ms内运行此代码”,所以它并不是您真正想要的。我想你唯一能做的就是打断一下。但是,无法保证通过蓝牙发送的数据在您发送数据的5秒内能够正确发送,这可能会导致数据损坏问题。我将缓冲区数据放在线程中,因此每次蓝牙套接字都会连接数据流。你有我可以在这里使用的特定中断的链接吗?我仍然不明白为什么你要将其限制为5秒运行时间,因为我想反复打开和关闭蓝牙。我想检查蓝牙设置为空闲模式时的数据损坏和功耗。这是为了我的研究。on-off的占空比可能会有所不同,而不仅仅是5秒。如果您在一个单独的线程中运行蓝牙处理代码(您应该是这样),那么您可能可以使用thread.interrupt()来告诉蓝牙线程停止所有处理(我相信)。嘿,它不起作用,您在自己的方法中调用函数是什么意思?private void yourMethodToExecuteForFiveSeconds(){//如果(canExecute){//这将仅在五秒钟内为真,则代码将显示在此处。yourMethodToExecuteForFiveSeconds();}