Java 如何在 Android Studio 中使用延迟功能?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37323110/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:12:43  来源:igfitidea点击:

How to use delay functions in Android Studio?

javaandroid

提问by R.Sahin Altunbas

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

我想用蓝牙发送一个字符。当只有一个字符时,代码运行良好。但我想在两个代码之间使用延迟函数。我想用 EditText 输入任何数字,应用程序将采用该数字并执行 EditText/44。这就是我想在 2 个代码之间等待的

Finally work.. Thanks guys. :)

终于工作了..谢谢大家。:)

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

我在 setOnClick.. 中移动了 a,b,c

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

采纳答案by Vanraj Ghed

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

回答by Mohit Charadva

You can do like this

你可以这样做

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");
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                sendData("S");    
            }
        }, c);    
    }
});

回答by Krishna

You can use handler

您可以使用处理程序

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

            @Override
            public void run() {
                //do something
            }
        }, 2000 );//time in milisecond

回答by Iamat8

just use runOnUiThreadon button click and post Handlerafter time delay..

只需使用runOnUiThread按钮单击并Handler在延时后发布..

  Button button = new Button(this);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            sendData("F");                   // send
            delay(2000);
        }
    });

UPDATE

更新

delay()..

延迟()..

 public void delay(final int c){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(c);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    });
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            sendData("S");           //send
        }
    }, c);

}

回答by Jay

Use handler like this:

像这样使用处理程序:

new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            // do something after 2s = 2000 miliseconds
        }
    }, 2000); //Time in milisecond