Commit 24cc7626bd3dcab685b9ccfc5f1249ec28f829ba

Authored by 谷俊
1 parent 7578ccb6

权限工具修改

app/src/main/java/cn/vlion/simple/util/PermissionHelper.java
@@ -13,46 +13,25 @@ import android.support.v4.content.ContextCompat; @@ -13,46 +13,25 @@ import android.support.v4.content.ContextCompat;
13 import android.util.Log; 13 import android.util.Log;
14 14
15 /** 15 /**
16 - * Android 6.0 上权限分为<b>正常</b>和<b>危险</b>级别  
17 - * <ul>  
18 - * <li>正常级别权限:开发者仅仅需要在AndroidManifext.xml上声明,那么应用就会被允许拥有该权限,如:android.permission.INTERNET</li>  
19 - * <li>危险级别权限:开发者需要在AndroidManifext.xml上声明,并且在运行时进行申请,而且用户允许了,应用才会被允许拥有该权限,如:android.permission.WRITE_EXTERNAL_STORAGE</li>  
20 - * </ul>  
21 - * 有米的以下权限需要在Android6.0上被允许,有米广告sdk才能正常工作,开发者需要在调用有米的任何代码之前,提前让用户允许权限  
22 - * <ul>  
23 - * <li>必须申请的权限  
24 - * <ul>  
25 - * <li>android.permission.READ_PHONE_STATE</li>  
26 - * <li>android.permission.WRITE_EXTERNAL_STORAGE</li>  
27 - * </ul>  
28 - * </li>  
29 - * <li>可选申请的权限  
30 - * <ul>  
31 - * <li>android.permission.ACCESS_FINE_LOCATION</li>  
32 - * </ul>  
33 - * </li>  
34 - * </ul>  
35 - * Android 6.0+ 权限申请助手  
36 - *  
37 - * @since 2016-01-12 16 + * sdk所需权限工具类
38 */ 17 */
39 public class PermissionHelper { 18 public class PermissionHelper {
40 - 19 +
41 private static final String TAG = "PermissionHelper"; 20 private static final String TAG = "PermissionHelper";
42 - 21 +
43 /** 22 /**
44 * 小tips:这里的int数值不能太大,否则不会弹出请求权限提示,测试的时候,改到1000就不会弹出请求了 23 * 小tips:这里的int数值不能太大,否则不会弹出请求权限提示,测试的时候,改到1000就不会弹出请求了
45 */ 24 */
46 private final static int READ_PHONE_STATE_CODE = 101; 25 private final static int READ_PHONE_STATE_CODE = 101;
47 - 26 +
48 private final static int WRITE_EXTERNAL_STORAGE_CODE = 102; 27 private final static int WRITE_EXTERNAL_STORAGE_CODE = 102;
49 - 28 +
50 private final static int REQUEST_OPEN_APPLICATION_SETTINGS_CODE = 12345; 29 private final static int REQUEST_OPEN_APPLICATION_SETTINGS_CODE = 12345;
51 - 30 +
52 /** 31 /**
53 - * 有米 Android SDK 所需要向用户申请的权限列表 32 + * Android SDK 所需要向用户申请的权限列表
54 */ 33 */
55 - private PermissionModel[] mPermissionModels = new PermissionModel[] { 34 + private PermissionModel[] mPermissionModels = new PermissionModel[]{
56 new PermissionModel("电话", Manifest.permission.READ_PHONE_STATE, "我们需要读取手机信息的权限来标识您的身份", READ_PHONE_STATE_CODE), 35 new PermissionModel("电话", Manifest.permission.READ_PHONE_STATE, "我们需要读取手机信息的权限来标识您的身份", READ_PHONE_STATE_CODE),
57 new PermissionModel( 36 new PermissionModel(
58 "存储空间", 37 "存储空间",
@@ -61,19 +40,19 @@ public class PermissionHelper { @@ -61,19 +40,19 @@ public class PermissionHelper {
61 WRITE_EXTERNAL_STORAGE_CODE 40 WRITE_EXTERNAL_STORAGE_CODE
62 ) 41 )
63 }; 42 };
64 - 43 +
65 private Activity mActivity; 44 private Activity mActivity;
66 - 45 +
67 private OnApplyPermissionListener mOnApplyPermissionListener; 46 private OnApplyPermissionListener mOnApplyPermissionListener;
68 - 47 +
69 public PermissionHelper(Activity activity) { 48 public PermissionHelper(Activity activity) {
70 mActivity = activity; 49 mActivity = activity;
71 } 50 }
72 - 51 +
73 public void setOnApplyPermissionListener(OnApplyPermissionListener onApplyPermissionListener) { 52 public void setOnApplyPermissionListener(OnApplyPermissionListener onApplyPermissionListener) {
74 mOnApplyPermissionListener = onApplyPermissionListener; 53 mOnApplyPermissionListener = onApplyPermissionListener;
75 } 54 }
76 - 55 +
77 /** 56 /**
78 * 这里我们演示如何在Android 6.0+上运行时申请权限 57 * 这里我们演示如何在Android 6.0+上运行时申请权限
79 */ 58 */
@@ -81,7 +60,7 @@ public class PermissionHelper { @@ -81,7 +60,7 @@ public class PermissionHelper {
81 try { 60 try {
82 for (final PermissionModel model : mPermissionModels) { 61 for (final PermissionModel model : mPermissionModels) {
83 if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(mActivity, model.permission)) { 62 if (PackageManager.PERMISSION_GRANTED != ContextCompat.checkSelfPermission(mActivity, model.permission)) {
84 - ActivityCompat.requestPermissions(mActivity, new String[] { model.permission }, model.requestCode); 63 + ActivityCompat.requestPermissions(mActivity, new String[]{model.permission}, model.requestCode);
85 return; 64 return;
86 } 65 }
87 } 66 }
@@ -92,7 +71,7 @@ public class PermissionHelper { @@ -92,7 +71,7 @@ public class PermissionHelper {
92 Log.e(TAG, "", e); 71 Log.e(TAG, "", e);
93 } 72 }
94 } 73 }
95 - 74 +
96 /** 75 /**
97 * 对应Activity的 {@code onRequestPermissionsResult(...)} 方法 76 * 对应Activity的 {@code onRequestPermissionsResult(...)} 方法
98 * 77 *
@@ -102,85 +81,85 @@ public class PermissionHelper { @@ -102,85 +81,85 @@ public class PermissionHelper {
102 */ 81 */
103 public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 82 public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
104 switch (requestCode) { 83 switch (requestCode) {
105 - case READ_PHONE_STATE_CODE:  
106 - case WRITE_EXTERNAL_STORAGE_CODE:  
107 - // 如果用户不允许,我们视情况发起二次请求或者引导用户到应用页面手动打开  
108 - if (PackageManager.PERMISSION_GRANTED != grantResults[0]) {  
109 -  
110 - // 二次请求,表现为:以前请求过这个权限,但是用户拒接了  
111 - // 在二次请求的时候,会有一个“不再提示的”checkbox  
112 - // 因此这里需要给用户解释一下我们为什么需要这个权限,否则用户可能会永久不在激活这个申请  
113 - // 方便用户理解我们为什么需要这个权限  
114 - if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, permissions[0])) {  
115 - AlertDialog.Builder builder = new AlertDialog.Builder(mActivity).setTitle("权限申请")  
116 - .setMessage(findPermissionExplain  
117 - (permissions[0])).setPositiveButton(  
118 - "确定",  
119 - new DialogInterface.OnClickListener  
120 - () {  
121 -  
122 - @Override  
123 - public void onClick(  
124 - DialogInterface dialog,  
125 - int which) {  
126 - applyPermissions();  
127 - }  
128 - }  
129 - );  
130 - builder.setCancelable(false);  
131 - builder.show();  
132 - }  
133 - // 到这里就表示已经是第3+次请求,而且此时用户已经永久拒绝了,这个时候,我们引导用户到应用权限页面,让用户自己手动打开  
134 - else {  
135 - AlertDialog.Builder builder = new AlertDialog.Builder(mActivity).setTitle("权限申请")  
136 - .setMessage("请在打开的窗口的权限中开启" +  
137 - findPermissionName  
138 - (permissions[0]) +  
139 - "权限,以正常使用本应用")  
140 - .setPositiveButton(  
141 - "去设置",  
142 - new DialogInterface.OnClickListener  
143 - () {  
144 -  
145 - @Override  
146 - public void onClick(  
147 - DialogInterface dialog,  
148 - int which) {  
149 - openApplicationSettings(  
150 - REQUEST_OPEN_APPLICATION_SETTINGS_CODE);  
151 - }  
152 - }  
153 - )  
154 - .setNegativeButton(  
155 - "取消",  
156 - new DialogInterface.OnClickListener  
157 - () {  
158 - @Override  
159 - public void onClick(  
160 - DialogInterface dialog,  
161 - int which) {  
162 - mActivity.finish();  
163 - }  
164 - }  
165 - );  
166 - builder.setCancelable(false);  
167 - builder.show(); 84 + case READ_PHONE_STATE_CODE:
  85 + case WRITE_EXTERNAL_STORAGE_CODE:
  86 + // 如果用户不允许,我们视情况发起二次请求或者引导用户到应用页面手动打开
  87 + if (PackageManager.PERMISSION_GRANTED != grantResults[0]) {
  88 +
  89 + // 二次请求,表现为:以前请求过这个权限,但是用户拒接了
  90 + // 在二次请求的时候,会有一个“不再提示的”checkbox
  91 + // 因此这里需要给用户解释一下我们为什么需要这个权限,否则用户可能会永久不在激活这个申请
  92 + // 方便用户理解我们为什么需要这个权限
  93 + if (ActivityCompat.shouldShowRequestPermissionRationale(mActivity, permissions[0])) {
  94 + AlertDialog.Builder builder = new AlertDialog.Builder(mActivity).setTitle("权限申请")
  95 + .setMessage(findPermissionExplain
  96 + (permissions[0])).setPositiveButton(
  97 + "确定",
  98 + new DialogInterface.OnClickListener
  99 + () {
  100 +
  101 + @Override
  102 + public void onClick(
  103 + DialogInterface dialog,
  104 + int which) {
  105 + applyPermissions();
  106 + }
  107 + }
  108 + );
  109 + builder.setCancelable(false);
  110 + builder.show();
  111 + }
  112 + // 到这里就表示已经是第3+次请求,而且此时用户已经永久拒绝了,这个时候,我们引导用户到应用权限页面,让用户自己手动打开
  113 + else {
  114 + AlertDialog.Builder builder = new AlertDialog.Builder(mActivity).setTitle("权限申请")
  115 + .setMessage("请在打开的窗口的权限中开启" +
  116 + findPermissionName
  117 + (permissions[0]) +
  118 + "权限,以正常使用本应用")
  119 + .setPositiveButton(
  120 + "去设置",
  121 + new DialogInterface.OnClickListener
  122 + () {
  123 +
  124 + @Override
  125 + public void onClick(
  126 + DialogInterface dialog,
  127 + int which) {
  128 + openApplicationSettings(
  129 + REQUEST_OPEN_APPLICATION_SETTINGS_CODE);
  130 + }
  131 + }
  132 + )
  133 + .setNegativeButton(
  134 + "取消",
  135 + new DialogInterface.OnClickListener
  136 + () {
  137 + @Override
  138 + public void onClick(
  139 + DialogInterface dialog,
  140 + int which) {
  141 + mActivity.finish();
  142 + }
  143 + }
  144 + );
  145 + builder.setCancelable(false);
  146 + builder.show();
  147 + }
  148 + return;
168 } 149 }
169 - return;  
170 - }  
171 -  
172 - // 到这里就表示用户允许了本次请求,我们继续检查是否还有待申请的权限没有申请  
173 - if (isAllRequestedPermissionGranted()) {  
174 - if (mOnApplyPermissionListener != null) {  
175 - mOnApplyPermissionListener.onAfterApplyAllPermission(); 150 +
  151 + // 到这里就表示用户允许了本次请求,我们继续检查是否还有待申请的权限没有申请
  152 + if (isAllRequestedPermissionGranted()) {
  153 + if (mOnApplyPermissionListener != null) {
  154 + mOnApplyPermissionListener.onAfterApplyAllPermission();
  155 + }
  156 + } else {
  157 + applyPermissions();
176 } 158 }
177 - } else {  
178 - applyPermissions();  
179 - }  
180 - break; 159 + break;
181 } 160 }
182 } 161 }
183 - 162 +
184 /** 163 /**
185 * 对应Activity的 {@code onActivityResult(...)} 方法 164 * 对应Activity的 {@code onActivityResult(...)} 方法
186 * 165 *
@@ -190,18 +169,18 @@ public class PermissionHelper { @@ -190,18 +169,18 @@ public class PermissionHelper {
190 */ 169 */
191 public void onActivityResult(int requestCode, int resultCode, Intent data) { 170 public void onActivityResult(int requestCode, int resultCode, Intent data) {
192 switch (requestCode) { 171 switch (requestCode) {
193 - case REQUEST_OPEN_APPLICATION_SETTINGS_CODE:  
194 - if (isAllRequestedPermissionGranted()) {  
195 - if (mOnApplyPermissionListener != null) {  
196 - mOnApplyPermissionListener.onAfterApplyAllPermission(); 172 + case REQUEST_OPEN_APPLICATION_SETTINGS_CODE:
  173 + if (isAllRequestedPermissionGranted()) {
  174 + if (mOnApplyPermissionListener != null) {
  175 + mOnApplyPermissionListener.onAfterApplyAllPermission();
  176 + }
  177 + } else {
  178 + mActivity.finish();
197 } 179 }
198 - } else {  
199 - mActivity.finish();  
200 - }  
201 - break; 180 + break;
202 } 181 }
203 } 182 }
204 - 183 +
205 /** 184 /**
206 * 判断是否所有的权限都被授权了 185 * 判断是否所有的权限都被授权了
207 * 186 *
@@ -215,12 +194,11 @@ public class PermissionHelper { @@ -215,12 +194,11 @@ public class PermissionHelper {
215 } 194 }
216 return true; 195 return true;
217 } 196 }
218 - 197 +
219 /** 198 /**
220 * 打开应用设置界面 199 * 打开应用设置界面
221 * 200 *
222 * @param requestCode 请求码 201 * @param requestCode 请求码
223 - *  
224 * @return 202 * @return
225 */ 203 */
226 private boolean openApplicationSettings(int requestCode) { 204 private boolean openApplicationSettings(int requestCode) {
@@ -228,7 +206,7 @@ public class PermissionHelper { @@ -228,7 +206,7 @@ public class PermissionHelper {
228 Intent intent = 206 Intent intent =
229 new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + mActivity.getPackageName())); 207 new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + mActivity.getPackageName()));
230 intent.addCategory(Intent.CATEGORY_DEFAULT); 208 intent.addCategory(Intent.CATEGORY_DEFAULT);
231 - 209 +
232 // Android L 之后Activity的启动模式发生了一些变化 210 // Android L 之后Activity的启动模式发生了一些变化
233 // 如果用了下面的 Intent.FLAG_ACTIVITY_NEW_TASK ,并且是 startActivityForResult 211 // 如果用了下面的 Intent.FLAG_ACTIVITY_NEW_TASK ,并且是 startActivityForResult
234 // 那么会在打开新的activity的时候就会立即回调 onActivityResult 212 // 那么会在打开新的activity的时候就会立即回调 onActivityResult
@@ -240,12 +218,11 @@ public class PermissionHelper { @@ -240,12 +218,11 @@ public class PermissionHelper {
240 } 218 }
241 return false; 219 return false;
242 } 220 }
243 - 221 +
244 /** 222 /**
245 * 查找申请权限的解释短语 223 * 查找申请权限的解释短语
246 * 224 *
247 * @param permission 权限 225 * @param permission 权限
248 - *  
249 * @return 226 * @return
250 */ 227 */
251 private String findPermissionExplain(String permission) { 228 private String findPermissionExplain(String permission) {
@@ -258,12 +235,11 @@ public class PermissionHelper { @@ -258,12 +235,11 @@ public class PermissionHelper {
258 } 235 }
259 return null; 236 return null;
260 } 237 }
261 - 238 +
262 /** 239 /**
263 * 查找申请权限的名称 240 * 查找申请权限的名称
264 * 241 *
265 * @param permission 权限 242 * @param permission 权限
266 - *  
267 * @return 243 * @return
268 */ 244 */
269 private String findPermissionName(String permission) { 245 private String findPermissionName(String permission) {
@@ -276,29 +252,29 @@ public class PermissionHelper { @@ -276,29 +252,29 @@ public class PermissionHelper {
276 } 252 }
277 return null; 253 return null;
278 } 254 }
279 - 255 +
280 private static class PermissionModel { 256 private static class PermissionModel {
281 - 257 +
282 /** 258 /**
283 * 权限名称 259 * 权限名称
284 */ 260 */
285 public String name; 261 public String name;
286 - 262 +
287 /** 263 /**
288 * 请求的权限 264 * 请求的权限
289 */ 265 */
290 public String permission; 266 public String permission;
291 - 267 +
292 /** 268 /**
293 * 解析为什么请求这个权限 269 * 解析为什么请求这个权限
294 */ 270 */
295 public String explain; 271 public String explain;
296 - 272 +
297 /** 273 /**
298 * 请求代码 274 * 请求代码
299 */ 275 */
300 public int requestCode; 276 public int requestCode;
301 - 277 +
302 public PermissionModel(String name, String permission, String explain, int requestCode) { 278 public PermissionModel(String name, String permission, String explain, int requestCode) {
303 this.name = name; 279 this.name = name;
304 this.permission = permission; 280 this.permission = permission;
@@ -306,16 +282,16 @@ public class PermissionHelper { @@ -306,16 +282,16 @@ public class PermissionHelper {
306 this.requestCode = requestCode; 282 this.requestCode = requestCode;
307 } 283 }
308 } 284 }
309 - 285 +
310 /** 286 /**
311 * 权限申请事件监听 287 * 权限申请事件监听
312 */ 288 */
313 public interface OnApplyPermissionListener { 289 public interface OnApplyPermissionListener {
314 - 290 +
315 /** 291 /**
316 * 申请所有权限之后的逻辑 292 * 申请所有权限之后的逻辑
317 */ 293 */
318 void onAfterApplyAllPermission(); 294 void onAfterApplyAllPermission();
319 } 295 }
320 - 296 +
321 } 297 }