home top ad

CALL_PHONE permission android example

Hello ,Guys today we are learn about call permission in android.We can make phone calls from our app very easily, through an Intent in Android, you can give the facility to call the user from our app very easily.

Phone call in android

To implement the phone call in your app, two steps have to be followed.

Create New Custom Toast Project

Choose "File", "New", select "New Project" and then click "Next". In the New Android Application window, enter your chosen Application, Project, and Package names and then choose "Next" and last finish.
After creating the project, you will see two files, first activity_main and second MainActivity.java, then you have to paste the code given below activity_main and MainActivity.java.

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   xmlns:tools="http://schemas.android.com/tools"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   android:paddingLeft="@dimen/activity_horizontal_margin"  
   android:paddingTop="@dimen/activity_vertical_margin"  
   android:paddingRight="@dimen/activity_horizontal_margin"  
   android:paddingBottom="@dimen/activity_vertical_margin"  
   tools:context=".MainActivity">  
   <LinearLayout  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:layout_alignParentTop="true"  
     android:layout_centerHorizontal="true"  
     android:layout_marginTop="20dp"  
     android:gravity="center_horizontal"  
     android:orientation="vertical">  
     <TextView  
       android:id="@+id/textView"  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content"  
       android:layout_gravity="center"  
       android:gravity="center"  
       android:text="Please,Write Phone Number"  
       android:textStyle="bold"  
       android:textColor="@color/purple_700"  
       android:textSize="20sp"/>  
     <EditText  
       android:id="@+id/editText"  
       android:layout_width="match_parent"  
       android:layout_height="wrap_content"  
       android:layout_marginTop="60dp"  
       android:ems="10"  
       android:hint="Please Enter A phone number"  
       android:inputType="phone" />  
     <Button  
       android:id="@+id/call"  
       android:layout_width="wrap_content"  
       android:layout_height="wrap_content"  
       android:layout_marginTop="50dp"  
       android:text="Call"  
       android:src="@drawable/ic_call" />  
   </LinearLayout>  
 </RelativeLayout>  
Here we have a button, an edittext and a textView on which we will perform the operation.

MainActivity.java

 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.Menu;  
 import android.view.MenuItem;  
 import android.view.View;  
 import android.widget.Button;  
 import android.widget.EditText;  
 import android.widget.ImageView;  
 import android.widget.Toast;  
 public class MainActivity extends AppCompatActivity {  
   private Button call;  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     call = findViewById(R.id.call);  
     call.setOnClickListener(new View.OnClickListener() {  
       @Override  
       public void onClick(View v) {  
         final EditText etext = findViewById(R.id.editText);  
         String num = etext.getText().toString();  
         if (num.isEmpty()) {  
           Toast.makeText(getApplicationContext(), "Please Enter Mobile Number", Toast.LENGTH_SHORT).show();  
         }  
         else {  
           Intent intent = new Intent(Intent.ACTION_CALL);  
           intent.setData(Uri.parse("tel:" + num));  
           if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {  
             if (checkSelfPermission(Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {  
               Toast.makeText(getApplicationContext(), "Please give me permission", Toast.LENGTH_SHORT).show();  
               requestPermissions(new String[]{Manifest.permission.CALL_PHONE}, 10);  
               return;  
             }  
           }  
           startActivity(intent);  
         }  
       }  
     });  
   }  
 }  
Here we will use all the attributes that we have used to layout here like this we will perform operation on buttons and edittext in which we will input mobile number from user from edittext which we will store in a string type variable num. After that we will check in the if condition that the mobile number has been entered in the edittext by the user that if the user number is not input then we will print a toast that please enter the mobile number.
After this, the else method will run and in that we will enter the mobile number which the user will enter through Intent, our app will call the mobile phone together with the Android system, but keep in mind that intent.setData (Uri.parse ("tel:" + + num)); The number will be connected to the Android system via tel:
After that we will take the run time CALL _PHONE permission like we would the first time we input the number from the user and click on the call button the same way it will take the CALL_PHONE permission, after that it will call the phone.
And finally you have to give android.permission.CALL_PHONE permission in the manifest of this project. Like the code below.

AndroidManifest
 <?xml version="1.0" encoding="utf-8"?>  
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.avdigits.callingproject">  
   <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.CallingProject">  
     <activity android:name=".MainActivity">  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
     <activity android:name=".ProActivity" />  
     <activity android:name=".RadioColor" />  
   </application>  
 </manifest>  

Hope this article is very useful for you.
Below are some screenshots of how the app will work
Thanks .....

Post a Comment

0 Comments