Pra*_*nge 5 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.
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_PHONE_STATE}, 1);
Run Code Online (Sandbox Code Playgroud)
它适用于SEND_SMS许可,但不适用于READ_PHONE_STATE许可
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SEND_SMS}, 1);
Run Code Online (Sandbox Code Playgroud)
您正在使用相同的请求代码(此处: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
}
}
Run Code Online (Sandbox Code Playgroud)
像这样使用它:
if(isPermissionGranted()){
//do your specific task after read phone state
}
Run Code Online (Sandbox Code Playgroud)
另外,在您的清单中添加:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10069 次 |
| 最近记录: |