Android 6.0权限申请详解及权限资料整理
在android6.0开始,部分的权限需要我们动态申请,也就是说当我们的打开app的时候系统不会主动像您申请app所需要的部分权限,需要客户在使用app的时候主动的去申请。
一、权限的申请两步骤:
1、权限申请:
/** *@parampermissions需要申请的权限 *@paramrequestCode申请回调code */ publicstaticvoidrequestPermissions(final@NonNullActivityactivity,final@NonNullString[]permissions,finalintrequestCode)
2、申请回调处理:
/** *@paramrequestCode申请回调code *@parampermissions申请的权限 *@paramgrantResults返回申请状态PackageManager#PERMISSION_GRANTED(授予)PackageManager#PERMISSION_DENIED(拒绝) */ publicvoidonRequestPermissionsResult(intrequestCode,@NonNullString[]permissions,@NonNullint[]grantResults)
二、Fragment中运行时权限的特殊处理
在Fragment中申请权限,不要使用ActivityCompat.requestPermissions,直接使用Fragment的requestPermissions方法,否则会回调到Activity的onRequestPermissionsResult如果在Fragment中嵌套Fragment,在子Fragment中使用requestPermissions方法,onRequestPermissionsResult不会回调回来,建议使用getParentFragment().requestPermissions方法,
这个方法会回调到父Fragment中的onRequestPermissionsResult,加入以下代码可以把回调透传到子Fragment
@Override publicvoidonRequestPermissionsResult(intrequestCode,String[]permissions,int[]grantResults){ super.onRequestPermissionsResult(requestCode,permissions,grantResults); Listfragments=getChildFragmentManager().getFragments(); if(fragments!=null){ for(Fragmentfragment:fragments){ if(fragment!=null){ fragment.onRequestPermissionsResult(requestCode,permissions,grantResults); } } } }
三、权限申请的简单封装
publicclassRequestPermissions{ publicstaticintREQUEST_EXTERNAL_STORAGE=1; publicstaticString[]PERMISSIONS_STORAGE={ Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE }; /** *读写权限申请回调true有权限false无权限 * *@parammContext */ publicstaticvoidwriteExternalStorage(finalContextmContext,PermissionCallBackmCallBack){ //检查是否拥有相应的权限 intpermission=ActivityCompat.checkSelfPermission(mContext,Manifest.permission.WRITE_EXTERNAL_STORAGE); //没有权限发起申请 if(permission!=PackageManager.PERMISSION_GRANTED){ //6.0及以上版本才需要申请权限 if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) ActivityCompat.requestPermissions((Activity)mContext,PERMISSIONS_STORAGE,REQUEST_EXTERNAL_STORAGE); else{ mCallBack.setOnPermissionListener(false); } }else{ mCallBack.setOnPermissionListener(true); } } /** *读写权限申请回调true有权限false无权限 * *@parammContext */ publicstaticvoidwriteExternalStorage(finalContextmContext,PermissionCallBackmCallBack,intrequestCode){ //检查是否拥有相应的权限 intpermission=ActivityCompat.checkSelfPermission(mContext,Manifest.permission.WRITE_EXTERNAL_STORAGE); //没有权限发起申请 if(permission!=PackageManager.PERMISSION_GRANTED){ //6.0及以上版本才需要申请权限 if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M) ActivityCompat.requestPermissions((Activity)mContext,PERMISSIONS_STORAGE,requestCode); else{ mCallBack.setOnPermissionListener(false); } }else{ mCallBack.setOnPermissionListener(true); } } /** *权限申请回调,需要在你调用的Activity中onRequestPermissionsResult调用该方法true申请权限成功false申请权限失败 * *@paramrequestCode *@parampermissions *@paramgrantResults */ publicstaticvoidonRequestPermissionsResult(intrequestCode,@NonNullString[]permissions,@NonNullint[]grantResults,PermissionCallBackmCallBack){ if(requestCode==REQUEST_EXTERNAL_STORAGE){ //判断权限是否申请成功 if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){ mCallBack.setOnPermissionListener(true); }else{ mCallBack.setOnPermissionListener(false); } return; } } /** *6.0以下申请打开权限管理 * *@parammContext */ publicstaticvoidopenPre(finalContextmContext){ RemindDialogUtil.showRemindDialog(mContext,"當前無權限,將無法進行應用升級~,是否打开设置?",newRemindDialogUtil.DialogCallBack(){ @Override publicvoidclickYes(){ //跳转到设置页面,手动打开权限 Intentintent=newIntent(); intent.setAction(Settings.ACTION_APPLICATION_SETTINGS); mContext.startActivity(intent); RemindDialogUtil.hideRemindDialog(); } @Override publicvoidclickCancel(){ RemindDialogUtil.hideRemindDialog(); } }); } publicinterfacePermissionCallBack{ voidsetOnPermissionListener(Booleanbo); } }
附录
以下权限都需要在运行时判断:
身体传感器
日历
摄像头
通讯录
地理位置
麦克风
电话
短信
存储空间
以下权限只需要在AndroidManifest.xml中声明即可使用
android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_NOTIFICATION_POLICY
android.permission.ACCESS_WIFI_STATE
android.permission.ACCESS_WIMAX_STATE
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
android.permission.BROADCAST_STICKY
android.permission.CHANGE_NETWORK_STATE
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.CHANGE_WIMAX_STATE
android.permission.DISABLE_KEYGUARD
android.permission.EXPAND_STATUS_BAR
android.permission.FLASHLIGHT
android.permission.GET_ACCOUNTS
android.permission.GET_PACKAGE_SIZE
android.permission.INTERNET
android.permission.KILL_BACKGROUND_PROCESSES
android.permission.MODIFY_AUDIO_SETTINGS
android.permission.NFC
android.permission.READ_SYNC_SETTINGS
android.permission.READ_SYNC_STATS
android.permission.RECEIVE_BOOT_COMPLETED
android.permission.REORDER_TASKS
android.permission.REQUEST_INSTALL_PACKAGES
android.permission.SET_TIME_ZONE
android.permission.SET_WALLPAPER
android.permission.SET_WALLPAPER_HINTS
android.permission.SUBSCRIBED_FEEDS_READ
android.permission.TRANSMIT_IR
android.permission.USE_FINGERPRINT
android.permission.VIBRATE
android.permission.WAKE_LOCK
android.permission.WRITE_SYNC_SETTINGS
com.android.alarm.permission.SET_ALARM
com.android.launcher.permission.INSTALL_SHORTCUT
com.android.launcher.permission.UNINSTALL_SHORTCUT
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!