Runtime permission for call phone in Android
By: Golap Gunjan Barman
Tags: Android App Development, Android, Android Calling App, AndroidDev, Runtime Permissions

In this tutorial, we’re going to see how to give Runtime Permission in Android for using the Android OS function. Before going to implement it, first, let’s see what Runtime Permission is and the importance of Runtime permission.

What is Android Runtime Permissions?

With the introduction of Android 6.0 (SDK 23), users are advised for some specific permission at runtime when they become necessary to use. But what about the older versions of the Android SDK.

So our first question is — Will the older apps run on Android Marshmallow? The answer is yes if the targetSdkVersion is 22 or less.

Hence android runtime permission supports backward compatibility. Now, that doesn’t mean that we can work with an old model of permissions by setting the SDK version to 22. A user using Marshmallow can revoke the dangerous permissions from the Settings > Apps > Permissions. In that case, we try to call some function that requires permissions that the user has not granted yet, the function will suddenly throw an Exception (java.lang.SecurityException) that will lead to the application crashing. Hence we need to implement this new android permissions model in our application.

Runtime Permission for Call in Android

Open android studio and create a new project.

Now open the activity_main.xml file inside the layout directory and paste the below lines of 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=”.MainActivity”> <EditText android:layout_width=”match_parent” android:layout_height=”wrap_content” android:id=”@+id/number” android:hint=”Enter Phone Nuber” android:textColor=”@color/white” android:inputType=”phone” android:layout_margin=”16dp”/> <ImageButton android:layout_width=”50dp” android:layout_height=”50dp” android:id=”@+id/call” android:src=”@drawable/call” android:background=”@drawable/call_button” android:layout_gravity=”center_horizontal”/> </LinearLayout>

The output of the above code snippet:

For button-background create a drawable file call_button.xml inside res > drawable folder.

<?xml version=”1.0" encoding=”utf-8"?> <shape xmlns:android=”http://schemas.android.com/apk/res/android"> <solid android:color=”@color/purple_700"/> <corners android:radius=”120dp”/> </shape>

The output of the above code snippet:

Now go to the MainActivity.java class and call the EditText and Button inside onCreate() method.

final EditText phone = findViewById(R.id.number); final ImageButton call = findViewById(R.id.call);

Now create a function for runtime permission inside the button onClick.

call.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { callAtRuntime(); } });

Now create a logic to call at runtime permission inside callAtRuntime() function.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){ requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 1); } else { //we will store the number in a String variable //and get the value from EditText String phoneNumber = phone.getText().toString(); Intent intent = new Intent(Intent.ACTION_CALL); intent.setData(Uri.parse(“tel:”+ phoneNumber)); startActivity(intent); }

As we know runtime permission launch in Marshmallow, so users should have marshmallow or greater version in their mobile. Here checkSelfPermission check the permission from your manifest file and PackageManager does not allow that permission until now. So users can request permission from their mobile os.

check the permission from your manifest file and does not allow that permission until now. So users can request permission from their mobile os. Now receive the output in the onRequestPermissionResult method.

@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == 1){ if (grantResults[0] == PackageManager.PERMISSION_GRANTED){ callAtRuntime(); } else { Toast.makeText(this, “Oh No!!! Permission Denied. Try Again!”, Toast.LENGTH_SHORT).show(); } } }

Before running the app, go to the AndroidManifest.xml file and give CALL PHONE permission.

<?xml version=”1.0" encoding=”utf-8"?> <manifest xmlns:android=”http://schemas.android.com/apk/res/android" package=”com.codewithgolap.calldialer”> <uses-permission android:name=”android.permission.CALL_PHONE”/> <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/Theme.CallDialer”> <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>

Now run the app.

If you first deny the permission the Toast message “Oh No!!! Permission Denied. Try Again!” shown.

Again click on the call button and allow the permission, finally you will able to call that number using runtime permission.

MainActivity.java class

package com.codewithgolap.calldialer; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.ImageButton; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private EditText phone; private ImageButton call; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); phone = findViewById(R.id.number); call = findViewById(R.id.call); call.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { callAtRuntime(); } }); } private void callAtRuntime() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){ requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 1); } else { //we will store the number in a String variable //and get the value from EditText String phoneNumber = phone.getText().toString(); Intent intent = new Intent(Intent.ACTION_CALL); intent.setData(Uri.parse(“tel:”+ phoneNumber)); startActivity(intent); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { if (requestCode == 1){ if (grantResults[0] == PackageManager.PERMISSION_GRANTED){ callAtRuntime(); } else { Toast.makeText(this, “Oh No!!! Permission Denied. Try Again!”, Toast.LENGTH_SHORT).show(); } } } }

Output