Android中添加授权操作
程序员文章站
2022-05-14 21:16:10
...
Android 6.0之后,需要用户进行授权
不仅需要在配置文件中添加授权
<uses-permission android:name="android.permission.CALL_PHONE" />
还需要在请求的地方添加:
if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE}, 1);
} else {
call();
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 1:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
call();
} else {
Toast.makeText(this, "You denied the permission", Toast.LENGTH_SHORT).show();
}
break;
default:
}
}
下一篇: python简单读取大文件
推荐阅读