/**
* 动态获取权限,Android 6.0 新特性,一些保护权限,除了要在 AndroidManifest 中声明权限,
* 还要使用如下代码动态获取
*/
if (Build.VERSION.SDK_INT >= 23) {
int REQUEST_CODE_CONTACT = 101;
// 可以在 String[] 数组中继续添加需要的权限
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
// 验证是否许可权限
for (String str : permissions) {
if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
// 申请权限
this.requestPermissions(permissions, REQUEST_CODE_CONTACT);
return;
}
}
}
实际使用
BaseActivity 中
public class BaseActivity extends AppCompatActivity {
private static final int REQUEST_CODE = 1024;
......
/**
* 权限请求结果回调
*
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
@NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
boolean hasAllGranted = true;
StringBuilder permissionName = new StringBuilder();
for (String s : permissions) {
permissionName = permissionName.append(s + "\r\n");
}
switch (requestCode) {
case REQUEST_CODE: {
for (int i = 0; i < grantResults.length; ++i) {
if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
hasAllGranted = false;
// 在用户已经拒绝授权的情况下,如果 shouldShowRequestPermissionRationale 返回 false 则
// 可以推断出用户选择了“不在提示”选项,在这种情况下需要引导用户至设置页手动授权
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[i])) {
new AlertDialog.Builder(this)
.setMessage(getResources().getString(R.string.permission_hint)
+ permissionName
+ getResources().getString(R.string.empower_hint))
.setPositiveButton(getResources().getString(R.string.to_empower),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent =
new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package",
getApplicationContext().getPackageName(),
null);
intent.setData(uri);
startActivity(intent);
}
})
.setNegativeButton(getResources().getString(R.string.cancel),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mRequestPermissionCallBack.denied();
}
}).setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
mRequestPermissionCallBack.denied();
}
}).show();
} else {
// 用户拒绝权限请求,但未选中“不再提示”选项
mRequestPermissionCallBack.denied();
}
break;
}
}
if (hasAllGranted) {
mRequestPermissionCallBack.granted();
}
}
}
}
/**
* 发起权限请求
*
* @param context
* @param permissions
* @param callback
*/
public void requestPermissions(final Context context, final String[] permissions,
RequestPermissionCallBack callback) {
this.mRequestPermissionCallBack = callback;
StringBuilder permissionNames = new StringBuilder();
for (String s : permissions) {
permissionNames = permissionNames.append(s + "\r\n");
}
// 如果所有权限都已授权,则直接返回授权成功,只要有一项未授权,则发起权限请求
boolean isAllGranted = true;
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_DENIED) {
isAllGranted = false;
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, permission)) {
new AlertDialog.Builder(context)
.setMessage(getResources().getString(R.string.permission_apply_hint)
+ permissionNames
+ getResources().getString(R.string.permission_allow_hint))
.setPositiveButton(getResources().getString(R.string.confirm),
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions(((Activity) context), permissions,
REQUEST_CODE);
}
}).show();
} else {
ActivityCompat.requestPermissions(((Activity) context), permissions, REQUEST_CODE);
}
break;
}
}
if (isAllGranted) {
mRequestPermissionCallBack.granted();
return;
}
}
/**
* 权限请求结果回调接口
*/
public interface RequestPermissionCallBack {
/**
* 同意授权
*/
public void granted();
/**
* 取消授权
*/
public void denied();
}
在打开 APP 后的第一个 Activity 中使用:
public class WelcomeActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome_layout);
context = this;
requestMultiPermission();
}
/**
* 统一的程序权限控制入口
*/
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
public void requestMultiPermission() {
// 需要动态申请的权限往 String[] 里加,并且别忘了在清单文件里也需要加权限
String[] permissions = new String[] {
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.READ_PHONE_STATE,
Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(this, permissions,
new RequestPermissionCallBack() {
@Override
public void granted() {
// 请求权限成功后进行的操作
}
@Override
public void denied() {
// 请求权限失败后进行的操作
}
});
}
}
上面代码中用到的一些字符串:
<resources xmlns:tools="http://schemas.android.com/tools">
<string name="permission_hint" tools:ignore="ExtraTranslation">
【用户选择了不在提示按钮,或者系统默认不在提示(如MIUI)。引导用户到应用设置页去手动授权,注意提示用户具体需要哪些权限】\r\n获取相关权限失败:\r\n
</string>
<string name="empower_hint">将导致部分功能无法正常使用,需要到设置页面手动授权</string>
<string name="to_empower">去授权</string>
<string name="permission_apply_hint">警告:\n您好,需要如下权限:\n</string>
<string name="permission_allow_hint">请允许,否则将影响部分功能的正常使用。</string>
<string name="get_permission_failed_hint">获取权限失败,正常功能受到影响,退出程序</string>
<string name="cancel">取消</string>
<string name="confirm">确认</string>
</resources>
网友评论