How do you rate this blog

Saturday, May 5, 2012

Simple SMS - Send feature


SMS is the most used and one of the most popular features in the mobile phone. In this post I have explained a simple custom sms app which uses the API’s provided by android.

                In this app I will create a text box which will take only numbers and one more text box where in you can type in your message. Once these are done the user can click on the send button which will send the sms to the phone number mentioned.
So lets get started with the creation of the application-
Step 1 – Modification of AndroidManifest.xml file
                Since this application sends SMS we need to provide the application rights to access the SMS service in the mobile device. So we need to add the code below in the “androidmanifest.xml” file
<uses-permission android:name="android.permission.SEND_SMS">    </uses-permission>
The manifest file would like in the snapshot shown below.



Step 2 – Layout Creation
                The layout for this is kept very simple wherein we have one editable text box for phone number and other for message. The layout will be as shown in the snapshot given below.



Step 3 – Code to send SMS
                Open the java file in src folder, as you can see you already have basic code already setup for you. So we need to just implement the send sms functionality in this. Declare the buttons and editable text boxes and bind them to the respective controls in the UI as shown in the code below –
    Button send;
    EditText ph_num;
    EditText message;

/*bind the variables to the controls used in the UI*/
        send=(Button) findViewById(R.id.button1);
        ph_num=(EditText) findViewById(R.id.editText1);
        message=(EditText) findViewById(R.id.editText2);
       
Then implement the send SMS functionality when you click the Send SMS button, the code for which is given below.

send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
/*to convert the phone number and the message to string format*/
                String phone_num=ph_num.getText().toString();
                String sms=message.getText().toString();
/*we will go to send sms functionality only if there is message and phone number*/
                if(phone_num.length()>0 && sms.length() > 0)
                                send_sms(phone_num,sms);
                else
Toast.makeText(getBaseContext(), "Please enter both phone number and message", Toast.LENGTH_LONG).show();
                                                }
                                });

Now we will code the Send SMS functionality –
Inorder to send SMS android provides SmsManager, but this object cant be instantiated directly hence we need to make use of Pending Intent. A Pending Intent is a token that is provided to a foreign application which gives it right to use the applications permission to execute a predefined code in the application. This is done as per the code shown below
public void send_sms(String phone, String msg){
PendingIntent SMS_Sent=PendingIntent.getActivity(this, 0, new Intent(this,AndroidSMSActivity.class), 0);
                 SmsManager sms=SmsManager.getDefault();
                 sms.sendTextMessage(phone, null, msg, SMS_Sent, null);
                Toast.makeText(this, "SMS Sent", Toast.LENGTH_LONG).show();
    }
At the end we add a Toast to notify the user that the SMS has been sent. The code will be as shown below in the snapshot.


Now run the application through the emulator, although it doesnt send sms to any mobile device and the result will be as shown in the snapshot below. 



I will explain how to test out the application using 2 emulators in the post where I will explain how to receive SMS. Hope this was useful to you.

No comments:

Post a Comment