Android Send SMS Programmatically with Permissions

  • by
Android Send SMS Programmatically with Permissions 01

Android Send SMS Programmatically with Permissions Manager and Android SMS Manager.

In this post let’s explore Android SMS Manager class and make an Android App to send out SMS. We will also Grant the SMS Permissions within Android Application so that it can be directly used according to updated Android Permissions rules.

If you want to learn How to make Dialer Application in Android for making phone calls programmatically.

In your Project, edit your layout xml file as below :

The layout file will contain two Android EditText text boxes, one to enter Mobile number of the SMS Recipient and other to enter the SMS text Message. And also two buttons, one to Send our SMS Message and other two clear the EditText text boxes.

activity_xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="5dp"
android:text="Android SMS Application"
android:textColor="@color/colorAccent"
android:textSize="25sp"
android:textStyle="bold" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="Mobile No : "
android:textStyle="bold" />

<EditText
android:id="@+id/edtMobileNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:hint="Mobile Number of Recipient" />

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:text="Message Text : "
android:textStyle="bold" />

<EditText
android:id="@+id/edtMessage"
android:minLines="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:hint="Message Text"
android:inputType="textMultiLine" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@color/colorPrimaryDark"
android:text="Send"
android:textColor="#fff" />

<Button
android:id="@+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@color/colorAccent"
android:text="Clear"
android:textColor="#fff" />

</LinearLayout>

</LinearLayout>

Design :

Android Send SMS Programmatically with Permissions 01

Now in your Android Manifest file give SMS Permission reference : android.permission.SEND_SMS above the application tag.

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.parallelcodes.smsapp">

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

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

Now let’s create the Code to send our SMS Messages and Allow Permissions to our Application. On pressing the Send Button Application will send the SMS Message to the specified SMS recipient.

Class for sending out SMS :

public void SendMessage(String strMobileNo, String strMessage) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(strMobileNo, null, strMessage, null, null);
Toast.makeText(getApplicationContext(), "Your Message Sent",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), ex.getMessage().toString(),
Toast.LENGTH_LONG).show();
}
}

For Accessing Android Permissions, you will require “android.permission.SEND_SMS” permissions. This will be declared in the onCreate method of your File.

if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.SEND_SMS)) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Need SMS Permission");
builder.setMessage("This app needs SMS permission to send Messages.");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.SEND_SMS}, SMS_PERMISSION_CONSTANT);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
} else if (permissionStatus.getBoolean(Manifest.permission.SEND_SMS, false)) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Need SMS Permission");
builder.setMessage("This app needs SMS permission to send Messages.");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
sentToSettings = true;
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
Toast.makeText(getBaseContext(),
"Go to Permissions to Grant SMS permissions", Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}
, SMS_PERMISSION_CONSTANT);
}

SharedPreferences.Editor editor = permissionStatus.edit();
editor.putBoolean(Manifest.permission.SEND_SMS, true);
editor.commit();

}

Complete class :

MainActivity.java:

package com.parallelcodes.smsapp;

import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
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 {

EditText edtMobileNo, edtMessage;
Button btnSend, btnClear;
SharedPreferences permissionStatus;
private boolean sentToSettings = false;
private static final int SMS_PERMISSION_CONSTANT = 100;
private static final int REQUEST_PERMISSION_SETTING = 101;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

edtMessage = (EditText) findViewById(R.id.edtMessage);
edtMobileNo = (EditText) findViewById(R.id.edtMobileNo);

btnClear = (Button) findViewById(R.id.btnClear);
btnSend = (Button) findViewById(R.id.btnSend);

permissionStatus = getSharedPreferences("permissionStatus", MODE_PRIVATE);

btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SendMessage(edtMobileNo.getText().toString(), edtMessage.getText().toString());
}
});

if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.SEND_SMS)) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Need SMS Permission");
builder.setMessage("This app needs SMS permission to send Messages.");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.SEND_SMS}, SMS_PERMISSION_CONSTANT);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
} else if (permissionStatus.getBoolean(Manifest.permission.SEND_SMS, false)) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Need SMS Permission");
builder.setMessage("This app needs SMS permission to send Messages.");
builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
sentToSettings = true;
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
Toast.makeText(getBaseContext(),
"Go to Permissions to Grant SMS permissions", Toast.LENGTH_LONG).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.SEND_SMS}
, SMS_PERMISSION_CONSTANT);
}

SharedPreferences.Editor editor = permissionStatus.edit();
editor.putBoolean(Manifest.permission.SEND_SMS, true);
editor.commit();

}
}

public void SendMessage(String strMobileNo, String strMessage) {
try {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(strMobileNo, null, strMessage, null, null);
Toast.makeText(getApplicationContext(), "Your Message Sent",
Toast.LENGTH_LONG).show();
} catch (Exception ex) {
Toast.makeText(getApplicationContext(), ex.getMessage().toString(),
Toast.LENGTH_LONG).show();
}
}
}

 

This will send SMS to the entered number in the text box.

Android Send SMS Programmatically with Permissions 02

Please also see :

How to make Dialer Application in Android for making phone calls programmatically.

 


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.