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

时间:2017-12-21 07:04:56

标签: android sqlite android-studio sms smsmanager

我有一个聊天应用程序。我必须只发送一次消息,而我正在使用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);
    }
}

2 个答案:

答案 0 :(得分:0)

您可以使用break;,例如:

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

}

答案 1 :(得分:0)

Django Channels