如何在发送消息时在Android Studio中打破while循环

问题描述 投票:0回答:2

我有一个聊天应用程序。我必须只发送一次消息,而我正在使用while(true)。由于此循环消息一次又一次地发送。当消息只发送一次时,如何打破while循环?不应该一次又一次地发送该消息。

这是我的代码:

while (true) {
    String[] jobsNumbers = numbers.toArray(new String[numbers.size()]);
    new SendMessage().execute(jobsNumbers);
}

这是我的sendMessage Asynctask:

private class sendMessage extends AsyncTask<String, Integer, Integer> {

    @Override
   protected Integer doInBackground(String[] toNumbers) {
       totalNumbers = toNumbers.length;
       for (i = 0; i < toNumbers.length; i++) {
           toNumber = toNumbers[i];
           if (countMessagePerDay() >= Integer.parseInt(MaxNoOfMessagePerDay)) {
               try {
                   Thread.sleep(10000);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           } else {
               sendSMS(toNumber);
               this.publishProgress(1);
           }

           if (randomNo != 0 && isSent) {
               databaseHelperClass.updateJobMessageStatus(JobId);
              }
       }
    return toNumbers.length;
}
    @Override
    protected void onProgressUpdate(Integer... values) {
        Toast.makeText(getApplicationContext(), i + "/" + totalNumbers + " :: to " + toNumber, Toast.LENGTH_SHORT).show();
        super.onProgressUpdate(values);
    }

    @Override
    protected void onCancelled(Integer integer) {
        super.onCancelled(integer);
    }

    @Override
    protected void onPostExecute(Integer integer) {
        Toast.makeText(getApplicationContext(), "Message sent!", Toast.LENGTH_LONG).show();
        super.onPostExecute(integer);
    }
}
android sqlite android-studio sms smsmanager
2个回答
0
投票

您可以使用break;,例如:

while (true) {
String[] jobsNumbers = numbers.toArray(new String[numbers.size()]);
new SendMessage().execute(jobsNumbers);
Boolean destroy= true;
 if(destroy == true){
 break;
} 

}

0
投票
int i = 0;
while (i<1) 
{
  String[] jobsNumbers = numbers.toArray(new String[numbers.size()]);
  new SendMessage().execute(jobsNumbers);
  i++;
}
© www.soinside.com 2019 - 2024. All rights reserved.