2010-08-30 33 views
4

使用smsManager類,我可以在同一時間發送相同的短信多個號碼?或者我需要做某種循環?在Android中是否可以向代碼中的多個收件人發送短信?

我想不是通過消息傳遞程序,從我的應用程序中發送短信。

+0

你嘗試發送由逗號隔開的數字? – Macarse 2010-08-30 12:48:03

+1

我試圖在模擬器上,它不工作只發送到第一個號碼,讓我試着在設備上看看。 – mbwasi 2010-08-30 12:56:36

+0

好的分離不起作用。 – mbwasi 2010-09-03 14:42:41

回答

6

這裏我附上源代碼,我所做的以編程方式發送消息。

短信:

public class SMS extends Activity { 

Button btnSendSMS; 
EditText txtPhoneNo; 
EditText txtMessage; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    btnSendSMS = (Button) findViewById(R.id.btnSendSMS); 
    txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo); 
    txtMessage = (EditText) findViewById(R.id.txtMessage); 

    btnSendSMS.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View v) 
     {     
      String message = txtMessage.getText().toString(); 
      String phoneNo = txtPhoneNo.getText().toString(); 

      StringTokenizer st=new StringTokenizer(phoneNo,","); 
      while (st.hasMoreElements()) 
      { 
       String tempMobileNumber = (String)st.nextElement(); 
       if(tempMobileNumber.length()>0 && message.trim().length()>0) { 
        sendSMS(tempMobileNumber, message); 
       } 
       else { 
        Toast.makeText(getBaseContext(), 
          "Please enter both phone number and message.", 
          Toast.LENGTH_SHORT).show(); 
       } 
      } 

//    if (phoneNo.length()>0 && message.length()>0)     
//     sendSMS(phoneNo, message);     
//    else 
//     Toast.makeText(getBaseContext(), 
//      "Please enter both phone number and message.", 
//      Toast.LENGTH_SHORT).show(); 
     } 
    }); 

} 
private void sendSMS(String phoneNumber, String message) 
{ 
    String SENT = "SMS_SENT"; 
    String DELIVERED = "SMS_DELIVERED"; 

    PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, 
     new Intent(SENT), 0); 

    PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0, 
     new Intent(DELIVERED), 0); 

    //---when the SMS has been sent--- 
    registerReceiver(new BroadcastReceiver(){ 
     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      switch (getResultCode()) 
      { 
       case Activity.RESULT_OK: 
        Toast.makeText(getBaseContext(), "SMS sent", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case SmsManager.RESULT_ERROR_GENERIC_FAILURE: 
        Toast.makeText(getBaseContext(), "Generic failure", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case SmsManager.RESULT_ERROR_NO_SERVICE: 
        Toast.makeText(getBaseContext(), "No service", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case SmsManager.RESULT_ERROR_NULL_PDU: 
        Toast.makeText(getBaseContext(), "Null PDU", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case SmsManager.RESULT_ERROR_RADIO_OFF: 
        Toast.makeText(getBaseContext(), "Radio off", 
          Toast.LENGTH_SHORT).show(); 
        break; 
      } 
     } 
    },new IntentFilter(SENT)); 

    //---when the SMS has been delivered--- 
    registerReceiver(new BroadcastReceiver(){ 
     @Override 
     public void onReceive(Context arg0, Intent arg1) { 
      switch (getResultCode()) 
      { 
       case Activity.RESULT_OK: 
        Toast.makeText(getBaseContext(), "SMS delivered", 
          Toast.LENGTH_SHORT).show(); 
        break; 
       case Activity.RESULT_CANCELED: 
        Toast.makeText(getBaseContext(), "SMS not delivered", 
          Toast.LENGTH_SHORT).show(); 
        break;       
      } 
     } 
    }, new IntentFilter(DELIVERED));   

    SmsManager sms = SmsManager.getDefault(); 
    sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);  
} 

} 

SmsReceiver:

public class SmsReceiver extends BroadcastReceiver { 

@Override 
public void onReceive(Context context, Intent intent) { 
    //---get the SMS message passed in--- 
    Bundle bundle = intent.getExtras();   
    SmsMessage[] msgs = null; 
    String str = ""; 
    if (bundle != null) 
    { 
     //---retrieve the SMS message received--- 
     Object[] pdus = (Object[]) bundle.get("pdus"); 
     msgs = new SmsMessage[pdus.length];    
     for (int i=0; i<msgs.length; i++) 
     { 
      msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);     
      str += "SMS from " + msgs[i].getOriginatingAddress();      
      str += " :"; 
      str += msgs[i].getMessageBody().toString(); 
      str += "\n";   
     } 
     //---display the new SMS message--- 
     Toast.makeText(context, str, Toast.LENGTH_SHORT).show(); 
    } 
} 
} 

和需要清單設置權限 READ_SMS,RECIEVE_SMS。 這就是你可以發送郵件到單用戶..

這是怎麼ü可以發送消息給多個用戶。 ............................................ 假設你需要發送給多個用戶的消息你可以編寫以逗號分隔的號碼 例如。 5558,5554。

這是如何u能使用 字符串標記在上面的代碼中使用

可以將此邏輯,其中u被調用SendSms功能和每次通過appropiate電話號碼類分離由逗號隔開的兩個數字

它適用於我.....可以試試.. 它回答你的問題?

感謝,

拉克什

+0

喜羅馬我也做應用程序與此能夠發送消息到多個模擬器...你可以試試看..它會工作,並寫數字以逗號分隔,例如。 5556,5558 .... – 2010-09-01 04:19:28

+2

謝謝你,這個作品很棒! – mbwasi 2010-09-03 14:42:14

+0

嗨羅馬,你滿意答案我發佈了,它能夠爲你工作,是它接受的答案,或需要一些更多的幫助,我會很樂意幫助..謝謝.Rakesh – 2010-09-04 04:42:05

相關問題