Skip to content

Commit

Permalink
修复系统在重启应用后重新触发权限请求的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Sep 5, 2021
1 parent 508b178 commit e5e3864
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 9 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ android {
dependencies {
// 权限请求框架:https://github.com/getActivity/XXPermissions
implementation 'com.github.getActivity:XXPermissions:12.0'
implementation 'com.github.getActivity:XXPermissions:12.2'
}
```

Expand Down Expand Up @@ -151,13 +151,13 @@ public class XxxActivity extends AppCompatActivity {

| 功能及细节 | [XXPermissions](https://github.com/getActivity/XXPermissions) | [AndPermission](https://github.com/yanzhenjie/AndPermission) | [RxPermissions](https://github.com/tbruyelle/RxPermissions) | [PermissionsDispatcher](https://github.com/permissions-dispatcher/PermissionsDispatcher) | [EasyPermissions](https://github.com/googlesamples/easypermissions) | [PermissionX](https://github.com/guolindev/PermissionX) | [AndroidUtilCode](https://github.com/Blankj/AndroidUtilCode) |
| :--------: | :------------: | :------------: | :------------: | :------------: | :------------: | :------------: | :------------: |
| 对应版本 | 11.8 | 2.0.3 | 0.12 | 4.8.0 | 3.0.0 | 1.5.0 | 1.30.6 |
| 对应版本 | 12.2 | 2.0.3 | 0.12 | 4.8.0 | 3.0.0 | 1.5.0 | 1.30.6 |
| issues 数 | [![](https://img.shields.io/github/issues/getActivity/XXPermissions.svg)](https://github.com/getActivity/XXPermissions/issues) | [![](https://img.shields.io/github/issues/yanzhenjie/AndPermission.svg)](https://github.com/yanzhenjie/AndPermission/issues) | [![](https://img.shields.io/github/issues/tbruyelle/RxPermissions.svg)](https://github.com/tbruyelle/RxPermissions/issues) | [![](https://img.shields.io/github/issues/permissions-dispatcher/PermissionsDispatcher.svg)](https://github.com/permissions-dispatcher/PermissionsDispatcher/issues) | [![](https://img.shields.io/github/issues/googlesamples/easypermissions.svg)](https://github.com/googlesamples/easypermissions/issues) | [![](https://img.shields.io/github/issues/guolindev/PermissionX.svg)](https://github.com/guolindev/PermissionX/issues) | [![](https://img.shields.io/github/issues/Blankj/AndroidUtilCode.svg)](https://github.com/Blankj/AndroidUtilCode/issues) |
| 框架体积 | 27 KB | 127 KB | 28 KB | 91 KB | 48 KB | 66 KB | 500 KB |
| 安装包权限 ||||||||
| 悬浮窗权限 |||| ||||
| 悬浮窗权限 |||| ||||
| 通知栏权限 ||||||||
| 系统设置权限 |||| ||||
| 系统设置权限 |||| ||||
| Android 8.0 权限适配 ||||||||
| Android 9.0 权限适配 ||||||||
| Android 10.0 权限适配 ||||||||
Expand Down
Binary file modified XXPermissions.apk
Binary file not shown.
11 changes: 9 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.permissions.demo"
minSdkVersion 14
targetSdkVersion 30
versionCode 1200
versionName "12.0"
versionCode 1220
versionName "12.2"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down Expand Up @@ -41,6 +41,13 @@ android {
signingConfig signingConfigs.config
}
}

applicationVariants.all { variant ->
// apk 输出文件名配置
variant.outputs.all { output ->
outputFileName = rootProject.getName() + '.apk'
}
}
}

dependencies {
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ android {

defaultConfig {
minSdkVersion 11
versionCode 1200
versionName "12.0"
versionCode 1220
versionName "12.2"
}

// 使用 JDK 1.8
Expand Down
2 changes: 1 addition & 1 deletion library/src/main/java/com/hjq/permissions/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public final class Permission {
private Permission() {}

/**
* 外部存储权限(特殊权限,需要 Android 11 及以上)
* 文件管理权限(特殊权限,需要 Android 11 及以上)
*
* 如果你的应用需要上架 GooglePlay,那么需要详细查看:https://support.google.com/googleplay/android-developer/answer/9956427
*/
Expand Down
19 changes: 19 additions & 0 deletions library/src/main/java/com/hjq/permissions/PermissionFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ public static void beginRequest(Activity activity, ArrayList<String> permissions
fragment.setArguments(bundle);
// 设置保留实例,不会因为屏幕方向或配置变化而重新创建
fragment.setRetainInstance(true);
// 设置权限申请标记
fragment.setRequestFlag(true);
// 设置权限回调监听
fragment.setCallBack(callback);
// 设置权限请求拦截器
Expand All @@ -69,6 +71,9 @@ public static void beginRequest(Activity activity, ArrayList<String> permissions
/** 是否申请了危险权限 */
private boolean mDangerousRequest;

/** 权限申请标记 */
private boolean mRequestFlag;

/** 权限回调对象 */
private OnPermissionCallback mCallBack;

Expand Down Expand Up @@ -99,6 +104,13 @@ public void setCallBack(OnPermissionCallback callback) {
mCallBack = callback;
}

/**
* 权限申请标记(防止系统杀死应用后重新触发请求的问题)
*/
public void setRequestFlag(boolean flag) {
mRequestFlag = flag;
}

/**
* 设置权限请求拦截器
*/
Expand Down Expand Up @@ -155,6 +167,13 @@ public void onDestroy() {
@Override
public void onResume() {
super.onResume();

// 如果当前 Fragment 是通过系统重启应用触发的,则不进行权限申请
if (!mRequestFlag) {
detachActivity(getActivity());
return;
}

// 如果在 Activity 不可见的状态下添加 Fragment 并且去申请权限会导致授权对话框显示不出来
// 所以必须要在 Fragment 的 onResume 来申请权限,这样就可以保证应用回到前台的时候才去申请权限
if (mSpecialRequest) {
Expand Down

0 comments on commit e5e3864

Please sign in to comment.