how to send sms in android application
The android.telephony package contains the SmsManager and SmsMessage classes. The SmsManager defines many important SMS-related constants, and also provides the sendDataMessage, sendMultipartTextMessage, and sendTextMessage methods. As long as your app is configured with proper permissions to send text messages (using the android.permission.SEND_SMS permission) you can send text messages to whoever you like SmsManager to send SMS messages
** UPDATE: Android Complete tutorial now available here.
Example:1
sendTextMessage (
String phoneNumber,
String serviceCenterAddress,
String text,
PendingIntent sentIntent,
PendingIntent deliveryIntent
)
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("9884788301", null, "hi simplecodestuffs!", sentPI, deliveredPI);
Sent Intent is a pending intent which will be triggered when sms is sent from your phone and delivery Intent is a pending intent which will be triggered when sms is delivered to the target phone.
Example:2
private SmsManager smsManager;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.smsexample);
// . . . other onCreate view item inflation omitted for brevity
smsSend = (Button) findViewById(R.id.smssend_button);
smsManager = SmsManager.getDefault();
final PendingIntent sentIntent =
PendingIntent.getActivity(
this, 0, new Intent(this,
SmsSendCheck.class), 0);
smsSend.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String dest = smsInputDest.getText().toString();
if (PhoneNumberUtils.
isWellFormedSmsAddress(dest)) {
smsManager.sendTextMessage(
smsInputDest.getText().toString, null,
smsInputText.getText().toString(),
sentIntent, null);
Toast.makeText(SmsExample.this,
"SMS message sent",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(SmsExample.this,
"SMS destination invalid - try again",
Toast.LENGTH_LONG).show();
}
}
});
}
Before doing anything with SMS messages, we must obtain an instance of the SmsManager with the static getDefault() method . The manager will also send the message later. Before we can send the message, we need to create a PendingIntent to provide to the send method. A PendingIntent can specify an Activity, a Broadcast, or a Service that it requires. In our case, we use the getActivity() method, which requests an Activity, and then we specify the context, a request code (not used for this case), the Intent to execute, and additional flags . The flags indicate whether the system should create a new instance of the referenced Activity (or Broadcast or Service), if one doesn’t already exist. Next, we check that the destination address is valid for SMS , and we send the message using the manager’s sendTextMessage() method.