Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use delay functions in Android Studio?

Tags:

java

android

I want to send a character with Bluetooth. The code works perfectly when there is only a single character.But I want to use a delay function between the two codes. I want to enter any number with EditText and the app will take that number and do EditText/44. That is what I want to wait between 2 codes

Finally work.. Thanks guys. :)

I moved a,b,c inside setOnClick.. ;

kileri = (Button) findViewById(R.id.kileri);
final EditText value1 = (EditText) findViewById(R.id.textkont);
assert value1 != null;
value1.setText("0");


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

kileri.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            int a = Integer.parseInt(value1.getText().toString());
            int b = a / 44;
            int c = b * 1000;

            sendData("F");

            try {
                Thread.sleep(c);

            } catch (Exception e) {
                e.printStackTrace();
            }
like image 751
R.Sahin Altunbas Avatar asked May 19 '16 12:05

R.Sahin Altunbas


2 Answers

You can use handler

new Handler().postDelayed(new Runnable() {

            @Override
            public void run() {
                //do something
            }
        }, 2000 );//time in milisecond
like image 143
Krishna Avatar answered Nov 10 '22 03:11

Krishna


 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");
        }
    });
like image 5
Vanraj Ghed Avatar answered Nov 10 '22 02:11

Vanraj Ghed