In this tutorial, I will be demonstrating how to build an App with a Functionality of Calling And Sending Message without Using Phones dialerPad Or Message Inbox.
Below is screenshot of final output- Create a new project by going to File ⇒ New Android Project. Fill all the details and name your activity as MainActivity
- Once the project is created open your activity_main.xml file and Add Below Code
<?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:orientation="vertical"
tools:context="com.targetandroid.info.smscallapp.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter No."
android:id="@+id/etno_sms"
android:inputType="phone"
android:gravity="center"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="enter message"
android:lines="4"
android:padding="16dp"
android:id="@+id/etmessage_sms"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send Message"
android:id="@+id/bt_message"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Call"
android:id="@+id/bt_call"
/>
</LinearLayout>
3. Open AndroidManifest.xml
and Add this Two Permission Above Application Tag
<uses-permission android:name="android.permission.CALL_PHONE" /> <uses-permission android:name="android.permission.SEND_SMS" />
4. Now Finally Open MainActivity.java and Copy this Part of Code
/**
* Created by Faizan
*/
package com.targetandroid.info.smscallapp;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
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 {
EditText etno_sms, etmessage_sms;
Button bt_message, bt_call;
private static final int MY_PERMISSION_SMS=1;
private static final int MY_PERMISSION_CALL=2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initializing Views
etno_sms = (EditText) findViewById(R.id.etno_sms);
etmessage_sms = (EditText) findViewById(R.id.etmessage_sms);
bt_message = (Button) findViewById(R.id.bt_message);
bt_call = (Button) findViewById(R.id.bt_call);
bt_message.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Checking Which Android Version is Running In Phone
if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
//This Block Executes if Android Version in greater or equals to Marshmalllow
if ((ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.SEND_SMS)!= PackageManager.PERMISSION_GRANTED)){
/*This block executes if permisson in not given to app previously after
giving Permission onRequestPermissionsResult method will execute*/
requestPermissions(new String[]{Manifest.permission.SEND_SMS},MY_PERMISSION_SMS);
}
else {
//This block executes if permisson is already given to app
sendSMSMessage();
}
}
else {
//This block executes if Version of Phone is Lower than Marshmallow
sendSMSMessage();
}
}
});
bt_call.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Checking Which Android Version is Running In Phone
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
//This Block Executes if Android Version in greater or equals to Marshmalllow
if ((ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED))
{
/*This block executes if permisson in not given to app previously after
giving Permission onRequestPermissionsResult method will execute*/
requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, MY_PERMISSION_CALL);
}
else
{
//This block executes if permisson is already given to app
call();
}
}
else
{
//This block executes if Version of Phone is Lower than Marshmallow
call();
}
}
});
}
/**
*for calling
*/
private void call() {
String phone = etno_sms.getText().toString();
Intent callintent = new Intent(Intent.ACTION_CALL);
callintent.setData(Uri.parse("tel:+91" + phone));
try {
startActivity(callintent);
} catch (Exception e) {
// Toast.makeText(SmsActivity.this,e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
/**
* For SMS
*/
private void sendSMSMessage() {
//get the content Entered in Edittext
String message = etmessage_sms.getText().toString();
String phone = etno_sms.getText().toString();
/*You can write a condition and Can Check
@message and
@phone
if it is empty or null so that you can show some Toast to user*/
try {
SmsManager smsmanager = SmsManager.getDefault();
smsmanager.sendTextMessage(phone, null, message, null, null);
Toast.makeText(MainActivity.this, "Sending Sms To " + phone, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case MY_PERMISSION_SMS:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
sendSMSMessage();
}
else {
Toast.makeText(MainActivity.this, "Permission Cancelled,application cannot Send SMS.", Toast.LENGTH_SHORT).show();
}
break;
case MY_PERMISSION_CALL:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
call();
}
else {
Toast.makeText(MainActivity.this, "Permission Cancelled,application cannot Call.", Toast.LENGTH_SHORT).show();
}
break;
}
}
}
For More Tutorial Share And Subscribe to my Blog So you will get all the updates on your email, We Never Spam.
Thank You




EmoticonEmoticon