自从6.0开始,SD卡的读写也需要获取运行时权限。我们应尽量避免去动态获取SD卡权限,而是将文件缓存在Android/data/应用包名路径下,此路径不需要获取权限,而且当应用删除时此路径也会删除
//在Android/data/应用包名/cache下,一般放一些不重要的数据,因为可能会被垃圾清理程序清除
File file=getExternalCacheDir();//
//重要的文件可以存在Android/data/应用包名/files下,一般放一些长时间保存的数据
getExternalFilesDir("");
//如果是getExternalFilesDir("abc") 则会在files下新建一个abc目录
下面以获取拨打电话的例子来讲解运行时权限
Intent intent=new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:13426112458"));
startActivity(intent);
记得在清单文件里添加<uses-permission android:name="android.permission.CALL_PHONE" />
- 当项目的gradle中,targetSdkVersion<23时,运行程序,不会请求运行时权限。
- 当我们把targetSdkVersion改为23或者是23以上再运行程序,发现还是没有请求运行时权限。这是因为应用升级时会把之前请求道的权限带到新升级到的版本。
一般写法
//判断 如果没有拨打电话的权限
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) !=PackageManager.PERMISSION_GRANTED)
{
//请求拨打电话的权限
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CALL_PHONE},101);
} else {
callPhone();
}
请求权限的回调
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case 101:
//判断是否获取到权限
if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
//请求到权限
callPhone();
}else{
Toast.makeText(MainActivity.this,"权限被拒绝",Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
}```
有一点需要注意:拨打电话的方法需要添加权限异常
``` private void callPhone() {
try {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:13426112458"));
startActivity(intent);
}catch (SecurityException e){//权限异常,因为有可能在任何地方在没有请求权限的情况下调用callPhone()方法
}
}
如果需要同时获取多个权限
List<String> permissionList = new ArrayList<String>();
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(Manifest.permission.CALL_PHONE);
}
//注意读写sd卡权限需要在清单文件里注册
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
permissionList.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
}
if (!permissionList.isEmpty()) {//permissionList不为空,说明有权限需要动态获取
//注意第二个参数如何将List转换为数组
ActivityCompat.requestPermissions(MainActivity.this, permissionList.toArray(new String[permissionList.size()]), 101);
} else {
doSomeThing();
}```
权限请求的回调
``` @Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case 101:
if (grantResults.length > 0) {
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(MainActivity.this, permissions[i] + "权限被拒绝", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, permissions[i] + "权限被允许", Toast.LENGTH_SHORT).show();
}
}
}
break;
default:
break;
}
}
封装运行时权限的获取
一般我们的应用中都会有一个BaseActivity,其他的Activity都要继承这个BaseActivity。我们将运行时权限的代码封装进BaseActivity
public class BaseActivity extends Activity {
PermissionListener mListener;
public void requestRuntimePermissions(String[] permissions, PermissionListener listener) {
mListener = listener;
List<String> listPermission = new ArrayList<>();
for (String permission : permissions) {
if (ActivityCompat.checkSelfPermission(this, permission) != PackageManager.PERMISSION_GRANTED) {
listPermission.add(permission);
}
}
if (listPermission.isEmpty()) {//所有权限都以获取
mListener.onSuccess();
} else {
ActivityCompat.requestPermissions(this, listPermission.toArray(new String[listPermission.size()]), 1);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && requestCode == 1) {
List<String> deniedPermissions = new ArrayList<>();//被拒绝的权限列表
for (int i = 0; i < grantResults.length; i++) {
if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {//被拒绝
deniedPermissions.add(permissions[i]);
}
}
if (deniedPermissions.isEmpty()) {//没有被拒绝的权限
mListener.onSuccess();
} else {
mListener.onFailed(deniedPermissions);
}
}
}
}```
接口很简单,就一个成功跟失败的回调,这里就不添加了
网友评论