此模块基于AndPermission进行二次封装
为AndPermission加入依赖
implementation 'com.yanzhenjie:permission:2.0.3' //添加依赖
1
封装类CheckPermission
public class CheckPermission {
private Context context;
private static CheckPermission checkPermission;
private static final String PACKAGE_HEADER = "package:";
public CheckPermission(Context context) {
this.context = context;
}
郑州不孕不育医院那家好:http://jbk.39.net/yiyuanfengcai/tsyl_zztjyy/1449/
/**
* 获取权限
*
* @param listener
* @param permission
*/
@SuppressLint("WrongConstant")
public void requestPermission(String [] permission,final PermissionLinstener listener) {
AndPermission.with(context)
.runtime()
.permission(permission)
// .rationale(context)//添加拒绝权限回调
.onGranted(new Action>() {
@Override
public void onAction(List
// data.get(0);
Log.d("permission", data.get(0));
listener.onSuccess(context,data);
}
})
.onDenied(new Action>() {
@Override
public void onAction(List
/**
* 当用户没有允许该权限时,回调该方法
*/
listener.onFailed(context,data);
/**
* 判断用户是否点击了禁止后不再询问,AndPermission.hasAlwaysDeniedPermission(MainActivity.this, data)
* 如要引导用户跳转至应该权限设置页面,可用intent
*/
if (AndPermission.hasAlwaysDeniedPermission(context, data)) {
//true,弹窗再次向用户索取权限
listener.onNotApply(context,data);
}
}
}).start();
}
/**
* 引导用户到系统设置页面
*/
public void GuidePermission(){
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse(PACKAGE_HEADER + context.getPackageName()));
context.startActivity(intent);
}
郑州好的不孕不育医院有哪些:http://www.xbzztj.com/
public interface PermissionLinstener {
void onSuccess(Context context,List
void onFailed(Context context,List
void onNotApply(Context context,List
}
}
使用案例
final String [] strings = new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE};
new CheckPermission(context).requestPermission(strings, new CheckPermission.PermissionLinstener() {
@Override
public void onSuccess(Context context, List
Toast.makeText(context, "申请成功", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailed(Context context, List
Toast.makeText(context, "申请失败", Toast.LENGTH_SHORT).show();
}
/**
* 可在此调用,GuidePermission();引导用户前往系统设置页面获取权限
* @param context
* @param data
*/
@Override
public void onNotApply(final Context context, List
Toast.makeText(context, "用户点击了不再提示", Toast.LENGTH_SHORT).show();
}
});
不要忘了在清单中,加入注册权限