Recipe 5.1 Sending SMS Messages Through the Built-in Messaging Application
In this recipe, you learn how to send SMS messages using the built-in Messaging application installed on all Android phones. Using this approach enables you to “send and forget” — all you need to do is invoke the Messaging application using an Intent object and pass it the necessary pieces of information, such as the recipient’s phone number and the message. Users see the familiar user interface of the Messaging application on their device, saving you the need to create a separate UI for entering the phone number, message, and so on.
Solution
To invoke the built-in Messaging application on your Android device to send an SMS message, you need to use an Intent object together with the startActivity() method. The following code shows how to invoke the built-in Messaging app:
package net.learn2develop.messagingapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(android.content.Intent.ACTION_VIEW);
i.putExtra("address", "5556; 5558; 5560");
i.putExtra("sms_body", "Greetings!");
i.setType("vnd.android-dir/mms-sms");
startActivity(i);
}
}
Take ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access