SajaWal NaWaz SajaWal NaWaz - 5 months ago 23
Android Question

How can i send SMS from my android app?

I am trying to send an SMS from my android app. Every time I click on my button, it goes to

catch
part rather then
try
.

Here is my method:

Button sms = (Button)findViewById(R.id.button2);
sms.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{

Log.i("Send SMS", "");
Intent sms = new Intent(Intent.ACTION_VIEW);
sms.setData(Uri.parse("smsto:"));
sms.setType("vnd.android-dir/mms-sms");
sms.putExtra("address", new String(con));
sms.putExtra("sms_body", "Test SMS to Angilla");

try {
startActivity(sms);
finish();
Log.i("Finished sending SMS...", "");
}
catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(DetailActivity.this,
"SMS faild, please try again later.", Toast.LENGTH_SHORT).show();
}

}
});

Answer

You can send sms two ways: 1. SMS Manager 2. Built in app

Both ways need permissions in manifest

<uses-permission android:name="android.permission.SEND_SMS" />

Example with sms manager:

SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("phoneNo", null, "sms message", null, null);

Example with built-in app:

Intent sendIntent = new Intent(Intent.ACTION_VIEW);
sendIntent.putExtra("sms_body", "default content"); 
sendIntent.setType("vnd.android-dir/mms-sms");
startActivity(sendIntent);