Android动态申请权限
1.可以在AndroidManifest里面注册的权限:
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
1 |
|
2.动态申请权限
判断是否有权限
1
2
3
4
5
6if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
return true;
}申请权限
1
2
3
4
5
6
7
8
9
10
11
12@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 1) {
if (grantResults.length != 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, "权限开启成功", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "权限开启失败", Toast.LENGTH_SHORT).show();
}
}
}
Android动态申请权限
http://example.com/2023/03/28/Android权限申请/