封裝一個MPermissionsActivity的思路和步驟
阿新 • • 發佈:2017-11-14
miss .sh 調用 成功 cli osi settings 應用 str
http://blog.csdn.net/longkehuawei/article/details/53202804
第一步:檢測所有的權限是否都已授權
/**
* 檢測所有的權限是否都已授權
*
* @param permissions
* @return
*/
private boolean checkPermissions(String[] permissions) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
return true;
}
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(this, permission) !=
PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
第二步:獲取權限集中需要申請權限的列表
/**
* 獲取權限集中需要申請權限的列表
*
* @param permissions
* @return
*/
private List<String> getDeniedPermissions(String[] permissions) {
List<String> needRequestPermissionList = new ArrayList<>();
for (String permission : permissions) {
if (ContextCompat.checkSelfPermission(this, permission) !=
PackageManager.PERMISSION_GRANTED ||
ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {
needRequestPermissionList.add(permission);
}
}
return needRequestPermissionList;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
第三步:請求權限
/**
* 請求權限
*
* @param permissions 請求的權限
* @param requestCode 請求權限的請求碼
*/
public void requestPermission(String[] permissions, int requestCode) {
this.REQUEST_CODE_PERMISSION = requestCode;
if (checkPermissions(permissions)) {
permissionSuccess(REQUEST_CODE_PERMISSION);
} else {
List<String> needPermissions = getDeniedPermissions(permissions);
ActivityCompat.requestPermissions(this, needPermissions.toArray(new String[needPermissions.size()]), REQUEST_CODE_PERMISSION);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
第四步:處理權限請求回調
/**
* 系統請求權限回調
*
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == REQUEST_CODE_PERMISSION) {
if (verifyPermissions(grantResults)) {
permissionSuccess(REQUEST_CODE_PERMISSION);
} else {
permissionFail(REQUEST_CODE_PERMISSION);
showTipsDialog();
}
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
第五步:查看處理權限請求回調用戶是否已經授權
/**
* 確認所有的權限是否都已授權
*
* @param grantResults
* @return
*/
private boolean verifyPermissions(int[] grantResults) {
for (int grantResult : grantResults) {
if (grantResult != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
第六步:授權成功處理函數
/**
* 獲取權限成功
*
* @param requestCode
*/
public void permissionSuccess(int requestCode) {
Log.d(TAG, "獲取權限成功=" + requestCode);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
第七步:授權失敗處理函數與彈出用戶提示
/**
* 權限獲取失敗
* @param requestCode
*/
public void permissionFail(int requestCode) {
Log.d(TAG, "獲取權限失敗=" + requestCode);
}
/**
* 顯示提示對話框
*/
private void showTipsDialog() {
new AlertDialog.Builder(this)
.setTitle("提示信息")
.setMessage("當前應用缺少必要權限,該功能暫時無法使用。如若需要,請單擊【確定】按鈕前往設置中心進行權限授權。")
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
})
.setPositiveButton("確定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startAppSettings();
}
}).show();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
第八步:授權失敗給用戶提示後想再次開啟跳到設置app權限界面
/**
* 啟動當前應用設置頁面
*/
private void startAppSettings() {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
封裝一個MPermissionsActivity的思路和步驟