Monday, July 16, 2012

Send SMS and Receive Delivery Report in Android



Click Here To Download Source Code

Package Name   :  selva.sms

Project Name     :  SMS1

Version              :  1.5 ( Supports 1.5 and above versions)

main.xml


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Button
        android:id="@+id/btnSendSMS"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send SMS" />
</LinearLayout>




SMS1Activity.java


package selva.sms;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.gsm.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class SMS1Activity extends Activity
{
Button btnSendSMS;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

setContentView(R.layout.main);
btnSendSMS = (Button) findViewById(R.id.btnSendSMS);
btnSendSMS.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
sendSMS("5556", "Hi You got a message!");
}
});
}
//---sends an SMS message to another device---

private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
String DELIVERED = "SMS_DELIVERED";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,
new Intent(SENT), 0);
PendingIntent deliveredPI = PendingIntent.getBroadcast(this, 0,new Intent(DELIVERED), 0);
//---when the SMS has been sent---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS sent",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(getBaseContext(), "Generic failure",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(getBaseContext(), "Null PDU",
Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(getBaseContext(), "Radio off",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(SENT));
//---when the SMS has been delivered---
registerReceiver(new BroadcastReceiver(){
@Override
public void onReceive(Context arg0, Intent arg1) {
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "SMS delivered",
Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(getBaseContext(), "SMS not delivered",
Toast.LENGTH_SHORT).show();
break;
}
}
}, new IntentFilter(DELIVERED));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, deliveredPI);
}
}



 AndroidManifest.xml


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="selva.sms"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".SMS1Activity" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>



OUTPUT: 






Click Here To Download Source Code


15 comments:

  1. what is the R object you are using in this code?

    ReplyDelete
  2. btnSendSMS = (Button) findViewById(R.id.btnSendSMS);

    Here what does the R button stand for?

    ReplyDelete
    Replies
    1. R.id.btnSendSMS -> This is using to find a button which we declared in XML

      Delete
  3. Thanks for your quick response, I was wondering if there is a way to enable SMS delivery reports pragmatically?

    ReplyDelete
  4. Can you help me if you have any idea?

    ReplyDelete
  5. can u help me in providing code for authentication pin of 4 digit

    ReplyDelete
  6. Thanks for sharing the useful blog,Great info. Truly appreciated.
    You can use Android SMS App To send 1000’s of Bulk sms at once

    ReplyDelete
  7. Good article, your effort is totally appreciable full of informative stuff thanks for sharing. Keep sharing information like this. android sms sender

    ReplyDelete
  8. It was very helpful to me. keep posting amazing stuffs. Thanks Android Bulk SMS sender App

    ReplyDelete
  9. Thank you so much for providing such a useful information !!
    Android sms sender

    ReplyDelete
  10. Best information about software.Thanks for sharing such great information. hope you keep sharing such kind of information Android Bulk sms sender

    ReplyDelete
  11. These details are great. The SMS marketing is just so much great help to the new businesses because they can get instant results. I am also into business world and would like to tell you that the secret to our continuously increasing sales graph is- heymarket real estate text marketing app.

    ReplyDelete
  12. Nice blog and information. Thanks for sharing

    such great information.Android Bulk SMS Sender

    ReplyDelete
  13. Such a great information, thanks for sharing with us.Android Bulk SMS Sender

    ReplyDelete