您請求permission具有相同的請求代碼以下,但不工作1

試試這種方式請求Runtime permission

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 ur 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 ur specific task after read phone state

}

而且在你的清單中添加:

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐