android-studio
  1. android-studio-simple-caller-talker

Android Studio Simple Caller Talker

Syntax

public class MainActivity extends Activity {
   private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 0;
   private TelephonyManager mTelephonyManager;
   private TextView mTextView;
   private String mPhoneNumber;

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

       mTextView = (TextView) findViewById(R.id.text_view);

       // Here, thisActivity is the current activity
       if (ContextCompat.checkSelfPermission(this,
               Manifest.permission.READ_PHONE_STATE)
               != PackageManager.PERMISSION_GRANTED) {

           // Permission is not granted
           // Should we show an explanation?
           if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                   Manifest.permission.READ_PHONE_STATE)) {
               // Show an explanation to the user *asynchronously* -- don't block
               // this thread waiting for the user's response! After the user
               // sees the explanation, try again to request the permission.
           } else {
               // No explanation needed; request the permission
               ActivityCompat.requestPermissions(this,
                       new String[]{Manifest.permission.READ_PHONE_STATE},
                       MY_PERMISSIONS_REQUEST_READ_PHONE_STATE);

               // MY_PERMISSIONS_REQUEST_READ_PHONE_STATE is an
               // app-defined int constant. The callback method gets the
               // result of the request.
           }
       } else {
           // Permission has already been granted
           mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
           mPhoneNumber = mTelephonyManager.getLine1Number();
           mTextView.setText("Phone number: " + mPhoneNumber);
           Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mPhoneNumber));
           startActivity(intent);
       }
   }

   @Override
   public void onRequestPermissionsResult(int requestCode,
                                          String[] permissions, int[] grantResults) {
       switch (requestCode) {
           case MY_PERMISSIONS_REQUEST_READ_PHONE_STATE: {
               // If request is cancelled, the result arrays are empty.
               if (grantResults.length > 0
                       && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                   // Permission granted
                   mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
                   mPhoneNumber = mTelephonyManager.getLine1Number();
                   mTextView.setText("Phone number: " + mPhoneNumber);
                   Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + mPhoneNumber));
                   startActivity(intent);
               } else {

                   // Permission denied
                   Toast.makeText(MainActivity.this, "Permission Denied", Toast.LENGTH_SHORT).show();
               }
               return;
           }
       }
   }
}

Example

Below is an example where we request for permission to read phone state and upon granting of the permission, user’s phone number is displayed in a TextView and also a phone call is made to the displayed phone number.

Output

Output

Explanation

This Android Studio project implements an application that displays the user’s phone number when permission is granted and makes a phone call to the user’s phone number using an intent.ACTION_CALL when the user taps on the display.

Use

This project provides a simple guide to request for permission and read the user’s phone number in an Android application.

Important Points

  • Permissions must be requested before performing any action that requires them.
  • Permissions are requested at runtime as opposed to pre-defined permissions in the app manifest.
  • The onRequestPermissionsResult method is called after requesting permission where you can handle the result of the request.

Summary

This project has demonstrated how to request for permission and read the user’s phone number and make a phone call using the Android Studio environment. The code demonstrates the concept of how permissions can be requested at runtime.

Published on: