此版本引入了一种新的权限模式,如今,用户可直接在运行时管理应用权限。这种模式让用户能够更好地了解和控制权限,同时为应用开发者精简了安装和自动更新过程。用户可为所安装的各个应用分别授予或撤销权限。
对于以 Android 6.0(API 级别 23)或更高版本为目标平台的应用,请务必在运行时检查和请求权限。要确定您的应用是否已被授予权限,请调用新增的 checkSelfPermission() 方法。要请求权限,请调用新增的 requestPermissions() 方法。即使您的应用并不以 Android 6.0(API 级别 23)为目标平台,您也应该在新权限模式下测试您的应用。
偷懒的方法:
工程目录下-->app/buld.gradle --> targetSdkVersion 22 目标版本<23 指向Android 5.0 这样系统就
不会检测运行时权限 但毕竟这是偷懒的方法 说不定那天Google 大大不支持这种兼容性的套路 不就傻眼了 所以
该适配还是要适配滴
-
checkSelfPermission(@NonNull Context context, @NonNull String permission) //确定您的应用是否已被授予权限
// 判断权限集合 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; }
-
requestPermissions(@NonNull String[] permissions, int requestCode) //请求权限
-
onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults) //请求权限的回调
判断权限是否授予:
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
/**
* File: LacksPermissionUtils.java
*/
public class LacksPermissionUtils {
private static LacksPermissionUtils lacksPermissionUtils;
private Context mContext;
public static LacksPermissionUtils getInstance() {
synchronized (LacksPermissionUtils.class) {
if (lacksPermissionUtils == null) {
lacksPermissionUtils = new LacksPermissionUtils();
}
}
return lacksPermissionUtils;
}
public LacksPermissionUtils init(@NonNull Context context) {
this.mContext = context.getApplicationContext();
return lacksPermissionUtils;
}
/**
* 传入权限集合
* 6.0权限集合如果有权限申请通过 权限集合权限默认全部允许
* @param str
* @return
*/
public boolean lacksPermissions(String... str) {
if (str != null && str.length > 0) {
for (int i = 0, size = str.length; i < size; i++) {
if (!lacksPermission(str[i])) {
return true;
}
}
return false;
} else
return true;
}
private boolean lacksPermission(String s) {
return ContextCompat.checkSelfPermission(mContext, s)
== PackageManager.PERMISSION_DENIED;
}
}
公共方法:
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
/**
* File: BaseActivity.java
*/
public abstract class BaseActivity extends AppCompatActivity {
private static final int REQUEST_CODE_TYPE = 100;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
boolean isLacks = LacksPermissionUtils.getInstance()
.init(this.getApplicationContext())
.lacksPermissions(getPremissions());
if (!isLacks) {
requestPermissions(getPremissions(), 100);
}
}
}
protected abstract String[] getPremissions();
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CODE_TYPE:
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted
permissionsGranted(true);
} else {
// Permission Denied
Toast.makeText(this, "READ_PERMISSION_STORAGE Denied", Toast.LENGTH_SHORT)
.show();
permissionsGranted(false);
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
break;
}
}
protected abstract void permissionsGranted(boolean granted);
}
调用:
import android.Manifest;
import android.os.Bundle;
public class MainActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected String[] getPremissions() {
return new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE};
}
@Override
protected void permissionsGranted(boolean granted) {
}
}
参考:
http://blog.csdn.net/caroline_wendy/article/details/50587230
网友评论