R.Sahin Altunbas R.Sahin Altunbas - 6 months ago 231
Java Question

How to use delay functions in Android Studio?

I have a problem. I hope you can help me. Thanks.
I should send a character with bluetooth. It send perfectly when only one character.But I want to use delay function between 2 code.

Like that ;

kileri = (Button) findViewById(R.id.kileri);
final EditText value1 = (EditText) findViewById(R.id.textkont);
assert value1 != null;
value1.setText("0");
final int a = Integer.parseInt(value1.getText().toString());
final int b = a/22;
final int c = b/2; // It will take a int from Edittext and do this operation on that.


btAdapter = BluetoothAdapter.getDefaultAdapter();
checkBTState();

kileri.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
sendData("F");
delay(c); // When i click button, it send "F" character and wait for c seconds and send "S" character.
sendData("S");
}
});


How can i do that ? I try lots of things but i failure.

Answer
 try {
       //set time in mili
        Thread.sleep(3000);

    }catch (Exception e){
        e.printStackTrace();
    }

edited as your code

 kileri.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            sendData("F");
            try {
                //set time in mili
                Thread.sleep(3000);

            }catch (Exception e){
                e.printStackTrace();
            }
            sendData("S");
        }
    });
Comments