简体   繁体   English

将意图发送到短信应用Android 4.4及更高版本

[英]Send intent to sms app android 4.4 and above

I'm using this code to send intent default sms app in my application 我正在使用此代码在应用程序中发送意图默认的短信应用程序

Intent smsIntent = new Intent(Intent.ACTION_VIEW);
        smsIntent.setType("vnd.android-dir/mms-sms");
        smsIntent.putExtra("address", number);
        smsIntent.putExtra("sms_body",message);

        if (smsIntent.resolveActivity(context.getPackageManager()) != null) {
            context.startActivity(smsIntent);
        }

This code open an sms app in android 5 (and probably in kitkat!) but it may or may not be the default sms app! 这段代码在android 5中打开了一个短信应用程序(可能在kitkat中!),但它可能不是默认的短信应用程序! So sometimes when it does not open default sms app i get an error because only default app can send sms! 因此,有时当它没有打开默认的短信应用程序时,我会收到错误消息,因为只有默认的应用程序才能发送短信!

my question is how to insure only default app handles my intents ? 我的问题是如何确保只有默认应用程序才能处理我的意图?

try adding this- 尝试添加此-

smsIntent.addCategory(Intent.CATEGORY_DEFAULT); 

this should ensure the recipient app is the default one. 这应确保收件人应用是默认应用。

Or alternatively for KK, you could try this- 或者,对于KK,您可以尝试以下操作-

SmsManager.getDefault().sendTextMessage("Phone Number", null, "Message", null, null);

NB: Using this method requires that your app has the SEND_SMS permission. 注意:使用此方法要求您的应用具有SEND_SMS权限。

For further information, please see sendTextMessage() documentation 有关更多信息,请参见sendTextMessage()文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM