Intent for sending text messages no longer working on Android 11

Viewed 2080

When targeting Android 11 and using an Android 11 device (physical or emulator), the below method no longer works for sending a text message. Changing either the target SDK version or the device SDK version makes it work. The logcat says nothing at all. Any idea why it isn't working?

    private void sendTextMessage() {
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        String phoneNumber = prefs.getString(KEY_USER_SELECTED_PHONE_NUMBER, getString(R.string.enter_phone_number_default_text));
        intent.setData(Uri.parse("smsto:" + phoneNumber));  // This ensures only SMS apps respond

        intent.putExtra("sms_body", fetchTextMessageString());
        if (intent.resolveActivity(getPackageManager()) != null) {
            try {
                startActivity(intent);
            } catch (ActivityNotFoundException anfe) {
                Utilities.showLogError(LOG_TAG, anfe, "Couldn't find SMS activity");
                showSnackbar(MainActivity.this, null, 4500, true, new SpannableString("No SMS app installed on your device"), false, "DISMISS", null);
            }
        }
    }

EDIT: I've found that I can fix this by adding this permission:

<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />

...but is there a more specific query I can use for sending text messages?

1 Answers

I figured it out. I needed to add this to my manifest:

    <queries>
        <intent>
            <action android:name="android.intent.action.SENDTO" />
            <data android:scheme="smsto"/>
        </intent>
    </queries>
Related