Ed Holloway-George
Ed Holloway-George

Reputation: 5149

Get SMS activity result from default SMS app

I have created the following method to open a device's default SMS application and pre-populate it with a message and (optionally) a phone number.

What I would like to know is how can I get a RESULT_OK returned from the SMS activity only when the SMS has been sent? Currently I get a RESULT_CANCELLED when I exit the SMS activity as I would probably expect.

Is there an easy way to determine if an SMS was sent on returning to my application?

public static void sendSMS(Activity activity, String message, String phoneNumber){

        boolean hasPhoneNumber = phoneNumber != null;

        if(message == null){
            showSMSError(activity, null);
            return;
        }

        Intent smsIntent;

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT){
            smsIntent = new Intent(Intent.ACTION_SENDTO);
            //Ensures only SMS apps respond
            smsIntent.setData(Uri.parse("smsto:" + (hasPhoneNumber ? phoneNumber : "")));
            //No resolvable activity
            if (smsIntent.resolveActivity(activity.getPackageManager()) == null) {
                showSMSError(activity, null);
                return;
            }
        }else{
            //Old way of accessing sms activity
            smsIntent = new Intent(Intent.ACTION_VIEW);
            smsIntent.setType("vnd.android-dir/mms-sms");
            if(hasPhoneNumber) {
                smsIntent.putExtra("address", phoneNumber);
            }
            smsIntent.putExtra("exit_on_sent", true);
        }
        smsIntent.putExtra("sms_body", message);
        activity.startActivityForResult(smsIntent, SMS_ACTIVITY_REQUEST);
    }

Upvotes: 0

Views: 700

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007614

What I would like to know is how can I get a RESULT_OK returned from the SMS activity only when the SMS has been sent?

Write your own SMS client app, which implements its ACTION_SENDTO and ACTION_VIEW activity (or activities) in a fashion that supports startActivityForResult(). Those Intent actions are not documented as having results, which is why few, if any, existing SMS clients will set a result.

Upvotes: 3

Related Questions