Android6.0中新添加了運(yùn)行時(shí)權(quán)限,對(duì)于一些敏感的權(quán)限系統(tǒng)要求等用戶確認(rèn)授予權(quán)限后才可使用。如果沒(méi)做Android6.0的適配在Android6.0的設(shè)備上并且apk的目標(biāo)版本高于23,一些功能可能因?yàn)槲茨塬@取權(quán)限而崩潰。
運(yùn)行時(shí)權(quán)限官方文檔
下面以獲取打開(kāi)相冊(cè)權(quán)限為例,了解下運(yùn)行時(shí)權(quán)限的適配處理,需要處理的敏感權(quán)限信息同時(shí)也應(yīng)該在清單文件中聲明。
檢查是否已獲得相應(yīng)權(quán)限
if (ContextCompat.checkSelfPermission(view.getContext(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED)
未獲取相應(yīng)權(quán)限
如果還沒(méi)有獲取相應(yīng)權(quán)限通過(guò)下面的方法申請(qǐng)
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.CAMERA}, 100);
系統(tǒng)會(huì)彈窗對(duì)話框像用戶申請(qǐng)權(quán)限,用戶同意或拒絕都會(huì)在onRequestPermissionsResult
方法中回調(diào)
回調(diào)方法參數(shù)中會(huì)有請(qǐng)求碼,申請(qǐng)的權(quán)限數(shù)組和申請(qǐng)結(jié)果數(shù)組,如果對(duì)應(yīng)的返回結(jié)果是
PERMISSION_GRANTED
代表用戶同意了該請(qǐng)求。
Fragment中和Activity中請(qǐng)求獲取權(quán)限的方法稍有不同,上面的方法如果在Fragment中不能成功獲取回調(diào)。在Fragment中應(yīng)使用requestPermissions(MainActivity.this,new String[]{Manifest.permission.CAMERA}, 100);
if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1001);
}else {
Snackbar.make(getWindow().getDecorView(),"獲取權(quán)限被拒絕",Snackbar.LENGTH_SHORT)
.show();
}
此外系統(tǒng)提供了另外一個(gè)方法,當(dāng)你申請(qǐng)的權(quán)限被拒絕時(shí)可以向用戶接受獲取該權(quán)限的原因的api,但檢查沒(méi)有獲取到權(quán)限可以加層判斷解釋獲取的原因。
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CAMERA)) {
Snackbar.make(view,"請(qǐng)求獲取打開(kāi)相冊(cè)權(quán)限",Snackbar.LENGTH_SHORT)
.setAction("OK", new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA}, 100);
}
}).show();
}
已經(jīng)獲取相應(yīng)權(quán)限
如果已經(jīng)獲取了權(quán)限,則直接走打開(kāi)相冊(cè)的代碼邏輯
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1001);
獲取權(quán)限主要代碼如下
private void showCamera(View view) {
if (ContextCompat.checkSelfPermission(view.getContext(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
Log.i("whl", "請(qǐng)求獲取權(quán)限");
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, Manifest.permission.CAMERA)) {
Snackbar.make(view,"請(qǐng)求獲取打開(kāi)相冊(cè)權(quán)限",Snackbar.LENGTH_SHORT)
.setAction("OK", new View.OnClickListener() {
@Override
public void onClick(View v) {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA}, 100);
}
}).show();
} else {
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.CAMERA}, 100);
}
} else {
Log.i("whl", "獲取了相機(jī)權(quán)限");
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1001);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
Log.i("whl",permissions[0]+"----"+grantResults[0]);
if (requestCode==100){
if (grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1001);
}else {
Snackbar.make(getWindow().getDecorView(),"獲取權(quán)限被拒絕",Snackbar.LENGTH_SHORT)
.show();
}
}
}