Skip to content

Instantly share code, notes, and snippets.

@Forgo7ten
Last active November 9, 2021 07:04
Show Gist options
  • Save Forgo7ten/060dde2d618d452d1ce37edaee86ffa7 to your computer and use it in GitHub Desktop.
Save Forgo7ten/060dde2d618d452d1ce37edaee86ffa7 to your computer and use it in GitHub Desktop.
Android动态申请权限 工具类
/** 调用示范 **/
public class MainActivity extends AppCompatActivity {
private PermissionsUtil permissionsUtil;
/**
* 动态申请权限
*/
private void requestPermission() {
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.READ_PHONE_STATE, Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO};
permissionsUtil = PermissionsUtil.with(this)
.permissions(permissions)
.onResult(new PermissionsUtil.PermissionsResult() {
@Override
public void onSuccess() {
Log.d(TAG, "onSuccess: 申请权限成功");
}
@Override
public void onFailed() {
Log.d(TAG, "onFailed: 申请权限失败");
}
});
permissionsUtil.check();
}
/**
* 权限申请回调方法
*
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions,
int[] grantResults) {
permissionsUtil.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* @ClassName PermissionsUtil
* @Description 动态申请权限 工具类
* @Author Forgo7ten
* @Date 2021/11/9
**/
public class PermissionsUtil {
/**
* 是否让用户前往设置手动授权
*/
public static boolean showSystemSetting = true;
/**
* 权限请求码
*/
private final int mRequestCode = 100;
/**
* Activity实例
*/
private final Activity mContext;
private final List<String> mPermissionList = new ArrayList<>();
/**
* 不再提示权限时的展示对话框
*/
AlertDialog mPermissionDialog;
/**
* 回调结果接口
*/
private PermissionsResult mPermissionsResult;
/**
* 构造方法
*
* @param context 传入Activity (this)
*/
private PermissionsUtil(Activity context) {
mContext = context;
}
/**
* 传入Activity对象
*
* @param context Activity对象
* @return 返回当前实例对象
*/
public static PermissionsUtil with(Activity context) {
return new PermissionsUtil(context);
}
/**
* 传入权限数组
*
* @param permissions 权限数组
* @return 返回当前实例对象
*/
public PermissionsUtil permissions(String[] permissions) {
// 清空权限数组
if (mPermissionList.size() != 0) {
mPermissionList.clear();
}
// 将权限添加到权限数组
mPermissionList.addAll(Arrays.asList(permissions));
return this;
}
/**
* 设置回调方法
*
* @param permissionsResult 传入实现接口的类
*/
public PermissionsUtil onResult(@NonNull PermissionsResult permissionsResult) {
mPermissionsResult = permissionsResult;
return this;
}
/**
* 开始检查权限
*/
public void check() {
new Thread(){
@Override
public void run() {
if (Build.VERSION.SDK_INT < 23) {
// 6.0及以上才用动态权限
mPermissionsResult.onSuccess();
return;
}
//创建一个mPermissionList,逐个判断哪些权限未授予,未授予的权限存储到mPerrrmissionList中
List<String> permissions = new ArrayList<>();
//逐个判断你要的权限是否已经通过
for (int i = 0; i < mPermissionList.size(); i++) {
if (ContextCompat.checkSelfPermission(mContext, mPermissionList.get(i)) != PackageManager.PERMISSION_GRANTED) {
permissions.add(mPermissionList.get(i));//添加还未授予的权限
}
}
// 开始申请权限
if (permissions.size() > 0) {
// 有权限没有通过,需要申请
String[] ps = permissions.toArray(new String[permissions.
size()]);
ActivityCompat.requestPermissions(mContext, ps, mRequestCode);
} else {
// 权限已经通过,执行回调
mPermissionsResult.onSuccess();
}
}
}.run();
}
/**
* 权限申请回调方法,需要在activity的onRequestPermissionsResult中调用
*
* @param requestCode
* @param permissions
* @param grantResults
*/
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
// 权限是否全部通过标志位
boolean hasPermissionDismiss = false;
if (mRequestCode == requestCode) {
for (int grantResult : grantResults) {
if (grantResult != PackageManager.PERMISSION_GRANTED) {
hasPermissionDismiss = true;
break;
}
}
// 如果有权限没有被允许
if (hasPermissionDismiss) {
if (showSystemSetting) {
// 跳转到系统设置权限页面,或者直接关闭页面,不让他继续访问
showSystemPermissionsSettingDialog();
} else {
mPermissionsResult.onFailed();
}
} else {
// 全部权限通过,可以进行下一步操作。。。
mPermissionsResult.onSuccess();
}
}
}
private void showSystemPermissionsSettingDialog() {
final String mPackName = mContext.getPackageName();
if (mPermissionDialog == null) {
mPermissionDialog = new AlertDialog.Builder(mContext)
.setMessage("已禁用权限,请手动授予")
.setPositiveButton("设置", (dialog, which) -> {
cancelPermissionDialog();
Uri packageURI = Uri.parse("package:" + mPackName);
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, packageURI);
mContext.startActivity(intent);
mContext.finish();
})
.setNegativeButton("取消", (dialog, which) -> {
//关闭页面或者做其他操作
cancelPermissionDialog();
//
mPermissionsResult.onFailed();
})
.create();
}
// 展示对话框
mPermissionDialog.show();
}
/**
* 在用户拒绝手动设置权限对话框后的操作
*/
private void cancelPermissionDialog() {
Toast.makeText(mContext, "必须同意所有权限才能使用本程序",
Toast.LENGTH_SHORT).show();
// 关闭对话框
if (mPermissionDialog != null) {
mPermissionDialog.cancel();
mPermissionDialog = null;
}
// 关闭Activity
mContext.finish();
}
/**
* 回调接口
*/
public interface PermissionsResult {
/**
* 申请权限成功后执行的回调方法
*/
void onSuccess();
/**
* 申请权限失败后执行的回调方法
*/
void onFailed();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment