如何在android 6.0中获取read_phone_state权限

时间:2017-02-05 16:51:15

标签: android android-permissions

我得到以下错误

java.lang.SecurityException: getDeviceId: Neither user 10250 nor current process has android.permission.READ_PHONE_STATE.

AndroidRuntime: FATAL EXCEPTION: main
Process: com.infyco.kp.new_tab, PID: 23149
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.infyco.kp.new_tab/com.infyco.kp.new_tab.Splashscreen}: java.lang.SecurityException: getDeviceId: Neither user 10257 nor current process has android.permission.READ_PHONE_STATE.

这是我的代码:

  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);

适用于SEND_SMS权限,但不适用于READ_PHONE_STATE权限

  ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 1);

2 个答案:

答案 0 :(得分:1)

您使用相同的请求代码请求权限(此处:1)。

试试这个:

public  boolean isPermissionGranted() {
    if (Build.VERSION.SDK_INT >= 23) {
        if (checkSelfPermission(android.Manifest.permission.READ_PHONE_STATE)
                == PackageManager.PERMISSION_GRANTED) {
            Log.v("TAG","Permission is granted");
            return true;
        } else {

            Log.v("TAG","Permission is revoked");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 2);
            return false;
        }
    }
    else { //permission is automatically granted on sdk<23 upon installation
        Log.v("TAG","Permission is granted");
        return true;
    }
}


 @Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {

        case 2: {

            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getApplicationContext(), "Permission granted", Toast.LENGTH_SHORT).show();
                //do your specific task after read phone state granted
            } else {
                Toast.makeText(getApplicationContext(), "Permission denied", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
 }

像这样使用:

if(isPermissionGranted()){
    //do your specific task after read phone state
}

此外,在您的清单中,添加:

<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

答案 1 :(得分:-1)

   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
    {
        if (checkSelfPermission(Manifest.permission.READ_PHONE_STATE)
                != PackageManager.PERMISSION_GRANTED) {
            requestPermissions(new String[]{Manifest.permission.READ_PHONE_STATE},
                    PERMISSIONS_REQUEST_READ_PHONE_STATE);
        }
        else
            {
            mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            IMEI_Number_Holder= mTelephonyManager.getDeviceId();
            }
    }
    else
    {
        mTelephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        IMEI_Number_Holder = mTelephonyManager.getDeviceId();
    }