做有关6.0权限的问题只需要在mainactiviy中一次性把所有的权限都让用户确认一遍就行。
public class MainActivity extends AppCompatActivity {
private static final String TAG ="MainActivity" ;
private boolean isRequireCheck; // 是否需要系统权限检测
private PermissionsChecker mPermissionsChecker;//检查权限
//危险权限(运行时权限)
static final String[] PERMISSIONS = new String[]{
Manifest.permission.CAMERA,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.CALL_PHONE,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.RECORD_AUDIO
};
private static final int PERMISSION_REQUEST_CODE = 0;
private String versionCode;
private String substring;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPermissionsChecker = new PermissionsChecker(this);
isRequireCheck = true;
versionCode = Build.VERSION.RELEASE;
substring = versionCode.substring(0, 1);
L.d(TAG,"我是版本号-==="+ versionCode);
}
@Override
protected void onResume() {
super.onResume();
/**
* 6.0权限
*/
if (isRequireCheck) {
//权限没有授权,进入授权界面
if(mPermissionsChecker.judgePermissions(PERMISSIONS)){
ActivityCompat.requestPermissions(this, PERMISSIONS, PERMISSION_REQUEST_CODE);
}
}else{
isRequireCheck = true;
}
}
/**
* 用户权限处理,
* 如果全部获取, 则直接过.
* 如果权限缺失, 则提示Dialog.
* @param requestCode 请求码
* @param permissions 权限
* @param grantResults 结果
*/
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE && hasAllPermissionsGranted(grantResults)) {
isRequireCheck = true;
} else {
isRequireCheck = false;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
showPermissionDialog();
}
}
}
// 含有全部的权限
private boolean hasAllPermissionsGranted( int[] grantResults) {
for (int grantResult : grantResults) {
if (grantResult == PackageManager.PERMISSION_DENIED) {
return false;
}
}
return true;
}
/**
* 提示对话框
*/
private void showPermissionDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("帮助");
builder.setMessage("当前应用缺少必要权限。请点击\"设置\"-打开所需权限。");
// 拒绝, 退出应用
builder.setNegativeButton("退出", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
// setResult(PERMISSIONS_DENIED);
finish();
}
});
builder.setPositiveButton("设置", new DialogInterface.OnClickListener() {
@Override public void onClick(DialogInterface dialog, int which) {
startAppSettings();
}
});
builder.setCancelable(false);
builder.show();
}
// 启动应用的设置
private void startAppSettings() {
Intent appDetailSettingIntent = Authority.getAppDetailSettingIntent(this);
startActivity(appDetailSettingIntent);
}
}
public class PermissionsChecker { private Context mContext; public PermissionsChecker(Context context){ mContext = context.getApplicationContext(); } /** * 判断权限 */ public boolean judgePermissions(String...permissions){ for(String permission:permissions){ if(deniedPermission(permission)){ return true; } } return false; } /** * 判断是否缺少权限 * PackageManager.PERMISSION_GRANTED 授予权限 * PackageManager.PERMISSION_DENIED 缺少权限 * */ private boolean deniedPermission(String permission){ return ContextCompat.checkSelfPermission(mContext,permission)== PackageManager.PERMISSION_DENIED; } }
public class Authority {
/**
* 相机
* 返回true 表示可以使用 返回false表示不可以使用
*/
public static boolean cameraIsCanUse() {
boolean isCanUse = true;
Camera mCamera = null;
try {
mCamera = Camera.open();
Camera.Parameters mParameters = mCamera.getParameters();
mCamera.setParameters(mParameters);
} catch (Exception e) {
isCanUse = false;
}
if (mCamera != null) {
try {
mCamera.release();
} catch (Exception e) {
e.printStackTrace();
return isCanUse;
}
}
return isCanUse;
}
// /**
// * 华为的权限管理页面
// */
// private static void gotoHuaweiPermission() {
// try {
// Intent intent = new Intent();
// intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// ComponentName comp = new ComponentName("com.huawei.systemmanager", "com.huawei.permissionmanager.ui.MainActivity");//华为权限管理
// intent.setComponent(comp);
// startActivity(intent);
// } catch (Exception e) {
// e.printStackTrace();
// startActivity(getAppDetailSettingIntent());
// }
//
// }
/**
* 跳转到miui的权限管理页面
*/
// private void gotoMiuiPermission() {
// Intent i = new Intent("miui.intent.action.APP_PERM_EDITOR");
// ComponentName componentName = new ComponentName("com.miui.securitycenter", "com.miui.permcenter.permissions.AppPermissionsEditorActivity");
// i.setComponent(componentName);
// i.putExtra("extra_pkgname", getPackageName());
// try {
// startActivity(i);
// } catch (Exception e) {
// e.printStackTrace();
// gotoMeizuPermission();
// }
// }
// /**
// * 跳转到魅族的权限管理系统
// */
// private void gotoMeizuPermission() {
// Intent intent = new Intent("com.meizu.safe.security.SHOW_APPSEC");
// intent.addCategory(Intent.CATEGORY_DEFAULT);
// intent.putExtra("packageName", BuildConfig.APPLICATION_ID);
// try {
// startActivity(intent);
// } catch (Exception e) {
// e.printStackTrace();
// gotoHuaweiPermission();
// }
// }
/**
* 获取应用详情页面intent
*
* @return
*/
public static Intent getAppDetailSettingIntent(Context context) {
Intent localIntent = new Intent();
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (Build.VERSION.SDK_INT >= 9) {
localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
localIntent.setData(Uri.fromParts("package", getPackageName(context), null));
} else if (Build.VERSION.SDK_INT <= 8) {
localIntent.setAction(Intent.ACTION_VIEW);
localIntent.setClassName("com.android.settings", "com.android.settings.InstalledAppDetails");
localIntent.putExtra("com.android.settings.ApplicationPkgName", getPackageName(context));
}
return localIntent;
}
private static String getPackageName(Context context) {
PackageManager pm = context.getPackageManager();
PackageInfo packageInfo = null;
try {
packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return packageInfo.packageName;
}
}