Android動態許可權的處理
阿新 • • 發佈:2018-12-09
Android的動態許可權校驗早已不是什麼新鮮的東西了,現在遇到了一個需求就是必須強制使用者賦予應用一個許可權的需求。如果使用者不賦予該許可權,要麼就一直提醒使用者,要麼就退出應用。最開始以為這個需求很簡單,等到真正開始開發時才發現這裡面還有很多細節要考慮。
一、效果圖展示
1.1 啟動應用時申請許可權,出現如下效果圖
1.2 點選“禁止”之後,出現如下效果圖
1.3 點選“去允許”之後又會彈出1.1的效果圖
1.4 勾選“不再詢問”,出現如下效果圖
1.5 點選“禁止”之後,出現如下效果圖
1.6 點選“去開啟”之後,跳轉至應用的詳情頁面
二、原始碼分析
package com.study.yang.requestpermissiondemo;
import android.Manifest;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Resources;
import android.net.Uri;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;
public class FullscreenActivity extends AppCompatActivity {
private String[] rps = {Manifest.permission.READ_PHONE_STATE};
private static final int REQUEST_PERMISSION_SETTING = 200;
private AlertDialog permissionDialog;
private TextView tvOk;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == 100 && permissions.length > 0) {
boolean flag = true;
for (int i = 0; i < permissions.length; i++) {
flag &= (grantResults[i] == PackageManager.PERMISSION_GRANTED);
}
//如果許可權賦予失敗
if (!flag) {
//如果勾選且拒絕了許可權申請,則返回false
boolean permissionRationale = ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0]);
showPermissionDialog(!permissionRationale);
} else {
requestPermissionSuccess();
}
}
}
private void showPermissionDialog(final boolean isCheck) {
if (permissionDialog == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(this, android.R.style.Theme_Holo_Light_Dialog);
View inflate = LayoutInflater.from(this).inflate(R.layout.permission_dialog_layout, null);
permissionDialog = builder.create();
permissionDialog.show();
permissionDialog.setCancelable(false);
Window window = permissionDialog.getWindow();
//設定遮罩的亮度
// window.setDimAmount(0);
window.setContentView(inflate);
window.setBackgroundDrawableResource(android.R.color.transparent);
tvOk = (TextView) inflate.findViewById(R.id.tv_ok);
} else {
permissionDialog.show();
}
Resources resources = getResources();
tvOk.setText(isCheck ? resources.getString(R.string.permission_open) : resources.getString(R.string.permission_allow));
tvOk.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isCheck) {
openSettingActivity();
} else {
PermissionCheckUtils.checkActivityPermissions(FullscreenActivity.this, rps, 100, null);
}
permissionDialog.dismiss();
}
});
}
/**
* 關掉對話方塊
*/
private void dismissPermissionDialog() {
if (permissionDialog != null && permissionDialog.isShowing()) {
permissionDialog.dismiss();
}
}
/**
* 開啟應用詳情頁
*/
private void openSettingActivity() {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
}
/**
* 用來處理從應用詳情頁返回之後的接受結果
* @param requestCode
* @param resultCode
* @param data
*/
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_PERMISSION_SETTING) {
//返回當前頁的時候再次校驗許可權
int size = PermissionCheckUtils.checkActivityPermissions(this, rps, 100, null);
//未被授予則進行再次申請
if (size > 0) {
showPermissionDialog(!ActivityCompat.shouldShowRequestPermissionRationale(this, rps[0]));
} else {
//請求許可權成功
requestPermissionSuccess();
}
}
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
int permissionSize = PermissionCheckUtils.checkActivityPermissions(this, rps, 100, null);
if (permissionSize == 0) {
requestPermissionSuccess();
}
}
private void requestPermissionSuccess() {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("permission", "賦予許可權成功");
startActivity(intent);
}
}