Skip to content

Commit

Permalink
适配 Android 12 权限
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Oct 12, 2021
1 parent 02dca7a commit 37884a7
Show file tree
Hide file tree
Showing 14 changed files with 253 additions and 69 deletions.
18 changes: 9 additions & 9 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.3'
implementation 'com.github.getActivity:XXPermissions:12.5'
}
```

Expand Down Expand Up @@ -175,21 +175,21 @@ XXPermissions.startPermissionActivity(Fragment fragment, String... permissions);

| 功能及细节 | [XXPermissions](https://github.com/getActivity/XXPermissions) | [AndPermission](https://github.com/yanzhenjie/AndPermission) | [PermissionX](https://github.com/guolindev/PermissionX) | [AndroidUtilCode](https://github.com/Blankj/AndroidUtilCode) | [RxPermissions](https://github.com/tbruyelle/RxPermissions) | [PermissionsDispatcher](https://github.com/permissions-dispatcher/PermissionsDispatcher) | [EasyPermissions](https://github.com/googlesamples/easypermissions) |
| :--------: | :------------: | :------------: | :------------: | :------------: | :------------: | :------------: | :------------: |
| 对应版本 | 12.3 | 2.0.3 | 1.5.0 | 1.30.6 | 0.12 | 4.8.0 | 3.0.0 |
| 对应版本 | 12.5 | 2.0.3 | 1.5.1 | 1.30.6 | 0.12 | 4.8.0 | 3.0.0 |
| 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/guolindev/PermissionX.svg)](https://github.com/guolindev/PermissionX/issues) | [![](https://img.shields.io/github/issues/Blankj/AndroidUtilCode.svg)](https://github.com/Blankj/AndroidUtilCode/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) |
| 框架体积 | 27 KB | 127 KB | 66 KB | 500 KB | 28 KB | 91 KB | 48 KB |
| 框架体积 | 28 KB | 127 KB | 66 KB | 500 KB | 28 KB | 91 KB | 48 KB |
| 安装包权限 ||||||||
| 悬浮窗权限 ||||||||
| 通知栏权限 ||||||||
| 系统设置权限 ||||||||
| Android 8.0 权限适配 ||||||||
| Android 9.0 权限适配 ||||||||
| Android 10.0 权限适配 ||||||||
| Android 11 新版存储权限 ||||||||
| Android 11 新版定位策略 ||| |||||
| 屏幕方向旋转场景适配 ||||||||
| 后台申请权限场景适配 ||||||||
| 低级错误检测机制 ||||||||
| Android 10 权限适配 ||||||||
| Android 11 权限适配 ||||||||
| Android 12 权限适配 ||| |||||
| 屏幕方向旋转场景适配 ||||||||
| 后台申请权限场景适配 ||||||||
| 低级错误检测机制 ||||||||

#### 屏幕旋转场景适配介绍

Expand Down
Binary file modified XXPermissions.apk
Binary file not shown.
8 changes: 4 additions & 4 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 30
compileSdkVersion 31

defaultConfig {
applicationId "com.hjq.permissions.demo"
minSdkVersion 14
targetSdkVersion 30
versionCode 1230
versionName "12.3"
targetSdkVersion 31
versionCode 1250
versionName "12.5"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}

Expand Down
10 changes: 9 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />

<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />

<application
android:name=".AppApplication"
android:icon="@mipmap/ic_launcher"
Expand All @@ -41,7 +48,8 @@

<activity
android:name=".MainActivity"
android:launchMode="singleTop">
android:launchMode="singleTop"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
Expand Down
63 changes: 46 additions & 17 deletions app/src/main/java/com/hjq/permissions/demo/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,22 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

findViewById(R.id.btn_main_request_1).setOnClickListener(this);
findViewById(R.id.btn_main_request_2).setOnClickListener(this);
findViewById(R.id.btn_main_request_3).setOnClickListener(this);
findViewById(R.id.btn_main_request_4).setOnClickListener(this);
findViewById(R.id.btn_main_request_5).setOnClickListener(this);
findViewById(R.id.btn_main_request_6).setOnClickListener(this);
findViewById(R.id.btn_main_request_7).setOnClickListener(this);
findViewById(R.id.btn_main_request_8).setOnClickListener(this);
findViewById(R.id.btn_main_request_single).setOnClickListener(this);
findViewById(R.id.btn_main_request_group).setOnClickListener(this);
findViewById(R.id.btn_main_request_location).setOnClickListener(this);
findViewById(R.id.btn_main_request_bluetooth).setOnClickListener(this);
findViewById(R.id.btn_main_request_storage).setOnClickListener(this);
findViewById(R.id.btn_main_request_package).setOnClickListener(this);
findViewById(R.id.btn_main_request_window).setOnClickListener(this);
findViewById(R.id.btn_main_request_notification).setOnClickListener(this);
findViewById(R.id.btn_main_request_setting).setOnClickListener(this);
findViewById(R.id.btn_main_app_details).setOnClickListener(this);
}

@Override
public void onClick(View view) {
int viewId = view.getId();
if (viewId == R.id.btn_main_request_1) {
if (viewId == R.id.btn_main_request_single) {

XXPermissions.with(this)
.permission(Permission.CAMERA)
Expand All @@ -55,7 +56,7 @@ public void onGranted(List<String> permissions, boolean all) {
}
});

} else if (viewId == R.id.btn_main_request_2) {
} else if (viewId == R.id.btn_main_request_group) {

XXPermissions.with(this)
.permission(Permission.RECORD_AUDIO)
Expand All @@ -70,7 +71,7 @@ public void onGranted(List<String> permissions, boolean all) {
}
});

} else if (viewId == R.id.btn_main_request_3) {
} else if (viewId == R.id.btn_main_request_location) {

XXPermissions.with(this)
.permission(Permission.ACCESS_COARSE_LOCATION)
Expand All @@ -87,12 +88,40 @@ public void onGranted(List<String> permissions, boolean all) {
}
});

} else if (viewId == R.id.btn_main_request_4) {
} else if (viewId == R.id.btn_main_request_bluetooth) {

long delayMillis = 0;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
delayMillis = 2000;
toast("当前版本不是 Android 12 及以上,旧版本的需要定位权限才能进行扫描蓝牙");
}

view.postDelayed(new Runnable() {

@Override
public void run() {
XXPermissions.with(MainActivity.this)
.permission(Permission.BLUETOOTH_SCAN)
.permission(Permission.BLUETOOTH_CONNECT)
.permission(Permission.BLUETOOTH_ADVERTISE)
.request(new OnPermissionCallback() {

@Override
public void onGranted(List<String> permissions, boolean all) {
if (all) {
toast("获取蓝牙权限成功");
}
}
});
}
}, delayMillis);

} else if (viewId == R.id.btn_main_request_storage) {

long delayMillis = 0;
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
delayMillis = 2000;
toast("当前版本不是 Android 11 以上,会自动变更为旧版的请求方式");
toast("当前版本不是 Android 11 及以上,会自动变更为旧版的请求方式");
}

view.postDelayed(new Runnable() {
Expand All @@ -116,7 +145,7 @@ public void onGranted(List<String> permissions, boolean all) {
}
}, delayMillis);

} else if (viewId == R.id.btn_main_request_5) {
} else if (viewId == R.id.btn_main_request_package) {

XXPermissions.with(this)
.permission(Permission.REQUEST_INSTALL_PACKAGES)
Expand All @@ -128,7 +157,7 @@ public void onGranted(List<String> permissions, boolean all) {
}
});

} else if (viewId == R.id.btn_main_request_6) {
} else if (viewId == R.id.btn_main_request_window) {

XXPermissions.with(this)
.permission(Permission.SYSTEM_ALERT_WINDOW)
Expand All @@ -140,7 +169,7 @@ public void onGranted(List<String> permissions, boolean all) {
}
});

} else if (viewId == R.id.btn_main_request_7) {
} else if (viewId == R.id.btn_main_request_notification) {

XXPermissions.with(this)
.permission(Permission.NOTIFICATION_SERVICE)
Expand All @@ -152,7 +181,7 @@ public void onGranted(List<String> permissions, boolean all) {
}
});

} else if (viewId == R.id.btn_main_request_8) {
} else if (viewId == R.id.btn_main_request_setting) {

XXPermissions.with(this)
.permission(Permission.WRITE_SETTINGS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,17 @@ protected String getPermissionHint(Context context, List<String> permissions) {
}
break;
}
case Permission.BLUETOOTH_SCAN:
case Permission.BLUETOOTH_CONNECT:
case Permission.BLUETOOTH_ADVERTISE: {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
String hint = context.getString(R.string.common_permission_bluetooth);
if (!hints.contains(hint)) {
hints.add(hint);
}
}
break;
}
case Permission.READ_PHONE_STATE:
case Permission.CALL_PHONE:
case Permission.ADD_VOICEMAIL:
Expand Down
24 changes: 16 additions & 8 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,61 +12,69 @@
android:orientation="vertical">

<Button
android:id="@+id/btn_main_request_1"
android:id="@+id/btn_main_request_single"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="申请单个危险权限" />

<Button
android:id="@+id/btn_main_request_2"
android:id="@+id/btn_main_request_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="申请多个危险权限" />

<Button
android:id="@+id/btn_main_request_3"
android:id="@+id/btn_main_request_location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="申请定位权限组" />

<Button
android:id="@+id/btn_main_request_4"
android:id="@+id/btn_main_request_bluetooth"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="申请蓝牙权限" />

<Button
android:id="@+id/btn_main_request_storage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="申请新版存储权限" />

<Button
android:id="@+id/btn_main_request_5"
android:id="@+id/btn_main_request_package"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="申请安装包权限" />

<Button
android:id="@+id/btn_main_request_6"
android:id="@+id/btn_main_request_window"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="申请悬浮窗权限" />

<Button
android:id="@+id/btn_main_request_7"
android:id="@+id/btn_main_request_notification"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="申请通知栏权限" />

<Button
android:id="@+id/btn_main_request_8"
android:id="@+id/btn_main_request_setting"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<string name="common_permission_contacts">通讯录权限</string>
<string name="common_permission_location">定位权限</string>
<string name="common_permission_location_background">后台定位权限</string>
<string name="common_permission_bluetooth">附近的设备权限</string>
<string name="common_permission_microphone">麦克风权限</string>
<string name="common_permission_phone">电话权限</string>
<string name="common_permission_call_log">通话记录权限</string>
Expand Down
6 changes: 3 additions & 3 deletions library/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 30
compileSdkVersion 31

defaultConfig {
minSdkVersion 11
versionCode 1230
versionName "12.3"
versionCode 1250
versionName "12.5"
}

// 使用 JDK 1.8
Expand Down
Loading

0 comments on commit 37884a7

Please sign in to comment.