2

I'm using following code for sending text message via Intent ( can not ask for permission so smsmanager is not an option)

//Code from this question 
//  <http://stackoverflow.com/questions/20079047/android-kitkat-4-4-hangouts-cannot-handle-sending-sms-intent>
    private void sendsms(String toContact, String text){
        Intent intent;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) // Android 4.4 and up
        {
            String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(this);


            intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + Uri.encode(toContact)));
            intent.putExtra("sms_body", text);

            if (defaultSmsPackageName != null) // Can be null in case that there is no default, then the user would be able to choose any app that supports this intent.
            {
                intent.setPackage(defaultSmsPackageName);
            }
        }
        else
        {
            intent = new Intent(Intent.ACTION_VIEW);
            intent.setType("vnd.android-dir/mms-sms");
            intent.putExtra("address", toContact);
            intent.putExtra("sms_body", text);
        }
        this.startActivity(intent);
    }

and I'm calling this in a for loop :

for(int i = 0; i<4 ;i++) {
    sendsms(phoneNo[i],smsBody[i]);
   }

Now the problem is whenever user gets to this line, the void will be called 4 times, but user will only see the last message in the devices default messaging app ready to be sent, but to get to the other ones, user should press back on the device and if not, he/she would never see the other messages.

what I need to be done is using a method like startActivityForResult(); so each time user sends the message he would be redirected to my app, and then my app starts another activity for the next text message.

any idea?

Thanks in advance

Muhammad Naderi
  • 3,090
  • 3
  • 24
  • 37

1 Answers1

0

First of all you need to create a String with your phones, for that you have to use "; " as separator, but take care with Samsung, in those devices you have to use ", ".

So your code has to be similar to this one:

private void sendSms(String phone1, String phone2){

    String separator = "; "

    if (Build.MANUFACTURER.toLowerCase().contains("samsung"))
          separator = ", "

    String phones = phone1 + separator + phone2...

    Intent intentSms = new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", phones, null));

    intentSms.putExtra("sms_body","your text") // Just if you want to add text 

    startActivity(intentSms);
}
javisilox
  • 95
  • 9