SMS in Android



Sending SMS in Android
======================

SMS is one of the most used and important function in android.It can be used to share small amount of data between people.SMs can be used to send message to your friend,Increase Sales and Brand exposure,New Revenue Source for your business,Mass Alert to everyone etc.

Sending SMS using Intent
========================

Using built in functionality ofAndroid,We can send SMS through Intent.


   Intent sendsms = new Intent(Intent.ACTION_VIEW);
   sendsms.setType("vnd.android-dir/mms-sms");
   sendsms.putExtra("address", "+919912345678");
   sendsms.putExtra("sms_body","hey wassuppp ?????");
   startActivity(sendsms);

When call startActivity() with action Intent.ACTION_VIEW, the system will start an activity to display the data to the user.Android knows based upon the specified "type"("vnd.android-dir/mms-sms") that it is meant for the built-in SMS Activity.The "address" and "sms_body" extras specify the phone number and the body of the message that will be filled in when the Activity launches.

For Example:

   public class sendsms extends Activity implements OnClickListener {
   @Override
   public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   Button sendButton = (Button) this.findViewById(R.id.btnsend);
   sendButton.setOnClickListener(this);
   }
   public void onClick(View v) {
   Intent sendsms = new Intent(Intent.ACTION_VIEW);
   sendsms.setType("vnd.android-dir/mms-sms");
   sendsms.putExtra("address", "+919930229995");
   sendsms.putExtra("sms_body","hey wassuppp ?????");
   startActivity(sendsms);
   }
   }

Programmatically sending an SMS
-------------------------------

If you dont want to use built-in applications to send SMS.You can use the Android API to programmatically send
SMS messages from your own applications and We have to use SmsManager for sending SMS programmatically.
In order to send SMS,you need to add the following permission to your AndroidManifest.xml file:


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

SmsManager
----------
SmsManager Manages SMS operations such as sending data, text, and pdu SMS messages.We can get this object by calling the static method SmsManager.getDefault().



   SmsManager sms = SmsManager.getDefault();

SmsManager.getDefault() will get the default instance of SmsManager.


   SmsManager sms = SmsManager.getDefault();
   sms.DataMessage(String destinationAddress, String scAddress, short destinationPort, byte[] data, PendingIntent sentIntent, PendingIntent deliveryIntent

Arguements:

destinationAddress = is the phone number to which you want to send the SMS.
scAddress= is the service center address or null to use the current default SMSC.
destinationPort = the port to deliver the message to.
data =  is the body of the message to send.
sentIntent = if not NULL this PendingIntent is broadcast when the message is successfully sent, or failed.
The result code will be Activity.RESULT_OK for success,RESULT_ERROR_GENERIC_FAILURE for Generic failure,
RESULT_ERROR_RADIO_OFF for radio off,RESULT_ERROR_NULL_PDU for null pdu
For RESULT_ERROR_GENERIC_FAILURE the sentIntent may include the extra "errorCode" containing a radio technology specific value, generally only useful    for troubleshooting.
The per-application based SMS control checks sentIntent. If sentIntent is NULL the caller will be checked against all unknown applications, which cause smaller number of SMS to be sent in checking period.


   SmsManager sms = SmsManager.getDefault();
   sms.sendTextMessage("+9912345678", null, "hey wassup bro ?", null, null);

Example:




   import android.app.Activity;
   import android.os.Bundle;
   import android.telephony.SmsManager;
   import android.view.View;
   import android.widget.Button;

   public class sendsms extends Activity {
    /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        Button send =(Button) findViewById(R.id.btnsend);
        send.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage("+9912345678", null, "Hi  how r u",null, null);

}
});
    }
    }

Comments

  1. it helped me in making sms sending app in android..

    ReplyDelete

Post a Comment

Popular Posts