-4

My function isPhoneSupported() shows this error ;

Call requires permission which maybe rejected by the user

My function is as follows:

public boolean isPhoneSupported() {
        TelephonyManager mgr;
        String context = Context.TELEPHONY_SERVICE;
        mgr = (TelephonyManager) getSystemService(context);
        if (mgr.getLine1Number() == null)
        {
            return false;
        }
        else{
            return true;
        }
    }

The error is shown in at : getLine1Number(). Can someone tell me how to overcome this??

Mohd Saquib
  • 580
  • 4
  • 14
  • Check this https://stackoverflow.com/questions/33327984/call-requires-permissions-that-may-be-rejected-by-user – John Joe Jan 30 '18 at 06:33

2 Answers2

1

This is because you have asked the user for the permission and user denied it. you should anyhow ask again and again to a user for this permission if this is mandatory for you else you can add a check before calling getLine1Number() if that is optional.

Aj 27
  • 2,316
  • 21
  • 29
0

You can use RxPermissions to simplify the flow for permission handling. Below are the some code snippet for Rx that you can directly use.

RxPermissions rxPermissions = new RxPermissions(this); // where this is an Activity instance

rxPermissions
    .requestEach(Manifest.permission.READ_PHONE_STATE, Manifest.permission.READ_SMS , Manifest.permission.READ_PHONE_NUMBERS)
    .subscribe(permission -> { // will emit 3 Permission objects
        if (permission.granted) {
           // `permission.name` is granted !
        } else if (permission.shouldShowRequestPermissionRationale) {
           // Denied permission without ask never again
        } else {
           // Denied permission with ask never again
           // Need to go to the settings for this. 
        }
    });

You can use above code snippet to efficiently manage permission model in your project.

Also, for quick integration just add below lines in build.gradle of your module along with above code snippet.

    //RxAndroid
        implementation "io.reactivex.rxjava2:rxandroid:${libraries.rxAndroid}"
   // RxJava
        implementation "io.reactivex.rxjava2:rxjava:${libraries.rxjava2}"
    //RXpermissions
        implementation 'com.tbruyelle.rxpermissions2:rxpermissions:0.8.1@aar'

Full integration of RxPermission you can refer to

  1. Rx Permission
Avtar Guleria
  • 2,126
  • 3
  • 21
  • 33