Android6.0許可權總結
阿新 • • 發佈:2019-01-14
/**
* Created by Leonidas on 2018/5/14.
* Version: V1.0
* Description:繼承了Activity,實現Android6.0的執行時許可權檢測,
* 需要進行執行時許可權檢測的Activity可以繼承這個類。
* Others: 暫無
* ReviseHistory(Author、Date、RevisePart): 暫無
*/
public class CheckPermissionsActivity extends AppCompatActivity {
/**
* 需要進行檢測的許可權陣列
*/
protected String[] needPermissions = {
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.READ_PHONE_STATE
};
//許可權請求碼,作為許可權請求的標誌
private static final int PERMISSIONS_REQUEST_CODE = 0;
/**
* 判斷是否需要檢測,防止不停的彈框
*/
private boolean isNeedCheck = true;
@Override
protected void onResume() {
super.onResume();
if (isSystemVersionLarge23()) {//判斷系統版本是否是Android6.0以上
if (isNeedCheck) {//如果是,則需要檢測
checkPermissions(needPermissions);
}
}
}
/**
* 判斷系統版本是否是Android6.0以上
* @return
*/
protected boolean isSystemVersionLarge23(){
if (Build.VERSION.SDK_INT >= 23 && getApplicationInfo().targetSdkVersion >= 23)
return true;
else
return false;
}
/**
* 檢查許可權
* @param permissions
*/
private void checkPermissions(String... permissions) {
try {
if (isSystemVersionLarge23()) {
/*需要請求的許可權*/
List<String> needRequestPermissions = findDeniedPermissions(permissions);
if (null != needRequestPermissions && needRequestPermissions.size() > 0) {
requestPermissions(needRequestPermissions.toArray(new String[needRequestPermissions.size()]),PERMISSIONS_REQUEST_CODE);
}
}
} catch (Throwable e) {
}
}
/**
* 獲取許可權集中需要申請許可權的列表
* @param Permissions
* @return
*
*/
private List<String> findDeniedPermissions(String[]Permissions) {
List<String> needRequestPermissionList = new ArrayList<String>();
if (isSystemVersionLarge23()){
try {
for (String perm : Permissions) {
if (checkSelfPermission(perm) != PackageManager.PERMISSION_GRANTED
||shouldShowRequestPermissionRationale(perm)) {//判斷許可權是否被授予
needRequestPermissionList.add(perm);
}
}
} catch (Throwable e) {
}
}
return needRequestPermissionList;
}
/**
* 檢測是否所有的許可權都已經授權
* @param grantResults 授權的結果
* @return
*/
private boolean verifyPermissions(int[] grantResults) {
for (int result : grantResults) {
if (result != PackageManager.PERMISSION_GRANTED) {
return false;
}
}
return true;
}
/**
*當進行許可權申請,
* 並且使用者做出選擇後會回撥onRequestPermissionsResult這個方法,
* 在這個方法中做相關處理。
* @param requestCode 請求碼---要與進行許可權申請的requestPermissions方法中的請求碼相同,作為許可權請求的標誌
* @param Permissions 許可權
* @param grantResults 許可權的“授權結果”
*/
@TargetApi(23)
public void onRequestPermissionsResult(int requestCode, String[] Permissions, int[] grantResults) {
if (requestCode == PERMISSIONS_REQUEST_CODE) {//許可權請求碼
if (!verifyPermissions(grantResults)) {//判斷是否所有需要被授權的許可權都被授權了
showMissingPermissionDialog();//有其中一個許可權沒被授權,顯示許可權丟失的對話方塊
isNeedCheck = false;//不需要再進行檢查了
}
}
}
/**
* 顯示許可權丟失的對話方塊
*/
private void showMissingPermissionDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.notifyTitle);
builder.setMessage("當前應用缺少必要許可權,\n會導致部分功能使用異常。\n\n請點選\"設定\"-\"許可權\"-開啟所需許可權。\n\n");
// 拒絕, 退出應用
builder.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.setPositiveButton(R.string.setting,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startAppSettings();//呼叫系統的App設定功能
}
});
builder.setCancelable(false);
builder.show();
}
/**
* 啟動應用的設定
*/
private void startAppSettings() {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
intent.setData(Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_BACK){//當按下鍵盤的回退按鈕則退出Activity
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
參考:
1.https://www.jianshu.com/p/e1ab1a179fbb
2.http://www.cnblogs.com/zqlxtt/p/4873831.html
3.https://www.jianshu.com/p/b4a8b3d4f587
4.高德地圖Demo原始碼