2012-02-01 51 views
0

我知道這已被要求多次推遲之前,但我有使其工作或搞清楚什麼工作最好的問題。在發送另一條消息之前,我需要在每條消息之間等待2-3秒。我看過並嘗試過處理程序,定時器和線程睡眠,我不確定哪一個最適合我的情況,或者如何使它正確工作。我對編程還很陌生,所以請對我輕鬆一點。孤男寡女已發送短信

// ---sends an SMS message--- 
private void sendSMS(String phoneNumber, String message) { 

    int i; 
    SmsManager sms = SmsManager.getDefault(); 
    int amount = 10; // just making 10 the default if the EditText has an 
         // invalid value 
    try { 
     amount = Integer.parseInt(smsamount.getText().toString()); 
    } catch (NumberFormatException smsamount) { 
    } 

    if (amount < 501) { 
     for (i = 0; i < amount; i++) { 
     sms.sendTextMessage(phoneNumber, null, message, sentPI, null); 
     } 
+2

blob代碼的目的是什麼?如果它不僅僅是需要回答你的問題,那麼_trim it down!_此外,短信是由用戶發起的行動發送的,你需要施加費率限制,還是別的什麼? – cdeszaq 2012-02-01 19:13:01

+0

@cdeszaq對不起,我修剪它,所以它只顯示代碼的發送短信部分。用戶將輸入他們想要發送給自己用於測試目的的一定數量的短信。默認值是10,但如果需要,它們可以輸入多達500。我遇到的問題是,如果他們輸入一個高數字,手機會嘗試立即發送它們並導致問題。如果我能將它們分開一兩秒鐘,它將有助於解決這個問題。 – Jasonwilliams10 2012-02-01 21:49:51

回答

0

也許這樣的事情。我沒有測試過它,但使用ScheduledExecutorService的想法應該是你想要的。

public class SMS extends Activity { 
    private final static OnClickListener EMPTY_ON_CLICK_LISTENER = new EmptyOnClickListener(); 
    TextView smsamount; 

    // ---sends an SMS message--- 
    private void sendSMS(String phoneNumber, String message) { 
     // just making 10 the default if the EditText has an invalid value 
     int amount = 10; 

     try { 
      amount = Integer.parseInt(smsamount.getText().toString()); 
     } catch (NumberFormatException smsamount) { 
      // Ignore 
     } 

     sendSMS(phoneNumber, message, amount); 
    } 

    // ---sends an SMS message--- 
    private void sendSMS(String phoneNumber, String message, int count) { 
     if (count >= 501) { 
      new AlertDialog.Builder(SMS.this).setTitle("Maximum amount of messages exceeded!") 
        .setMessage("Please enter 500 or less for the amount of messages") 
        .setNeutralButton("Ok", EMPTY_ON_CLICK_LISTENER).show(); 
      // Quit early when we know we can't go any further. 
      return; 
     } 

     String SENT = "SMS_SENT"; 
     PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0); 
     // ---when the SMS has been sent--- 
     registerReceiver(new SmsSentBroadcastReceiver(getBaseContext()), new IntentFilter(SENT)); 

     int delaySeconds = 3; 
     ScheduledExecutorService scheduler = new SmsScheduler().sendSmsMessages(phoneNumber, message, sentPI, delaySeconds, 
       count); 

     // You may cancel the scheduled messages with the scheduler. 
     // scheduler.shutdownNow(); 

     new AlertDialog.Builder(SMS.this).setTitle("Attention!") 
       .setMessage("Your messages will start sending shortly, please do not press the send sms button again") 
       .setNeutralButton("Ok", EMPTY_ON_CLICK_LISTENER).show(); 
    } 

    private static class SmsSentBroadcastReceiver extends BroadcastReceiver { 
     Context context; 

     public SmsSentBroadcastReceiver(Context context) { 
      this.context = context; 
     } 

     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      switch (getResultCode()) { 
      case Activity.RESULT_OK: 
       Toast.makeText(context, "SMS sent", Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
       Toast.makeText(context, "Generic failure", Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_NO_SERVICE: 
       Toast.makeText(context, "No service", Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_NULL_PDU: 
       Toast.makeText(context, "Null PDU", Toast.LENGTH_SHORT).show(); 
       break; 
      case SmsManager.RESULT_ERROR_RADIO_OFF: 
       Toast.makeText(context, "Radio off", Toast.LENGTH_SHORT).show(); 
       break; 
      } 
     } 
    } 

    private static class EmptyOnClickListener implements OnClickListener { 
     public void onClick(DialogInterface dialog, int which) { 
      // Does nothing 
     } 
    } 

    private static class SmsScheduler { 
     public ScheduledExecutorService sendSmsMessages(final String phoneNumber, final String message, 
       final PendingIntent sentIntent, int count, int delaySeconds) { 
      final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1); 

      final SmsManager sms = SmsManager.getDefault(); 

      // Create the task that will send a SMS message 
      final Runnable sender = new Runnable() { 
       public void run() { 
        sms.sendTextMessage(phoneNumber, null, message, sentIntent, null); 
       } 
      }; 

      // Schedule the messages to be sent at intervals of delaySeconds. 
      for (int i = 0; i < count; i++) { 
       scheduler.schedule(sender, delaySeconds * i, TimeUnit.SECONDS); 
      } 

      return scheduler; 
     } 
    } 
} 
+0

這工作就像我需要它,謝謝。 – Jasonwilliams10 2012-02-02 17:39:07

2
,如果你要使用的每個短信在2秒延時,使用ScheduledExecutorService的線程池

(1個線程可能是足夠的,因爲你不發送並行),並調用schedule方法的代碼發送短信。 對於每個調用,2秒(0,2,4,6,...)

希望它可以幫助提高延遲參數。