Sending SMS and MMS from Your Application Using Intents and the Native Client

In many circumstances you may find it easier to pass on the responsibility for sending SMS and MMS messages to another application, rather than implementing a full SMS client within your app.

To do so, call startActivity using an Intent with the Intent.ACTION_SENDTO action. Specify a target number using sms: schema notation as the Intent data. Include the message you want to send within the Intent payload using an sms_body extra, as shown in Listing 12-13.

LISTING 12-13: Sending an SMS message using Intents

Intent smslntent = new Intent(Intent.ACTION_SENDTO,

Uri.parse("sms:55512345")); smsIntent.putExtra("sms_body", "Press send to send me"); startActivity(smslntent);

You can also attach files (effectively creating an MMS message) to your messages. Add an intent.EXTRA_STREAM with the URI of the resource to attach, and set the Intent type to the mime-type of the attached resource.

Note that the native MMS application doesn't include an Intent Receiver for ACTION_SENDTO with a type set. Instead, you will need to use ACTION_SEND and include the target phone number as an address extra, as shown in Listing 12-14.

Available for download on

LISTING 12-14: Sending an MMS message with an attached image

Available for // Get the URI of a piece of media to attach.

Wrox°comn Uri attached_Uri = Uri.parse("content://media/external/images/media/1"); // Create a new MMS intent

Intent mmsIntent = new Intent(Intent.ACTION_SEND, attached_Uri); mmsIntent.putExtra("sms_body", "Please see the attached image"); mmsIntent.putExtra("address", "07912355432"); mmsIntent.putExtra(Intent.EXTRA_STREAM, attached_Uri); mmsIntent.setType("image/png"); startActivity(mmsIntent);

When running the MMS example shown in Listing 12-14, users are likely to be prompted to select one of a number of applications capable of fulfilling the send request, including the Gmail, e-mail, and SMS applications.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment