I am trying to send a text sms to all the contact numbers in my ArrayList
. My code is as follows:
try{
SmsManager sms = SmsManager.getDefault();
if (customarray.size() != 0) {
Log.d("contact",String.valueOf(customarray)); //shows all the contact numbers
for (int i = 0; i < customarray.size(); i++) {
Log.d("contact",String.valueOf(customarray.get(i))); //shows each of the contact numbers
sms.sendTextMessage(customarray.get(i), null, string, null, null);
Toast.makeText(contacts.this,"Your sms has been sent to "+customarray.get(i),Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}
I have also added the permission in my manifest file:
<uses-permission android:name="android.permission.SEND_SMS" />
Here, customarray
contains all the contact numbers. Even though the toast shows up, that "Your sms has been sent to...", but the sms never gets sent. Why?
As suggested by @Joachim Sauer, using PendingIntent
works:
try{
SmsManager sms = SmsManager.getDefault();
Intent intent=new Intent(getApplicationContext(),contacts.class);
PendingIntent pi= PendingIntent.getActivity(getApplicationContext(), 0, intent,0);
if (customarray.size() != 0) {
Log.d("contact",String.valueOf(customarray)); //shows all the contact numbers
for (int i = 0; i < customarray.size(); i++) {
Log.d("contact",String.valueOf(customarray.get(i))); //shows each of the contact numbers
sms.sendTextMessage(customarray.get(i), null, string, pi, null);
Toast.makeText(contacts.this,"Your sms has been sent to "+customarray.get(i),Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
e.printStackTrace();
}