在6.0下新的權(quán)限機(jī)制分為兩類螺男,普通權(quán)限繁莹,這類權(quán)限是不需要用戶授權(quán)页屠,不會(huì)觸及用戶的隱私粹胯,只需要在manefest里面申請(qǐng)即可;還有一類是危險(xiǎn)許可,這類權(quán)限需要?jiǎng)討B(tài)的去申請(qǐng)
官方文檔:
https://github.com/googlesamples/easypermissions
運(yùn)行時(shí)權(quán)限官方文檔解釋:
https://developer.android.com/training/permissions/requesting.html
2為什么使用Easypermissions
Android M對(duì)于權(quán)限系統(tǒng)進(jìn)行了改變.Android M之前所需權(quán)限只需要在AndroidManifest中申請(qǐng)即可辰企。但更新Android M系統(tǒng)后风纠,部分“危險(xiǎn)許可”需要在運(yùn)行時(shí)詢問申請(qǐng)。
正常和危險(xiǎn)權(quán)限解釋:
https://developer.android.com/guide/topics/security/permissions.html#normal-dangerous
如下圖所示:
舉個(gè)栗子:
下面的栗子是常用的獲取用戶的寫外部存儲(chǔ)器和打電話的權(quán)限
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE)
!= PackageManager.PERMISSION_GRANTED) {//未獲取權(quán)限
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CALL_PHONE)) {
//當(dāng)用戶第一次申請(qǐng)拒絕時(shí)牢贸,再次申請(qǐng)?jiān)摍?quán)限調(diào)用
Toast.makeText(this, "撥打電話權(quán)限", Toast.LENGTH_SHORT).show();
}
//申請(qǐng)權(quán)限
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CALL_PHONE}, 0x01);
} else {//已經(jīng)獲得了權(quán)限
call();
}
}
private void call() {
Toast.makeText(this, "打電話", Toast.LENGTH_SHORT).show();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 0x01) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {//授權(quán)成功
call();
} else {//授權(quán)失敗
Toast.makeText(this, "獲取權(quán)限失敗", Toast.LENGTH_SHORT).show();
}
}
}
運(yùn)行時(shí)權(quán)限申請(qǐng)
代碼中如果需要申請(qǐng)運(yùn)行時(shí)權(quán)限步驟:
1檢查運(yùn)行時(shí)權(quán)限
//所要申請(qǐng)的權(quán)限
String[] perms = {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CALL_PHONE};
if (EasyPermissions.hasPermissions(this, perms)) {//檢查是否獲取該權(quán)限
Log.i(TAG, "已獲取權(quán)限");
} else {
.....
}
2申請(qǐng)權(quán)限
//第二個(gè)參數(shù)是被拒絕后再次申請(qǐng)?jiān)摍?quán)限的解釋
//第三個(gè)參數(shù)是請(qǐng)求碼
//第四個(gè)參數(shù)是要申請(qǐng)的權(quán)限
EasyPermissions.requestPermissions(this, "必要的權(quán)限", 0, perms);
3接收權(quán)限處理結(jié)果
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
//把申請(qǐng)權(quán)限的回調(diào)交由EasyPermissions處理
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
//下面兩個(gè)方法是實(shí)現(xiàn)EasyPermissions的EasyPermissions.PermissionCallbacks接口
//分別返回授權(quán)成功和失敗的權(quán)限
@Override
public void onPermissionsGranted(int requestCode, List<String> perms) {
Log.i(TAG, "獲取成功的權(quán)限" + perms);
}
@Override
public void onPermissionsDenied(int requestCode, List<String> perms) {
Log.i(TAG, "獲取失敗的權(quán)限" + perms);
}
以上即是google建議的運(yùn)行時(shí)權(quán)限處理過程竹观。代碼量和處理地方還是比較繁瑣的.so,Easypermissions應(yīng)運(yùn)而生潜索。
3如何使用
首先臭增,引用庫
dependencies {
compile 'pub.devrel:easypermissions:0.1.9'
}
同樣,easypermissions處理權(quán)限分為3步竹习。
1檢查權(quán)限
String[] perms = {Manifest.permission.CAMERA, Manifest.permission.CHANGE_WIFI_STATE};
if (EasyPermissions.hasPermissions(this, perms)) {
//...
} else {
//...
}
2申請(qǐng)權(quán)限
EasyPermissions.requestPermissions(this, "拍照需要攝像頭權(quán)限",
RC_CAMERA_AND_WIFI, perms);
3實(shí)現(xiàn)EasyPermissions.PermissionCallbacks接口誊抛,直接處理權(quán)限是否成功申請(qǐng)
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
// Forward results to EasyPermissions
EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
}
//成功
@Override
public void onPermissionsGranted(int requestCode, List<String> list) {
// Some permissions have been granted
// ...
}
//失敗
@Override
public void onPermissionsDenied(int requestCode, List<String> list) {
// Some permissions have been denied
// ...
}
簡(jiǎn)易主要簡(jiǎn)化了對(duì)權(quán)限申請(qǐng)結(jié)果的處理和判斷,直接以接口的方式回調(diào)處理結(jié)果整陌。不需要再自行進(jìn)行處理芍锚。