简体   繁体   中英

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! 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-

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

NB: Using this method requires that your app has the SEND_SMS permission.

For further information, please see sendTextMessage() documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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