Android 6.0开发的权限注意事项
2016-07-26 10:42
549 查看
由于Android 6.0推出了新的权限机制,对于敏感权限进行了限制,即使在Android的AndroidManifest.xml中申明仍然需要进行授权,所以代码解决方法如下:
节选自Dropbox的Demo
private void performWithPermissions(final FileAction action) {
if (hasPermissionsForAction(action)) {
performAction(action);
return;
}
if (shouldDisplayRationaleForAction(action)) {
new AlertDialog.Builder(this)
.setMessage("This app requires storage access to download and upload files.")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermissionsForAction(action);
}
})
.setNegativeButton("Cancel", null)
.create()
.show();
} else {
requestPermissionsForAction(action);
}
}
private boolean hasPermissionsForAction(FileAction action) {
for (String permission : action.getPermissions()) {
int result = ContextCompat.checkSelfPermission(this, permission);
if (result == PackageManager.PERMISSION_DENIED) {
return false;
}
}
return true;
}
private boolean shouldDisplayRationaleForAction(FileAction action) {
for (String permission : action.getPermissions()) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
return true;
}
}
return false;
}
private void requestPermissionsForAction(FileAction action) {
ActivityCompat.requestPermissions(
this,
action.getPermissions(),
action.getCode()
);
}
private enum FileAction {
DOWNLOAD(Manifest.permission.WRITE_EXTERNAL_STORAGE),
UPLOAD(Manifest.permission.READ_EXTERNAL_STORAGE);
private static final FileAction [] values = values();
private final String [] permissions;
FileAction(String ... permissions) {
this.permissions = permissions;
}
public int getCode() {
return ordinal();
}
public String [] getPermissions() {
return permissions;
}
public static FileAction fromCode(int code) {
if (code < 0 || code >= values.length) {
throw new IllegalArgumentException("Invalid FileAction code: " + code);
}
return values[code];
}
}
要不然引导用户在App Setting中授权:
最后是效果:
节选自Dropbox的Demo
private void performWithPermissions(final FileAction action) {
if (hasPermissionsForAction(action)) {
performAction(action);
return;
}
if (shouldDisplayRationaleForAction(action)) {
new AlertDialog.Builder(this)
.setMessage("This app requires storage access to download and upload files.")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermissionsForAction(action);
}
})
.setNegativeButton("Cancel", null)
.create()
.show();
} else {
requestPermissionsForAction(action);
}
}
private boolean hasPermissionsForAction(FileAction action) {
for (String permission : action.getPermissions()) {
int result = ContextCompat.checkSelfPermission(this, permission);
if (result == PackageManager.PERMISSION_DENIED) {
return false;
}
}
return true;
}
private boolean shouldDisplayRationaleForAction(FileAction action) {
for (String permission : action.getPermissions()) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
return true;
}
}
return false;
}
private void requestPermissionsForAction(FileAction action) {
ActivityCompat.requestPermissions(
this,
action.getPermissions(),
action.getCode()
);
}
private enum FileAction {
DOWNLOAD(Manifest.permission.WRITE_EXTERNAL_STORAGE),
UPLOAD(Manifest.permission.READ_EXTERNAL_STORAGE);
private static final FileAction [] values = values();
private final String [] permissions;
FileAction(String ... permissions) {
this.permissions = permissions;
}
public int getCode() {
return ordinal();
}
public String [] getPermissions() {
return permissions;
}
public static FileAction fromCode(int code) {
if (code < 0 || code >= values.length) {
throw new IllegalArgumentException("Invalid FileAction code: " + code);
}
return values[code];
}
}
要不然引导用户在App Setting中授权:
最后是效果:
相关文章推荐
- Android Studio你不知道的调试技巧
- 写一个android小闹钟
- Android如何实现自毁
- android 浏览器搜索记录
- Android蓝牙通信聊天实现发送和接受功能
- Android Studio修改包名
- Android特效源码
- Android Permission denied 错误(附Android权限大全)
- Android——2016新技术Rxjava
- Android碎碎念 -- 阿里Weex
- Android内存泄漏的8种可能
- android—DatePicker 和TimePicker显示日期以及使用TimePickerDialog,DatePickerDialog来专门实现时间选择对话框
- Android设计模式之策略模式
- Android和h5混合开发之URL解析
- Android Dialog用法
- Android初学之Handler
- 持久化修改Android模拟器的system分区
- android 中的 window,view,activity具体关系
- Android性能优化之被忽视的Memory Leaks
- Android中部分特殊的显示