如何用延迟android工作室向一个数字循环发送一条消息



我想问一下我写的这段代码
Tt应该每5秒向相同的号码发送一条消息。我的问题是,当我运行它时,它会倒计时,然后发送消息。它不做5秒倒计时。

        start.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

                        String nums=loopnum.getText().toString();
                        int i = Integer.parseInt(nums);
                        int n = 1;
                    while ( n <= i) {
                                new CountDownTimer(5000, 1000) {
                                    public void onTick(long millisUntilFinished) {
                                        Toast.makeText(getApplicationContext(),"seconds remaining: " + millisUntilFinished / 1000, Toast.LENGTH_LONG).show();
                                    }
                                    public void onFinish() {
                                        // Toast.makeText(getApplicationContext(),"done!", Toast.LENGTH_LONG).show();
                                        String number = phonenumber.getText().toString();
                                        String sms = smstext.getText().toString();
                                        try {
                                            SmsManager smsManager = SmsManager.getDefault();
                                            smsManager.sendTextMessage(number, null, sms, null, null);
                                            Toast.makeText(getApplicationContext(), "SMS Sent!",
                                                    Toast.LENGTH_LONG).show();

                                        }
                                        catch (Exception e) {
                                            Toast.makeText(getApplicationContext(),
                                                    "SMS faild, please try again later!",
                                                    Toast.LENGTH_LONG).show();
                                            e.printStackTrace();
                                        }
                                    }
                                }.start();
                                n++;
                        }
        }
    });

这是因为while循环在主线程上运行,倒计时在单独的线程上运行。因此,从本质上讲,您已经同时启动了所有计时器。

使用这个替代:

String nums = loopnum.getText().toString();
int i = Integer.parseInt(nums);
int n = i * 5000;
new CountDownTimer(n, 5000) {
    public void onTick(long millisUntilFinished) {
           Toast.makeText(getApplicationContext(),"seconds remaining: " + millisUntilFinished / 1000, Toast.LENGTH_LONG).show();
           String number = phonenumber.getText().toString();
           String sms = smstext.getText().toString();
           try {
                  SmsManager smsManager = SmsManager.getDefault();
                  smsManager.sendTextMessage(number, null, sms, null, null);
                  Toast.makeText(getApplicationContext(), "SMS Sent!",
                  Toast.LENGTH_LONG).show();
               }
               catch (Exception e) {
                                       Toast.makeText(getApplicationContext(),
                                          "SMS faild, please try again later!",
                                       Toast.LENGTH_LONG).show();
                                       e.printStackTrace();
                                }
    }
    public void onFinish() {
            // Toast.makeText(getApplicationContext(),"done!", Toast.LENGTH_LONG).show();
    }
}.start();

最新更新