logo
Login

Sponsored Links

Sponsored

Sending SMS using Android

Sending SMS Android AppSMS - Short Message Service is very effective and quick way of communication. Your app may have a use case wherein it needs to send SMS to someone. So lets see how it can done, it is relative simple as you would see below:

Step 1: Creating the project
This is the simplest step of them all. Just go to your project wizard and create a new project with the following details:

Project name: SendSms2.3
Application name: SendSms2.3
Package name: send.sms
Minimum Required SDK: API 8
Target SDK: API 10
Main Activity: SendSmsActivity
Layout name: main
Step 2: Editing the Manifest
In order  to allow any app to send or receive SMS you need to allow the app some special permissions. They are:
[code]
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
[/code]
So I added then to the Manifest file and it looked like this:
[code]
    <?xml version="1.0" encoding="utf-8" ?>
<manifest xmlns:android=http://schemas.android.com/apk/res/android  package="send.sms" android:versionCode="1" android:versionName="1.0">
       <uses-sdk android:minSdkVersion="10" />
       <uses-permission android:name="android.permission.SEND_SMS" />
       <uses-permission android:name="android.permission.RECEIVE_SMS" />
     <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">
<activity android:name=".SendsmsActivity" android:label= "@string/app_name">
         <intent-filter>
                   <action android:name="android.intent.action.MAIN" />
                   <category android:name="android.intent.category.LAUNCHER" />
               </intent-filter>
          </activity>
   </application>
   </manifest>
[/code]

Step 3: Creating a layout
UI is most important but I created a very simple user interface to test everything out
SMS Android App Screenshot
It just has 2 text boxes and a button:
[code]
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android  android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">
<LinearLayout android:layout_width="fill_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_gravity="center_vertical|center_horizontal" android:layout_marginTop="50px">
<TextView android:text="Number :" android:id="@+id/textView1" android:layout_width="100px" android:layout_height="30px" android:gravity="center" android:layout_gravity="center_vertical" android:textSize="25px" />
<EditText android:layout_marginRight="0px" android:background="#FFFFFF" android:textColor="#000000" android:layout_marginLeft="30px" android:id="@+id/edt_number" android:layout_width="165px" android:layout_height="40px" android:layout_gravity="center_vertical" />
      </LinearLayout>
<LinearLayout android:layout_gravity="top" android:id="@+id/LinearLayout2" android:layout_height="wrap_content" android:layout_width="fill_parent" android:layout_marginTop="20dp">
       <TextView android:id="@+id/TextView1" android:layout_width="100px" android:layout_height="30px" android:layout_gravity="fill_vertical" android:gravity="center" android:text="Message :" android:textSize="25px" />
       <EditText android:layout_marginRight="0px" android:background="#FFFFFF" android:textColor="#000000" android:layout_gravity="top" android:layout_marginLeft="30px" android:layout_width="165px" android:layout_height="165px" android:id="@+id/edt_msgtext" />
   </LinearLayout>
<LinearLayout android:id="@+id/LinearLayout3" android:layout_width="300px" android:layout_height="50px" android:layout_gravity="center" android:layout_marginTop="20dp">
       <Button android:layout_height="50px" android:layout_width="fill_parent" android:layout_gravity="top" android:textSize="25px" android:text="Send" android:id="@+id/btn_send" />
   </LinearLayout>
   </LinearLayout>
[/code]
Now that it was done I proceeded to more important thing.

Step 4: Writing the activity
I created the activity like this:
[code]
package send.sms;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class SendsmsActivity extends Activity {
    /** Called when the activity is first created. */
    Button buttonSend;
        EditText editNumber,editMessageText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        buttonSend = (Button)findViewById(R.id.btn_send);
        editNumber = (EditText)findViewById(R.id.edt_number);
        editMessageText = (EditText)findViewById(R.id.edt_msgtext);
        buttonSend.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            String phoneno = editNumber.getText().toString();
String messagetxt = editMessageText.getText().toString();       
            if (phoneno.length()>0 && messagetxt.length()>0)               
                                sendSMS(phoneno, messagetxt);
                        else
                                Toast.makeText(SendsmsActivity.this,
                                    "Please enter both phone number and message.",                                               Toast.LENGTH_SHORT).show();
                });
    }
}
[/code]
We should use pending intent to broadcast the message, so I used that and also displayed a simple Toast if no message or phone number is indicated.
And then to send message we use SmsManager in a user defined function sendSms as follows:
[code]
private void sendSMS(String phoneNumber, String message)
    {       
        String SENT = "SMS_SENT";
         PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
        SmsManager sms = SmsManager.getDefault(); 
        sms.sendTextMessage(phoneNumber, "kalpen", message, sentPI,null);
    }
[/code]

And that is all you need to do to send an SMS using Android API. You can use this to create all sorts of applications For example, you can create an application for assigning Tasks to your employees with a feature to send a text message to them for reminder or alerts.

The complete project is attached to post.

AttachmentSize
sendsms2.3.zip152.85 KB
Like Developerfeed? Follows us via: RSS Feed Follow me on Twitter! Follow me on Facebook! Follow me on Youtube!

Socialize & Share Now :

Subscribe to DeveloperFeedDid you like this article? Did it Help you Solve your Problem? You can get the all the latest articles published at DeveloperFeed in your email inbox by entering your email address below. Your address will only be used for mailing you the articles, and each one will include a link so you can unsubscribe at any time.

Enter your email address:

Subscribe & Follow

RSS Feed Follow me on Twitter! Follow me on Facebook! Follow me on Youtube!
Feed via Email:

Sponsored Links

Facebook

Sponsored Apps

Job Change Alert for Linkedin


Contact2CRM for Salesforce


Advertise on DeveloperFeed

About

Manpreet is an Architect & Software developer currently focusing on developing mobile apps on the iOS (iPhone/iPad) Platform. He’s the founder of a small iPhone development studio
called Livrona.

Subscribe to Developer Feed iPhone Blog

Tags

Who's online

There are currently 0 users and 2 guests online.