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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
| public class CheckPermission extends Activity {
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 PERMISSON_REQUESTCODE = 0;
private boolean isNeedCheck = true;
@Override protected void onResume() { super.onResume(); if (Build.VERSION.SDK_INT >= 23 && getApplicationInfo().targetSdkVersion >= 23) { if (isNeedCheck) { checkPermissions(needPermissions); } } }
private void checkPermissions(String... permissions) { try { if (Build.VERSION.SDK_INT >= 23 && getApplicationInfo().targetSdkVersion >= 23) { List<String> needRequestPermissonList = findDeniedPermissions(permissions); if (null != needRequestPermissonList && needRequestPermissonList.size() > 0) { String[] array = needRequestPermissonList.toArray(new String[needRequestPermissonList.size()]); Method method = getClass().getMethod("requestPermissions", new Class[]{String[].class,int.class});
method.invoke(this, array, PERMISSON_REQUESTCODE); } } } catch (Throwable e) { } }
private List<String> findDeniedPermissions(String[] permissions) { List<String> needRequestPermissonList = new ArrayList<String>(); if (Build.VERSION.SDK_INT >= 23 && getApplicationInfo().targetSdkVersion >= 23) { try { for (String perm : permissions) { Method checkSelfMethod = getClass().getMethod("checkSelfPermission", String.class); Method shouldShowRequestPermissionRationaleMethod = getClass().getMethod("shouldShowRequestPermissionRationale", String.class); if ((Integer) checkSelfMethod.invoke(this, perm) != PackageManager.PERMISSION_GRANTED || (Boolean) shouldShowRequestPermissionRationaleMethod.invoke(this, perm)) { needRequestPermissonList.add(perm); } } } catch (Throwable e) {
} } return needRequestPermissonList; }
private boolean verifyPermissions(int[] grantResults) { for (int result : grantResults) { if (result != PackageManager.PERMISSION_GRANTED) { return false; } } return true; }
@TargetApi(23) public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] paramArrayOfInt) { if (requestCode == PERMISSON_REQUESTCODE) { if (!verifyPermissions(paramArrayOfInt)) { showMissingPermissionDialog(); isNeedCheck = false; } } }
private void showMissingPermissionDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("需要您授予下面权限:"); builder.setMessage("当前应用缺少必要权限。\\n\\n请点击\\\"设置\\\"-\\\"权限\\\"-打开所需权限。"); builder.setNegativeButton("拒绝", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { finish(); } }); builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { startAppSettings(); } });
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) { this.finish(); return true; } return super.onKeyDown(keyCode, event); }
|