如何使用SMS Intent发送短信?

时间:2015-10-28 17:59:00

标签: android android-intent

在我的Android应用程序中,我使用以下代码发送短信:

String defaultSmsPackageName = Telephony.Sms.getDefaultSmsPackage(this); // Need to change the build to API 19

             Intent sendIntent = new Intent(Intent.ACTION_SENDTO);
             sendIntent.setType("text/plain");
             sendIntent.putExtra(Intent.EXTRA_SUBJECT, "sujet");
             sendIntent.putExtra(Intent.EXTRA_TEXT, myMessage);
             sendIntent.setData(Uri.parse("smsto:"+phoneNumber));
             if (defaultSmsPackageName != null)// Can be null in case that there is no default, then the user would be able to choose
             // any app that support this intent.
             {
                sendIntent.setPackage(defaultSmsPackageName);
             }
             startActivity(sendIntent);

但是,当启动消息应用程序(例如环聊)时,我有正确的收件人,但消息(“myMessage”)不存在。如何将其置于Intent中以使其显示?

1 个答案:

答案 0 :(得分:0)

尝试按以下方式发送短信

String phoneNumber = "0123456789";
String message = "Hello World!";

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNumber, null, message, null, null);

详细了解文档中的SMS Manager