android权限适配 简书,Android 6.0 权限适配

Launcher Activity

public class SplashActivity extends BaseActivity {

// 所需的全部权限

static final String[] PERMISSIONS = new String[]{

Manifest.permission.READ_PHONE_STATE,

Manifest.permission.READ_EXTERNAL_STORAGE,

Manifest.permission.WRITE_EXTERNAL_STORAGE,

Manifest.permission.CAMERA,

Manifest.permission.GET_ACCOUNTS,

Manifest.permission.CALL_PHONE,

};

private PermissionsChecker mPermissionsChecker; // 权限检测器

@Override

public void onCreateActivity(Bundle budle) {

setContentView(R.layout.*****);

mPermissionsChecker = new PermissionsChecker(this);

initPermission();

}

private void initPermission() {

// 缺少权限时, 进入权限配置页面

if (mPermissionsChecker.lacksPermissions(PERMISSIONS)) {

startPermissionsActivity();

}else{

if(NetUtils.isConnected(this)){

initData();

}else{

showIsConnetionDialog();

}

}

}

private void startPermissionsActivity() {

PermissionsActivity.startActivityForResult(this, REQUEST_CODE, PERMISSIONS);

}

}

PermissionChecker.java

/**

* 检查权限的工具类

*/

public class PermissionsChecker {

private final Context mContext;

public PermissionsChecker(Context context) {

mContext = context.getApplicationContext();

}

// 判断权限集合

public boolean lacksPermissions(String... permissions) {

for (String permission : permissions) {

if (lacksPermission(permission)) {

return true;

}

}

return false;

}

// 判断是否缺少权限

private boolean lacksPermission(String permission) {

return ContextCompat.checkSelfPermission(mContext, permission) ==

PackageManager.PERMISSION_DENIED;

}

}

PermissionsActivity.java

/**

* 权限获取页面

*/

public class PermissionsActivity extends AppCompatActivity {

public static final int PERMISSIONS_GRANTED = 0; // 权限授权

public static final int PERMISSIONS_DENIED = 1; // 权限拒绝

private static final int PERMISSION_REQUEST_CODE = 0; // 系统权限管理页面的参数

private static final String EXTRA_PERMISSIONS =

"com.zhaoly.extra_permission"; // 权限参数

private static final String PACKAGE_URL_SCHEME = "package:"; // 方案

private PermissionsChecker mChecker; // 权限检测器

private boolean isRequireCheck; // 是否需要系统权限检测, 防止和系统提示框重叠

// 启动当前权限页面的公开接口

public static void startActivityForResult(Activity activity, int requestCode, String... permissions) {

Intent intent = new Intent(activity, PermissionsActivity.class);

intent.putExtra(EXTRA_PERMISSIONS, permissions);

ActivityCompat.startActivityForResult(activity, intent, requestCode, null);

}

@Override protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

if (getIntent() == null || !getIntent().hasExtra(EXTRA_PERMISSIONS)) {

throw new RuntimeException("PermissionsActivity需要使用静态startActivityForResult方法启动!");

}

setContentView(R.layout.activity_permissions);

mChecker = new PermissionsChecker(this);

isRequireCheck = true;

}

@Override protected void onResume() {

super.onResume();

if (isRequireCheck) {

String[] permissions = getPermissions();

if (mChecker.lacksPermissions(permissions)) {

requestPermissions(permissions); // 请求权限

} else {

allPermissionsGranted(); // 全部权限都已获取

}

} else {

isRequireCheck = true;

}

}

// 返回传递的权限参数

private String[] getPermissions() {

return getIntent().getStringArrayExtra(EXTRA_PERMISSIONS);

}

// 请求权限兼容低版本

private void requestPermissions(String... permissions) {

ActivityCompat.requestPermissions(this, permissions, PERMISSION_REQUEST_CODE);

}

// 全部权限均已获取

private void allPermissionsGranted() {

setResult(PERMISSIONS_GRANTED);

finish();

}

/**

* 用户权限处理,

* 如果全部获取, 则直接过.

* 如果权限缺失, 则提示Dialog.

*

* @param requestCode 请求码

* @param permissions 权限

* @param grantResults 结果

*/

@Override

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

if (requestCode == PERMISSION_REQUEST_CODE && hasAllPermissionsGranted(grantResults)) {

isRequireCheck = true;

allPermissionsGranted();

} else {

isRequireCheck = false;

showMissingPermissionDialog();

setResult(PERMISSIONS_DENIED);

finish();

}

}

// 含有全部的权限

private boolean hasAllPermissionsGranted(@NonNull int[] grantResults) {

for (int grantResult : grantResults) {

if (grantResult == PackageManager.PERMISSION_DENIED) {

return false;

}

}

return true;

}

// 显示缺失权限提示

private void showMissingPermissionDialog() {

AlertDialog.Builder builder = new AlertDialog.Builder(PermissionsActivity.this);

builder.setTitle(R.string.help);

builder.setMessage(R.string.string_help_text);

// 拒绝, 退出应用

builder.setNegativeButton(R.string.quit, new DialogInterface.OnClickListener() {

@Override public void onClick(DialogInterface dialog, int which) {

setResult(PERMISSIONS_DENIED);

finish();

}

});

builder.setPositiveButton(R.string.settings, new DialogInterface.OnClickListener() {

@Override public void onClick(DialogInterface dialog, int which) {

startAppSettings();

}

});

builder.setCancelable(false);

builder.show();

}

// 启动应用的设置

private void startAppSettings() {

Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);

intent.setData(Uri.parse(PACKAGE_URL_SCHEME + getPackageName()));

startActivity(intent);

}

}

当有权限无法全部获得时,会调用showMissingPermissionDialog()方法弹出设置对话框,请根据你的实际需求判断是否需要显示。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值