Send Sms Intent :
Send Sms Intent, Android has several inbuilt functionalities like sending message, calling, music player and many more we can use these inbuilt functionality right from our app also without closing our app. i.e, using intents we can use these functionality and can send SMS from our app.
Android device provides these inbuilt features that can be accessed through our app for better practices, we can make our app work better by getting the device info.
We require sending SMS in different scenarios like authenticating users, providing receipts for the payments made, sending tickets to the user devices saving paper.
Send SMS Intent make its easier for us to integrate the functionality and send sms on a task completion.
There are few third party api’s providing the service of sending the SMS from a company, organization to promote their products and alert user regarding new features.
Android Send Sms Intent Video:
Tutorial on how to send text message will let you learn Send Sms Intent i.e., sending sms from scratch.
main.xml
We need to design a simple UI such that we accept mobile number and message to be sent from the user interface.
Sometimes we can do this behind the UI according to our requirement.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#f1eff0" android:orientation="vertical" android:padding="10dp"> <EditText android:id="@+id/to_num" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:hint="9876543210" android:inputType="number" android:padding="10dp" /> <EditText android:id="@+id/msg_text" android:layout_width="300dp" android:layout_height="100dp" android:layout_alignParentBottom="true" android:background="@android:color/white" android:gravity="start" android:padding="10dp" android:hint="Your message here" android:inputType="textMultiLine" android:minLines="6" /> <Button android:id="@+id/btn_send" android:layout_width="60dp" android:layout_height="60dp" android:layout_alignParentBottom="true" android:layout_toRightOf="@+id/msg_text" android:background="@android:drawable/ic_menu_send" android:backgroundTint="@color/colorPrimary" /> </RelativeLayout>
MainActivity
Initialize edittexts and button
send = (Button) findViewById(R.id.btn_send); to_num = (EditText) findViewById(R.id.to_num); msg_text = (EditText) findViewById(R.id.msg_text); send.setOnClickListener(this);
then using sms manager we can send sms directly from our app
SmsManager smsManager = SmsManager.getDefault();
Creating a method for sending sms
public void sendmessage(){ String number = to_num.getText().toString(); String message = msg_text.getText().toString(); if(number==null&&message==null) { PendingIntent sent = PendingIntent.getBroadcast(getBaseContext(), 0, new Intent("sent"), 0); PendingIntent deliver = PendingIntent.getBroadcast(getBaseContext(), 0, new Intent("delivered"), 0); SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(number, null, message, sent, deliver); Toast.makeText(this, "Sent Message", Toast.LENGTH_SHORT).show(); }else { Toast.makeText(this, "Please enter number and message to continue", Toast.LENGTH_SHORT).show(); } }
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.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener{ EditText msg_text; EditText to_num; Button send; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); send = (Button) findViewById(R.id.btn_send); to_num = (EditText) findViewById(R.id.to_num); msg_text = (EditText) findViewById(R.id.msg_text); send.setOnClickListener(this); } @Override public void onClick(View v) { sendmessage(); } public void sendmessage(){ String number = to_num.getText().toString(); String message = msg_text.getText().toString(); if(number.length()>0&&message.length()>0) { PendingIntent sent = PendingIntent.getBroadcast(getBaseContext(), 0, new Intent("sent"), 0); PendingIntent deliver = PendingIntent.getBroadcast(getBaseContext(), 0, new Intent("delivered"), 0); SmsManager smsManager = SmsManager.getDefault(); smsManager.sendTextMessage(number, null, message, sent, deliver); Toast.makeText(this, "Sent Message", Toast.LENGTH_SHORT).show(); clearFields(); }else { Toast.makeText(this, "Please enter number and message to continue", Toast.LENGTH_SHORT).show(); } } public void clearFields(){ to_num.setText(""); msg_text.setText(""); } }
Manifest
You need to seek proper permission before accessing any device inbuilt feature. Add a permission for sending sms to your manifest file.
For runtime permission tutorial visit : Android run time permission tutorial
<uses-permission android:name="android.permission.SEND_SMS"/>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="sms.android.com.sendingsms"> <uses-permission android:name="android.permission.SEND_SMS"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
Output :
This screen depicts Android Send SMS intent example
If you have any query’s in this tutorial on Android Send SMS Intent let us know in the comment section below.
Share and like this tutorial if you like it for more interesting android tutorials.