In this post you can learn on android code to send sms from an android app. There are many applications in the android market where sms can be sent to the corresponding subscribers from the android web app using this functionality. Let us discuss about it in detail. You can find below an example on how to send sms from an android app. There is a text field for entering the phone number to whom you are gonna send the sms and there is a text field for typing the sms and there is a button named send. On filling the corresponding details in the field and on submitting the button the sms is sent to the corresponding phone number. Using this example you can develop more complex sms functionality for sending sms for a list of users. Let us see the brief android code to send sms below.
1. Create a layout named activity_main.xml in the layout folder of the project.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/EditText_PhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Number"
android:inputType="phone" >
</EditText>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="250dp" >
<EditText
android:id="@+id/MainActivity_Message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:hint="Enter message"
android:inputType="textMultiLine" />
</RelativeLayout>
<Button
android:id="@+id/Send_msg_Btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send_Message" />
</LinearLayout>
2. Create a MainActivity.java in the project
MainActivity.java
package com.example.sms;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button send;
EditText phone_Number, message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button) findViewById(R.id.Send_msg_Btn);
phone_Number = (EditText) findViewById(R.id.EditText_PhoneNumber);
message = (EditText) findViewById(R.id.MainActivity_Message);
send.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String phone_Num = phone_Number.getText().toString();
String send_msg = message.getText().toString();
try {
SmsManager sms = SmsManager.getDefault(); // using android SmsManager sms.sendTextMessage(phone_Num, null, send_msg, null, null); // adding number and text
} catch (Exception e) {
Toast.makeText(this, "Sms not Send", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
3. AndroidManifest File.
Add Uses permission to the below android manifest file to send the sms.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smsdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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>
After completing the above steps you can run your android app and can send sms to the corresponding user.
1. Create a layout named activity_main.xml in the layout folder of the project.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/EditText_PhoneNumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Enter Number"
android:inputType="phone" >
</EditText>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="250dp" >
<EditText
android:id="@+id/MainActivity_Message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ems="10"
android:hint="Enter message"
android:inputType="textMultiLine" />
</RelativeLayout>
<Button
android:id="@+id/Send_msg_Btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send_Message" />
</LinearLayout>
2. Create a MainActivity.java in the project
MainActivity.java
package com.example.sms;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button send;
EditText phone_Number, message;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send = (Button) findViewById(R.id.Send_msg_Btn);
phone_Number = (EditText) findViewById(R.id.EditText_PhoneNumber);
message = (EditText) findViewById(R.id.MainActivity_Message);
send.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String phone_Num = phone_Number.getText().toString();
String send_msg = message.getText().toString();
try {
SmsManager sms = SmsManager.getDefault(); // using android SmsManager sms.sendTextMessage(phone_Num, null, send_msg, null, null); // adding number and text
} catch (Exception e) {
Toast.makeText(this, "Sms not Send", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
}
3. AndroidManifest File.
Add Uses permission to the below android manifest file to send the sms.
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smsdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
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>
After completing the above steps you can run your android app and can send sms to the corresponding user.