Send SMS using SmsManager

Send SMS using SmsManager API in Android Studio

Project Details: Send SMS using SmsManager

Sending SMS messages can be very useful to communicate with other people. Developers can also leverage SMS messaging in their apps by using the smsmanager API. The API allows apps to send SMS text messages, schedule SMS messages, and monitor the number of outgoing and incoming SMS messages.

Android apps can send and receive SMS text messages from the cloud or other Android devices. Developers can also use the smsmanager API to interact with their apps from a mobile phone or a computer’s operating system.

Send SMS using SmsManager API in Android Studio, android studio, free download, free app, free source code, mobile app, free android studio, make an app, learn studio, android, ios, tutorial,

This illustration is utilizing the smsManager API to convey the SMS message.

Send SMS using SmsManager: Step 1 - Android Layout

Path: res >> layout > main.xml

				
					<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <TextView
        android:id="@+id/textViewPhoneNo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Phone Number : "
        android:textAppearance="?android:attr/textAppearanceLarge" />
 
    <EditText
        android:id="@+id/editTextPhoneNo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:phoneNumber="true" >
    </EditText>
 
    <TextView
        android:id="@+id/textViewSMS"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter SMS Message : "
        android:textAppearance="?android:attr/textAppearanceLarge" />
 
    <EditText
        android:id="@+id/editTextSMS"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:inputType="textMultiLine"
        android:lines="5"
        android:gravity="top" />
 
    <Button
        android:id="@+id/buttonSend"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Send" />
 
</LinearLayout>
				
			

Send SMS using SmsManager: Step 2 - SendSMSActivity.java

SendSMSActivity.java Activity to send SMS via smsManager.

				
					public class SendSMSActivity extends Activity {
 
Button buttonSend;
EditText textPhoneNo;
EditText textSMS;
 
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
 
buttonSend = (Button) findViewById(R.id.buttonSend);
textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
textSMS = (EditText) findViewById(R.id.editTextSMS);
 
buttonSend.setOnClickListener(new OnClickListener() {
 
@Override
public void onClick(View v) {
 
String phoneNo = textPhoneNo.getText().toString();
String sms = textSMS.getText().toString();
 
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
Toast.makeText(getApplicationContext(), "SMS Sent!",
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"SMS faild, please try again later!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
 
}
});
 
}
}

				
			

Learn More SMSManager; 

http://developer.android.com/reference/android/telephony/SmsManager.html

Step 3 Add Permission into Manifest file.

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

I hope the above article is of help to you for your learning and future project.

Don’t forget to share this post!

Leave a Comment

Your email address will not be published. Required fields are marked *

Related Article