0% found this document useful (0 votes)
35 views5 pages

Send Sms

This document discusses two ways to send SMS from an Android application: using the SMSManager API or an Intent. The SMSManager API directly sends SMS, while an Intent launches the default SMS app. Code samples are provided to demonstrate setting permissions and sending SMS with both approaches.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views5 pages

Send Sms

This document discusses two ways to send SMS from an Android application: using the SMSManager API or an Intent. The SMSManager API directly sends SMS, while an Intent launches the default SMS app. Code samples are provided to demonstrate setting permissions and sending SMS with both approaches.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Android Send SMS

In android, we can send SMS from our android application in two ways either
by using SMSManager API or Intents based on our requirements.

If we use SMSManager API, it will directly send SMS from our application.

In case if we use Intent with proper action (ACTION_VIEW), it will invoke a


built-in SMS app to send SMS from our application.

1. Android Send SMS using SMSManager API


In android, to send SMS using SMSManager API we need to write the code like
as shown below.

SmsManager smgr = SmsManager.getDefault();


smgr.sendTextMessage(MobileNumber,null,Message,null,null);

SMSManager API required SEND_SMS permission in our android manifest to


send SMS. Following is the code snippet to set SEND_SMS permissions in
manifest file.

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

2. Android Send SMS using Intent

• In android, Intent is a messaging object which is used to request an action


from another app component such as activities, services, broadcast
receivers, and content providers.
• To send SMS using the Intent object, we need to write the code like as shown
below.

Intent intObj = new Intent(Intent.ACTION_VIEW);


intObj.putExtra("address", new String[]{txtMobile.getText().toString()});
sInt.putExtra("sms_body",txtMessage.getText().toString());
sInt.setType("vnd.android-dir/mms-sms");

Even for Intent, it required a SEND_SMS permission in our android manifest to


send SMS.

Following is the code snippet to set SEND_SMS permissions in manifest file.

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

Complete example

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/fstTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="150dp"
android:text="Mobile No" />
<EditText
android:id="@+id/mblTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10"/>

<TextView
android:id="@+id/secTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Message"
android:layout_marginLeft="100dp" />
<EditText
android:id="@+id/msgTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:ems="10" />
<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Send SMS" />
</LinearLayout>

MainActivity.java

package com.example.sendsmsexample;
import android.content.Intent;
import android.net.Uri;
import android.provider.Telephony;
import android.support.v7.app.AppCompatActivity;
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 MainActivity extends AppCompatActivity {

private EditText txtMobile;


private EditText txtMessage;
private Button btnSms;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

txtMobile = (EditText)findViewById(R.id.mblTxt);
txtMessage = (EditText)findViewById(R.id.msgTxt);
btnSms = (Button)findViewById(R.id.btnSend);

btnSms.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try{
SmsManager smgr = SmsManager.getDefault();
smgr.sendTextMessage(txtMobile.getText().toString(),null,txtMessage
.getText().toString(),null,null);
Toast.makeText(MainActivity.this, "SMS Sent Successfully",
Toast.LENGTH_SHORT).show();
}
catch (Exception e){
Toast.makeText(MainActivity.this, "SMS Failed to Send, Please try
again", Toast.LENGTH_SHORT).show();
}
}
});
}
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sendsmsexample">
<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>

You might also like