在android 6.0开始,部分的权限需要我们动态申请,也就是说当我们的打开app的时候系统不会主动向您申请app所需要的部分权限,需要客户在使用app的时候主动的去申请。
一、权限的申请两步骤:
1、权限申请:
/**
* @param permissions需要申请的权限
* @param requestCode申请回调code
*/
public static void requestPermissions(final @NonNull Activity activity,final @NonNullString[] permissions, final int requestCode)
2、申请回调处理:
/**
*@param requestCode申请回调code
*@param permissions申请的权限
*@param grantResults返回申请状态PackageManager#PERMISSION_GRANTED(授予) PackageManager#PERMISSION_DENIED(拒绝)
*/
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
二、Fragment中运行时权限的特殊处理
在Fragment中申请权限,不要使用ActivityCompat.requestPermissions, 直接使用Fragment的requestPermissions方法,否则会回调到Activity的onRequestPermissionsResult如果在Fragment中嵌套Fragment,在子Fragment中使用requestPermissions方 法,onRequestPermissionsResult不会回调回来,建议使用 getParentFragment().requestPermissions方法,
这个方法会回调到父Fragment中的onRequestPermissionsResult,加入以下代码可以把回调透传到子Fragment
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
List fragments = getChildFragmentManager().getFragments();
if (fragments != null) {
for (Fragment fragment : fragments) {
if (fragment != null) {
fragment.onRequestPermissionsResult(requestCode,permissions,grantResults);
}
}
}
}
三、权限申请的简单封装
public class RequestPermissions {
public static intREQUEST_EXTERNAL_STORAGE = 1;
public static String[]PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
/**
*读写权限申请回调true有权限false无权限
*
* @param mContext
*/
public static voidwriteExternalStorage(final Context mContext, PermissionCallBack mCallBack) {
//检查是否拥有相应的权限
int permission =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无权限
*
* @param mContext
*/
public static voidwriteExternalStorage(final Context mContext, PermissionCallBack mCallBack, intrequestCode) {
//检查是否拥有相应的权限
int permission =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申请权限失败
*
* @param requestCode
* @param permissions
* @param grantResults
*/
public static void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults, PermissionCallBack mCallBack) {
if (requestCode ==REQUEST_EXTERNAL_STORAGE) {
//判断权限是否申请成功
if(grantResults.length > 0 && grantResults[0] ==PackageManager.PERMISSION_GRANTED) {
mCallBack.setOnPermissionListener(true);
} else {
mCallBack.setOnPermissionListener(false);
}
return;
}
}
/**
* 6.0以下申请打开权限管理
*
* @param mContext
*/
public static void openPre(final Context mContext) {
RemindDialogUtil.showRemindDialog(mContext, "當前無權限,將無法進行應用升級~,是否打开设置?", new RemindDialogUtil.DialogCallBack() {
@Override
public void clickYes() {
//跳转到设置页面,手动打开权限
Intent intent= new Intent();
intent.setAction(Settings.ACTION_APPLICATION_SETTINGS);
mContext.startActivity(intent);
RemindDialogUtil.hideRemindDialog();
}
@Override
public voidclickCancel() {
RemindDialogUtil.hideRemindDialog();
}
});
}
public interfacePermissionCallBack {
void setOnPermissionListener(Boolean bo);
}
}
附录
以下权限都需要在运行时判断:
身体传感器
日历
摄像头
通讯录
地理位置
麦克风
电话
短信
存储空间
以下权限只需要在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
网友评论