android新旧权限
在解决同一段多线程下载代码在不同Android API运行时,低于23 API的只需要在清单文件配置权限就没什么问题,而运行在23上,即使在Manifest.xml配置了也依然出现错误提醒,open failed.对于权限系统呗重新设计后,自己深深感觉这是一个不能逃避的问题,记录下来方便自己与他人学习。
android studio新建项目,targetSdkVersion会自动设置为23。如果你的app没有支持新运行时权限,建议降到22.
实验1:在版本API18上进行多线程下载断点续传。(第三方jar包xUtils-2.6.14.jar)
运行代码:`
public void test1(View v) {
HttpUtils httpUtils = new HttpUtils();
String fileName = "adsafese.v1.1.1.9900.exe";
File file = new File(Environment.getExternalStorageDirectory(),fileName);
//确定下载地址
String path = "http://10.1.156.35:8080/" + fileName;
httpUtils.download(path,//下载地址
file.getPath(),//文件保存路径
true,//是否支持断点续传
true,//如果从请求返回信息中获取文件名字,下载完成后重新命名
new RequestCallBack<File>() {
//下载成功后调用
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
Toast.makeText(MainActivity.this, responseInfo.result.getPath(), Toast.LENGTH_SHORT).show();
}
//下载失败后调用
@Override
public void onFailure(HttpException e, String s) {
textView.setText(s);
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
progressBar.setMax((int)total);
progressBar.setProgress((int) current);
tv_progress.setText(current*100/total + "%");
}
}
);
}
实验结果:
实验结论:在代码没问题前提下,如果清单文件权限配置到位,运行不会出现问题。
实验2:在版本API 23上进行多线程下载断点续传。(第三方jar包xUtils-2.6.14.jar)
运行代码:
public void test2(View v) {
HttpUtils httpUtils = new HttpUtils();
String fileName = "adsafese.v1.1.1.9900.exe";
File file = new File(Environment.getExternalStorageDirectory(), fileName);
//确定下载地址
String path = "http://10.1.156.35:8080/" + fileName;
httpUtils.download(path,//下载地址
file.getPath(),//文件保存路径
true,//是否支持断点续传
true,//如果从请求返回信息中获取文件名字,下载完成后重新命名
new RequestCallBack<File>() {
//下载成功后调用
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
Toast.makeText(MainActivity.this, responseInfo.result.getPath(), Toast.LENGTH_SHORT).show();
}
//下载失败后调用
@Override
public void onFailure(HttpException e, String s) {
textView.setText(s);
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
progressBar.setMax((int) total);
progressBar.setProgress((int) current);
tv_progress.setText(current * 100 / total + "%");
}
}
);
}
实验结果:IOException:open failed,EACCES(Permission denied)
实验结论:在代码没问题前提下,如果清单文件权限配置到位,却仍然出现权限问题。下载无法继续
那么如何改正呢?
首先我们得明白权限种类:
1.PROTECTION_NORMAL类权限(正常)
当用户安装或者更新应用时,系统将授予应用所请求的属于Protection_normal的所有权限####(安装时授权的一类基本权限),在mainfest.xml中声明就可以。
权限分类参考了:http://blog.csdn.net/u013472738/article/details/52441268
2.PROTECTION_DANGEROUS 类权限(危险)
adb shell pm list permissions -d -g 这个命令可以进行查看。以下权限好像都是2个2个一组的。
比如:READ_PHONE_STATE WRITE_PHONE_STATE 就是一组,只要其中一个被授权了,另外一个你就不需要再进行申请授了。
如下
public void test2(View v) {
int hasWriteExternalStorage = ActivityCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
if(hasWriteExternalStorage != PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(this,
new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_EXTERNAL_STORAGE);
}else {
download();
}
}
public void download(){
HttpUtils httpUtils = new HttpUtils();
String fileName = "adsafese.v1.1.1.9900.exe";
File file = new File(Environment.getExternalStorageDirectory(), fileName);
//确定下载地址
String path = "http://10.1.156.35:8080/" + fileName;
httpUtils.download(path,//下载地址
file.getPath(),//文件保存路径
true,//是否支持断点续传
true,//如果从请求返回信息中获取文件名字,下载完成后重新命名
new RequestCallBack<File>() {
//下载成功后调用
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
Toast.makeText(MainActivity.this, responseInfo.result.getPath(), Toast.LENGTH_SHORT).show();
}
//下载失败后调用
@Override
public void onFailure(HttpException e, String s) {
textView.setText(s);
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
super.onLoading(total, current, isUploading);
progressBar.setMax((int) total);
progressBar.setProgress((int) current);
tv_progress.setText(current * 100 / total + "%");
}
}
);
}
/**处理权限申请回调
* @param requestCode 定位到我的申请
* @param permissions 申请时的权限字符串数组
* @param grantResults 对应于申请的结果
*/
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode){
case REQUEST_EXTERNAL_STORAGE:
if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
download();
}else { //permission denied
Toast.makeText(this,"WRITE_EXTERNAL_STORAGE Denied",Toast.LENGTH_SHORT).show();
}
break;
default: super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
实验结果: