2016-12-09 38 views
-3

這是我的代碼發送多設備短信。但是短信只發送給第一個號碼四次。我想發送短信給存儲在ArrrayList中的所有用戶。發送短信到多設備

public class SendSMSActivity extends Activity { 

    Button buttonSend; 

    String smsBody = "Message from the API"; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    buttonSend = (Button) findViewById(R.id.buttonSend); 
    textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo); 
    textSMS = (EditText) findViewById(R.id.editTextSMS); 

    final ArrayList <String> phone=new ArrayList<String>(); 
    phone.add("9742504034"); 
    phone.add("9535179695"); 
    phone.add("9742504034"); 
    phone.add("7204860021"); 

    buttonSend.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 



     try { 

      for(int i = 0; i < phone.size(); i++) 
      { 
      SmsManager smsManager = SmsManager.getDefault(); 

      smsManager.sendTextMessage(String.valueOf(phone), null, smsBody, 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(); 
      } 

     } 
    }); 
    } 
} 
+2

請了解一些Java的基礎... ['smsManager.sendTextMessage(將String.valueOf(手機),...)']( http://ideone.com/2I7FIu)<=這不是我們如何訪問數組元素... – Selvin

回答

0

也許您的for循環應該如下:

SmsManager smsManager = SmsManager.getDefault(); 
for(String number: phone) 
{   
    smsManager.sendTextMessage(number, null, smsBody, null, null); 
    Toast.makeText(getApplicationContext(), "SMS Sent!", Toast.LENGTH_LONG).show(); 
}