赞
踩
权限分为几个保护级别。保护级别影响是否需要运行时权限请求:
需要我们了解的是正常权限和危险权限。
正常的权限覆盖了应用程序需要访问沙箱之外的数据或资源的区域,但这些区域对用户隐私或其他应用程序的操作几乎没有风险。例如,设置时区的权限是正常的权限。
如果应用程序在它的清单中声明它需要一个正常的权限,系统会在安装时自动授予该权限。系统不提示用户授予正常权限,用户也不能撤销这些权限。
那除了签名权限和普通权限,剩下的就都是危险权限,很多就不详细列举了。我们主要也是要对危险权限动态授权,官方是这么说的
权限组
权限组 | 权限 |
---|---|
CALENDAR | READ_CALENDAR WRITE_CALENDAR |
CAMERA | CAMERA |
CONTACTS | READ_CONTACTS WRITE_CONTACTS GET_ACCOUNTS |
LOCATION | ACCESS_FINE_LOCATION ACCESS_COARSE_LOCATION |
MICROPHONE | RECORD_AUDIO |
PHONE | READ_PHONE_STATE CALL_PHONE READ_CALL_LOG WRITE_CALL_LOG ADD_VOICEMAIL USE_SIP PROCESS_OUTGOING_CALLS |
SENSORS | BODY_SENSORS |
SMS | SEND_SMS RECEIVE_SMS READ_SMS RECEIVE_WAP_PUSH RECEIVE_MMS |
STORAGE | READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE |
- if (ContextCompat.checkSelfPermission(
- CONTEXT, Manifest.permission.REQUESTED_PERMISSION) ==
- PackageManager.PERMISSION_GRANTED) { //检查是否有该权限
- // You can use the API that requires the permission.
- performAction(...); //已经获取权限后做自己的功能逻辑
- } else if (shouldShowRequestPermissionRationale(...)) {//如果用户选择了已经知道了,不再提醒,这个方法就返回true,你就可以在这里提醒用户,你点了不再提醒,你要用xx功能,那你就自己去授权吧,或者开发者自己做引导的逻辑
- // In an educational UI, explain to the user why your app requires this
- // permission for a specific feature to behave as expected. In this UI,
- // include a "cancel" or "no thanks" button that allows the user to
- // continue using your app without granting the permission.
- showInContextUI(...); 、、
- } else { //去问用户要权限
- // You can directly ask for the permission.
- // The registered ActivityResultCallback gets the result of this request.
- requestPermissionLauncher.launch(
- Manifest.permission.REQUESTED_PERMISSION);
- }
- if(ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS)!= PackageManager.PERMISSION_GRANTED){
- Log.i(TAG, "onCreate: request");
- if(ActivityCompat.shouldShowRequestPermissionRationale(this,Manifest.permission.READ_CONTACTS)){
- Log.i(TAG, "onCreate: shouldShowRequestPermissionRationale");
- Toast.makeText(context,"应用不再提醒获取读取通讯录的权限,如果想获取通讯录,请到应用权限管理中给应用读取通讯录的权限",Toast.LENGTH_SHORT).show();
- }else
- ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_CONTACTS},1);
- }else{
- Cursor cursor1 = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
- null, null, null, null);
- Log.i(TAG, "onCreate: 通讯录"+cursor1.getCount());
- while (cursor1.moveToNext()) {
- int id = cursor1.getInt(cursor1.getColumnIndex(
- ContactsContract.Contacts._ID));
- String name1 = cursor1.getString(cursor1.getColumnIndex(
- ContactsContract.Contacts.DISPLAY_NAME));
- Log.i(TAG, "onCreate: 通讯录: "+id+" "+name1);
- }
- cursor1.close();
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- Log.i(TAG, "onRequestPermissionsResult: "+permissions[0]+grantResults[0]);
- if(requestCode==1){
- if(grantResults[0]==0){
- Cursor cursor1 = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
- null, null, null, null);
- Log.i(TAG, "onCreate: 通讯录"+cursor1.getCount());
- while (cursor1.moveToNext()) {
- int id = cursor1.getInt(cursor1.getColumnIndex(
- ContactsContract.Contacts._ID));
- String name1 = cursor1.getString(cursor1.getColumnIndex(
- ContactsContract.Contacts.DISPLAY_NAME));
- Log.i(TAG, "onCreate: 通讯录: "+id+" "+name1);
- }
- cursor1.close();
- }else {
- Toast.makeText(context,"没有得到权限,无法正常获取通讯录",Toast.LENGTH_SHORT).show();
- }
- }
- super.onRequestPermissionsResult(requestCode, permissions, grantResults);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。