How to close the SMS Activity after sending the SMS successfully

Viewed 1146

I am using "SMS Intent" to send the SMS, I am not able to exit the SMS screen after successful completion of the SMS. here is the source code.

//Sending SMS to multiple phone numbers
public void sendSms(Context context, String text, String numbers) {
    Uri uri = Uri.parse("sms:" + numbers)
    Intent intent = new Intent();
    intent.setData(uri);
    intent.putExtra(Intent.EXTRA_TEXT, text);
    intent.putExtra("sms_body", text);
    intent.putExtra("address", numbers);
    intent.putExtra("exit_on_sent", true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        intent.setAction(Intent.ACTION_SENDTO);
        String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context);
        if (defaultSmsPackageName != null) {
            intent.setPackage(defaultSmsPackageName);
        }
    } else {
        intent.setAction(Intent.ACTION_VIEW);
        intent.setType("vnd.android-dir/mms-sms");
    }

    try {
        context.startActivity(intent);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
    }
}

How can I close the SMS activity/screen? Can anyone help me to resolve this issue?

1 Answers

How to close the SMS Activity after sending the SMS successfully

Try this:

private int INVITE_COMPLETED = 1;

String message = "Hello";
Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
sendIntent.setData(Uri.parse("sms:" + "Mobile Number"));
sendIntent.putExtra("sms_body", message);
sendIntent.putExtra("exit_on_sent", true);
startActivityForResult(sendIntent, INVITE_COMPLETED);

Above code completely worked for me. After sending SMS it returns my activity.

You may also take a look: i found solution here

I've tried by using your code. Here it's work fine.

Below Your Code:

public void sendSms(Context context, String text, String numbers) {
    Uri uri = Uri.parse("sms:" + numbers);
    Intent intent = new Intent();
    intent.setData(uri);
    intent.putExtra(Intent.EXTRA_TEXT, text);
    intent.putExtra("sms_body", text);
    intent.putExtra("address", numbers);
    intent.putExtra("exit_on_sent", true);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        intent.setAction(Intent.ACTION_SENDTO);
        String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(context);
        if (defaultSmsPackageName != null) {
            intent.setPackage(defaultSmsPackageName);
        }
    } else {
        intent.setAction(Intent.ACTION_VIEW);
        intent.setType("vnd.android-dir/mms-sms");
    }

    try {
        startActivityForResult(intent, INVITE_COMPLETED);
    } catch (ActivityNotFoundException e) {
        e.printStackTrace();
    }
}

For multiple numbers below code i used & it works fine:

 sendSms(MainActivity.this, "Hello", "Num1;Num2");
Related