Java Android4.4 无法使用“vnd.android-dir/mms-sms”处理短信意图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19853220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 20:42:58  来源:igfitidea点击:

Android4.4 can not handle sms intent with "vnd.android-dir/mms-sms"

javaandroidandroid-intentsmsandroid-4.4-kitkat

提问by bluemotion

My application has a button to start default sms activity and it worked perfectly fine all android version except new one, Android 4.4(kitkat) Here is the code:

我的应用程序有一个按钮来启动默认的短信活动,除了新的 Android 4.4(kitkat) 之外,它在所有 android 版本上都运行良好,这是代码:

public void onClick(View arg0) {
    Intent smsIntent = new Intent(Intent.ACTION_VIEW);
    smsIntent.setType("vnd.android-dir/mms-sms");
    smsIntent.putExtra("address", member.getPhoneNumber().trim());
    context.startActivity(smsIntent);
}

And I get error messages

我收到错误消息

11-08 02:08:32.815: E/AndroidRuntime(14733): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW typ=vnd.android-dir/mms-sms (has extras) }

I know that google made some changes on how the default sms app handles sms intents. but my app is not a sms app but it only has function to start default sms app with recipient number. so please help.

我知道谷歌对默认短信应用程序处理短信意图的方式进行了一些更改。但我的应用程序不是短信应用程序,但它仅具有使用收件人号码启动默认短信应用程序的功能。所以请帮忙。

采纳答案by AdamK

To start the SMS app with number populated use action ACTION_SENDTO:

要使用填充号码启动 SMS 应用程序,请执行以下操作ACTION_SENDTO

Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("smsto:" + Uri.encode(phoneNumber)));
startActivity(intent);

This will work on Android 4.4. It should also work on earlier versions of Android however as the APIs were never public the behavior might vary. If you didn't have issues with your prior method I would probably just stick to that pre-4.4 and use ACTION_SENDTOfor 4.4+.

这将适用于 Android 4.4。它也应该适用于早期版本的 Android,但是由于 API 从未公开,因此行为可能会有所不同。如果您之前的方法没有问题,我可能会坚持使用 4.4 之前的版本并ACTION_SENDTO用于 4.4+。

回答by mohsen

to start the sms app without insert the number, you should delete setType

要在不插入号码的情况下启动短信应用程序,您应该删除 setType

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("sms:"));
intent.putExtra("sms_body", "smsMsgVar");
startActivity(intent);

回答by Suhad Bin Zubair

The issue happened when you are testing with Emulator,

当您使用模拟器进行测试时发生了问题,

Please test with the actual device.

请使用实际设备进行测试。