发送短信的意图不再适用于 Android 11 [英] Intent for sending text messages no longer working on Android 11

查看:90
本文介绍了发送短信的意图不再适用于 Android 11的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当面向 Android 11 并使用 Android 11 设备(物理或模拟器)时,以下方法不再适用于发送短信.更改目标 SDK 版本或设备 SDK 版本使其工作.logcat 什么也没说.知道为什么它不起作用吗?

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);
            }
        }
    }

我发现我可以通过添加这个权限来解决这个问题:

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?

推荐答案

我想通了.我需要将此添加到我的清单中:

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>

这篇关于发送短信的意图不再适用于 Android 11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆