/**
* @ClassName: BaseFragment
* @Description:
* @author: Endeavor
* @date: 2017/4/5
*/
public abstract class BaseFragment extends Fragment {
private PermissionsResultListener mListener;
private int mRequestCode;
/**
* 其他 fragment 继承 BaseFragment 调用 performRequestPermissions 方法
*
* @param desc 首次申请权限被拒绝后再次申请给用户的描述提示
* @param permissions 要申请的权限数组
* @param requestCode 申请标记值
* @param listener 实现的接口
*/
protected void performRequestPermissions(String desc, String[] permissions, int requestCode, PermissionsResultListener listener) {
if (permissions == null || permissions.length == 0) return;
mRequestCode = requestCode;
mListener = listener;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
List<String> mPermissionList = checkPermission(permissions);
if (!mPermissionList.isEmpty()) {// 检查是否声明了权限
String[] temp = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
requestEachPermissions(desc, temp, requestCode);
} else {
// 已经申请权限
if (mListener != null) {
mListener.onPermissionGranted();
}
}
} else {
if (mListener != null) {
mListener.onPermissionGranted();
}
}
}
/**
* 申请权限前判断是否需要声明
*
* @param desc
* @param permissions
* @param requestCode
*/
private void requestEachPermissions(String desc, String[] permissions, int requestCode) {
if (showRationale(permissions)) {// 需要再次声明
showRationaleDialog(desc, permissions, requestCode);
} else {
requestPermissions(permissions, requestCode);
}
}
/**
* 弹出声明的 Dialog
*
* @param desc
* @param permissions
* @param requestCode
*/
private void showRationaleDialog(String desc, final String[] permissions, final int requestCode) {
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("提示")
.setMessage(desc)
.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
requestPermissions(permissions, requestCode);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
})
.setCancelable(false)
.show();
}
/**
* 再次申请权限时,是否需要声明
*
* @param permissions
* @return
*/
private boolean showRationale(String[] permissions) {
for (String permission : permissions) {
if (shouldShowRequestPermissionRationale(permission)){
return true;
}
}
return false;
}
/**
* 检察每个权限是否申请
*
* @param permissions
* @return true 需要申请权限,false 已申请权限
*/
private List<String> checkPermission(String[] permissions) {
List<String> data = new ArrayList<>();
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(getActivity(), permission) != PackageManager.PERMISSION_GRANTED) {
data.add(permission);
}
}
return data;
}
/**
* 申请权限结果的回调
*
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == mRequestCode) {
if (checkEachPermissionsGranted(grantResults)) {
if (mListener != null) {
mListener.onPermissionGranted();
}
} else {// 用户拒绝申请权限
if (mListener != null) {
mListener.onPermissionDenied();
}
}
}
}
/**
* 检查回调结果
*
* @param grantResults
* @return
*/
private boolean checkEachPermissionsGranted(int[] grantResults) {
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
/**
* 权限申请接口
*/
public interface PermissionsResultListener {
void onPermissionGranted();
void onPermissionDenied();
}
}
BaseFragment 运行时权限
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 运行时权限 ----- 需要动态申请 联系人分组 group:android.permission-group.C...
- 0x00:前言 对于Android6.0运行时权限的处理方式网上有很多,包括注解,RxJava等等。一直没有正面提...
- 1.0 APP在程序内部进行操作,当然是安全的且合理的,但不同应用程序之间如何进行数据访问?比如这个例子,需要自己...