此版本引入了一種新的權(quán)限模式儿奶,如今框往,用戶可直接在運行時管理應用權(quán)限。這種模式讓用戶能夠更好地了解和控制權(quán)限闯捎,同時為應用開發(fā)者精簡了安裝和自動更新過程椰弊。用戶可為所安裝的各個應用分別授予或撤銷權(quán)限。
對于以 Android 6.0(API 級別 23)或更高版本為目標平臺的應用瓤鼻,請務必在運行時檢查和請求權(quán)限秉版。要確定您的應用是否已被授予權(quán)限,請調(diào)用新增的 checkSelfPermission() 方法茬祷。要請求權(quán)限清焕,請調(diào)用新增的 requestPermissions() 方法。即使您的應用并不以 Android 6.0(API 級別 23)為目標平臺,您也應該在新權(quán)限模式下測試您的應用秸妥。
偷懶的方法:
工程目錄下-->app/buld.gradle --> targetSdkVersion 22 目標版本<23 指向Android 5.0 這樣系統(tǒng)就
不會檢測運行時權(quán)限 但畢竟這是偷懶的方法 說不定那天Google 大大不支持這種兼容性的套路 不就傻眼了 所以
該適配還是要適配滴
-
checkSelfPermission(@NonNull Context context, @NonNull String permission) //確定您的應用是否已被授予權(quán)限
// 判斷權(quán)限集合 public boolean lacksPermissions(String... permissions) { for (String permission : permissions) { if (lacksPermission(permission)) { return true; } } return false; } // 判斷是否缺少權(quán)限 private boolean lacksPermission(String permission) { return ContextCompat.checkSelfPermission(mContext, permission) == PackageManager.PERMISSION_DENIED; }
requestPermissions(@NonNull String[] permissions, int requestCode) //請求權(quán)限
onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,@NonNull int[] grantResults) //請求權(quán)限的回調(diào)
判斷權(quán)限是否授予:
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;
}
/**
* 傳入權(quán)限集合
* 6.0權(quán)限集合如果有權(quán)限申請通過 權(quán)限集合權(quán)限默認全部允許
* @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);
}
調(diào)用:
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