SMS – Short Message Service is very effective and quick way of communication. Your app may need to support certain use-cases wherein you have to send an SMS to someone. Adding SMS capabilities in your Android app is relatively straightforward, simply follow the following steps:
Step 1: Create 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: Edit the Manifest
In order to allow any app to send or receive SMS, your needs to request access to a couple of special permissions as follows:
<uses-permission android:name=”android.permission.SEND_SMS” />
<uses-permission android:name=”android.permission.RECEIVE_SMS” />
With the above permissions, the contents of the manifest file look like below:
<?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>
Step 3: Create a layout
UI is most important but so create a very simple user interface to test everything out.
<?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>
Step 4: Write the activity
Create an activity as shown below:
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();
});
}
}
Use a pending intent to broadcast the message. Present an error message using a Toast (alert) is if a message or a phone number is not provided. Finally, send the SMS message we use SmsManager in a user-defined function sendSms as follows:
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);
}
And that is all you need to do to send an SMS using Android API. You can use this to create all sorts of interesting applications e.g. you can create an application for assigning tasks to your employees with a feature that allows sending a text message to them as reminders.
The complete project is attached to the post