├── .gitignore ├── LICENSE ├── README-en.md ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zyq │ │ └── permissiondemo │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── zyq │ │ │ └── permissiondemo │ │ │ ├── BaseActivity.java │ │ │ ├── ChekOnlyActivity.java │ │ │ ├── DismissAskActivity.java │ │ │ ├── EndlessActivity.java │ │ │ ├── FloatWindowActivity.java │ │ │ ├── LocationServiceActivity.java │ │ │ ├── MainActivity.java │ │ │ ├── MyApplication.java │ │ │ ├── NeedReslutActivity.java │ │ │ ├── NotificationActivity.java │ │ │ └── RequestWithExplainActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ └── ic_launcher_background.xml │ │ ├── layout │ │ ├── activity_chekonly.xml │ │ ├── activity_main.xml │ │ └── activity_need_reslut.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── values-en │ │ └── strings.xml │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── zyq │ └── permissiondemo │ └── ExampleUnitTest.java ├── build.gradle ├── easypermissionlib ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zyq │ │ └── easypermission │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── zyq │ │ │ └── easypermission │ │ │ ├── EasyPermission.java │ │ │ ├── EasyPermissionConfig.java │ │ │ ├── EasyPermissionHelper.java │ │ │ ├── EasyPermissionLog.java │ │ │ ├── EasyPermissionResult.java │ │ │ ├── bean │ │ │ ├── EasyAppSettingDialogStyle.java │ │ │ ├── EasyTopAlertStyle.java │ │ │ └── PermissionAlertInfo.java │ │ │ └── util │ │ │ ├── EasyAlphaTouchListener.java │ │ │ ├── EasyAppDialogTool.java │ │ │ ├── EasyCacheData.java │ │ │ ├── EasyFloatWindowTool.java │ │ │ ├── EasyLocationTool.java │ │ │ ├── EasyNotificationTool.java │ │ │ ├── EasyShapeUtil.java │ │ │ └── EasyViewUtil.java │ └── res │ │ ├── drawable │ │ ├── bg_alert_white_shape8.xml │ │ ├── bg_button_blue_shape20.xml │ │ └── bg_button_blueline_shape20.xml │ │ ├── layout │ │ ├── alert_info_top.xml │ │ └── dialog_info_middle.xml │ │ ├── values-en │ │ └── strings.xml │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── style.xml │ └── test │ └── java │ └── com │ └── zyq │ └── easypermission │ └── ExampleUnitTest.java ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | captures/ 34 | 35 | # IntelliJ 36 | *.iml 37 | .idea/ 38 | 39 | # Keystore files 40 | # Uncomment the following line if you do not want to check your keystore files in. 41 | #*.jks 42 | 43 | # External native build folder generated in Android Studio 2.2 and later 44 | .externalNativeBuild 45 | 46 | # Google Services (e.g. APIs or Firebase) 47 | google-services.json 48 | 49 | # Freeline 50 | freeline.py 51 | freeline/ 52 | freeline_project_description.json 53 | 54 | # fastlane 55 | fastlane/report.xml 56 | fastlane/Preview.html 57 | fastlane/screenshots 58 | fastlane/test_output 59 | fastlane/readme.md 60 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README-en.md: -------------------------------------------------------------------------------- 1 | # EasyPermission 2 | README: [中文](https://github.com/githubZYQ/easypermission/blob/master/README.md) | [English](https://github.com/githubZYQ/easypermission/blob/master/README-en.md) 3 | # Introduction to the EasyPermission 4 | * This is a convenient library for permission management in Android, which makes application permission and business code logic be separated easily, 5 | and does not care about permission application and callback. 6 | * The application and judgment of Android dynamic permission can be completed in one sentence:
7 | requestPermission(Manifest.permission.CAMERA)
8 | * The latest version 2.0 is a major update:
9 | 1. Added support for dynamic Settings of "PermissionAlertInfo"
10 | 2. Implementing an Activity monitor does not require an activity to be passed in. You can invoke permission requests anywhere 11 | (Activity,Fragment,View,Service,BroadcastReceiver)
12 | 3. Add special permission processing tools and demonstrations (notification bar, suspension window, location service) 13 | 4. OpenAppDetails () can be called directly from the EasyPermissionResult callback. 14 | 5. Added setAutoOpenAppDetails to automatically fire openAppDetails() when disabled if PermissionAlertInfo has a value. 15 | 6. Increase EasyAppSettingDialogStyle, pop-up support custom text color, size, button text and theme colors, etc. 16 | ## The original 17 | * How did you manage Android permissions in the past?
18 | 1. Determine whether you permission first 19 | 2. Then apply for permission 20 | 3. Finally onRequestPermissionsResult check one by one as a result, to execute the business logic?
21 | * In response to the latest privacy policy, the right to request time limit hope that the function description can pop up to inform users?
22 | * There is a call authority, many places to use, you have to write every call authority judgment application and result processing?
23 | * Now here's the good news:
24 | * There's a library for permissions management in Android where you tell it what permissions you need and then tell it what you want to execute. 25 | ## The Explain 26 | 1. module-**easypermissionlib** is EasyPermission Core source code;
27 | 2. module-**app** is one EasyPermission demo;
28 | # Integration method 29 | The latest version 30 | [![The latest version](https://jitpack.io/v/githubZYQ/easypermission.svg)](https://jitpack.io/#githubZYQ/easypermission) 31 | ## Step 1. Add the JitPack repository to your build file 32 | Add it in your root build.gradle at the end of repositories: 33 | ````groovy 34 | allprojects { 35 | repositories { 36 | ... 37 | maven { url 'https://jitpack.io' } 38 | } 39 | } 40 | ```` 41 | 42 | ## Step 2. Add the dependency 43 | ````groovy 44 | dependencies { 45 | implementation 'com.github.githubZYQ:easypermission:v2.0.15' 46 | } 47 | ```` 48 | ## Step 3. Initial Configuration. 49 | ### 1.The initialization is done in onCreate of the Application 50 | ````java 51 | @Override 52 | public void onCreate() { 53 | super.onCreate(); 54 | //Do this before applying for permission for the first time. It is recommended to do this in Application onCreate() 55 | EasyPermissionHelper.getInstance().init(this); 56 | } 57 | ```` 58 | ### 2.In the Activity that will use EasyPermission's**onRequestPermissionsResult**And**onActivityResult**,
59 | Which Activity need,Call EasyPermissionHelper.getInstance().onRequestPermissionsResult And onActivityResult;
60 | If you have the BaseActivity,then only need to call onRequestPermissionsResult And onActivityResult in BaseActivity。
61 | (To implement automatic callback for authorization results, you do not need to configure the two methods) 62 | ````java 63 | @Override 64 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 65 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 66 | //Use EasyPermissionHelper to inject callbacks 67 | EasyPermissionHelper.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults, this); 68 | } 69 | ```` 70 | ````java 71 | @Override 72 | protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { 73 | super.onActivityResult(requestCode, resultCode, data); 74 | //Use EasyPermissionHelper to inject callbacks 75 | EasyPermissionHelper.getInstance().onActivityResult(requestCode, resultCode, data); 76 | } 77 | ```` 78 | # Function using 79 | Now let's see how to use it. 80 | ## 1.To check the permission 81 | All you need to do is call the **hasPermission** method of EasyPermission, which allows multiple permissions to be passed in at the same time. 82 | ````java 83 | EasyPermission.build().hasPermission(Manifest.permission.CAMERA); 84 | ```` 85 | ## 2.To apply for permission 86 | If you need to apply for permissions when your application starts, and you don't care about the result of permissions,
87 | You only need to call the **requestPermission** method of EasyPermission, which supports multiple permissions passing in.
88 | ````java 89 | EasyPermission.build().requestPermission(Manifest.permission.CAMERA); 90 | ```` 91 | ## 3.Results that require permissions 92 | If you need to know the result of the user's selection after applying permission, while executing your own method myVoid(),
93 | So just do it in **onPermissionsAccess**,
94 | In **onPermissionsDismiss** is the user rejecting permission feedback. 95 | ````java 96 | EasyPermission.build() 97 | .mRequestCode(RC_CODE_PERMISSION) 98 | .mPerms(Manifest.permission.CAMERA) 99 | .mResult(new EasyPermissionResult() { 100 | @Override 101 | public void onPermissionsAccess(int requestCode) { 102 | super.onPermissionsAccess(requestCode); 103 | //Do what you want 104 | } 105 | 106 | @Override 107 | public void onPermissionsDismiss(int requestCode, @NonNull List permissions) { 108 | super.onPermissionsDismiss(requestCode, permissions); 109 | //What do you do if your access is denied by the user 110 | } 111 | }).requestPermission(); 112 | 113 | ```` 114 | ## 4.Sometimes the user rejects the permission, and the popup request is forbidden, what should I do? 115 | * In fact, in the new version you only need to dismissask text via **mAlertInfo**, which now handles pop-ups by default. That means you don't need to rewrite the ** onask ** and **openAppDetails** methods 116 | * As long as you dismissask ** in **onDismissAsk**, you'll get that forbidden result, and you'll want to note that **onDismissAsk** returns false** by default 117 | * If you modify **return true** yourself, you will have handled the forbidden result and will not call back **onPermissionsDismiss** 118 | * Call the **openAppDetails** method to pop up and direct the user to the Settings screen. OnPermissionsAccess will be automatically called back upon success 119 | ````java 120 | easyPermission = EasyPermission.build() 121 | .mRequestCode(RC_CODE_PERMISSION) 122 | .mPerms(Manifest.permission.CAMERA) 123 | .setAutoOpenAppDetails(true) //被拒绝并禁止时是否自动弹窗提醒,默认是true 124 | .mAlertInfo( new PermissionAlertInfo("**APP need to apply for camera permission", 125 | "**APP need to apply for camera shooting permission, so that you can scan the TWO-DIMENSIONAL code by scanning; Change the profile picture of your account by taking a photo; Take a photo and upload some id information needed to register an account. Refusal or cancellation of authorization will affect the above functions, but will not affect the use of other services")) 126 | .mResult(new EasyPermissionResult() { 127 | @Override 128 | public void onPermissionsAccess(int requestCode) { 129 | super.onPermissionsAccess(requestCode); 130 | //Permission approved 131 | } 132 | 133 | @Override 134 | public void onPermissionsDismiss(int requestCode, @NonNull List permissions) { 135 | super.onPermissionsDismiss(requestCode, permissions); 136 | //Permission denied 137 | } 138 | @Override 139 | public boolean onDismissAsk(int requestCode, @NonNull List permissions) { 140 | //Permission is denied and no further questioning is allowed 141 | //Here true means intercept processing, no more callback onPermissionsDismiss; 142 | return super.onDismissAsk(requestCode,permissions); 143 | } 144 | @Override 145 | public void openAppDetails() { 146 | //A dialog box is displayed asking you to set the default permission details. After the permission operation is complete on the Settings page, the system automatically calls back toonPermissionsAccess() 147 | super.openAppDetails(); 148 | //If the style is not satisfactory, you can pop up your own description popover and call "goToAppSettings()" when the user confirms;then jump Settings page is complete 149 | } 150 | ).requestPermission(); 151 | ```` 152 | ## 5.Popover style custom 153 | The permissions library is quite convenient to use, but the text color of the popover needs to be changed. It is not like writing the popover by yourself every time. Can you set the text size and color? No problem, we support popover custom styles. 154 | ### Use the default classic style (similar to JINGdong, Xiaohongshu) 155 | ````java 156 | EasyPermissionHelper.getInstance().setDialogStyle(new EasyAppSettingDialogStyle(EasyAppSettingDialogStyle.DialogStyle.STYLE_DEFAULT)); 157 | ```` 158 | ### Use the system's own popover style 159 | ````java 160 | EasyPermissionHelper.getInstance().setDialogStyle(new EasyAppSettingDialogStyle(EasyAppSettingDialogStyle.DialogStyle.STYLE_SYSTEM)); 161 | ```` 162 | ### Use system custom popover styles 163 | ````java 164 | EasyPermissionHelper.getInstance().setDialogStyle( 165 | new EasyAppSettingDialogStyle(EasyAppSettingDialogStyle.DialogStyle.STYLE_CUSTOM) 166 | .setTitleGravity(Gravity.CENTER)//设置居中 167 | .setTitleSize(17)//设置标题样式 168 | .setTitleColor("#333333") 169 | .setMessageSize(14)//设置内容样式 170 | .setMessageColor("#666666") 171 | .setButtonTextSize(14)//设置按钮样式 172 | .setButtonThemeColor("#FF0000") 173 | .setCancelText("取消")//设置按钮文本 174 | .setConfirmText("去打开")); 175 | ```` 176 | ### Fully custom popovers 177 | You need to set the preceding mode only once after the initial call and it takes effect globally. If the above method still does not satisfy your appetite, then you have to control the popover pull yourself。
178 | Rewrite **openAppDetails** in **EasyPermissionResult** 179 | ````java 180 | @Override 181 | public void openAppDetails() { 182 | //Display your own popup on the Go to App Settings details page to tell the user what permissions we want to open 183 | //When users click on the confirmation call easyPermission. GoToAppSettings (); The jump Settings page is complete 184 | } 185 | ```` 186 | ## 6.Top prompt message style custom 187 | 权限库用起来蛮方便的,但是顶部提示的背景颜色需要改一下,能不能设置一下文字大小、颜色、背景色?没问题,咱们支持顶部提示信息自定义样式。 188 | ### Use the default classic style 189 | ````java 190 | EasyPermissionHelper.getInstance().setTopAlertStyle( 191 | new EasyTopAlertStyle(EasyTopAlertStyle.AlertStyle.STYLE_DEFAULT)); 192 | ```` 193 | 194 | ### Use custom prompt styles 195 | ````java 196 | EasyPermissionHelper.getInstance().setTopAlertStyle( 197 | new EasyTopAlertStyle(EasyTopAlertStyle.AlertStyle.STYLE_CUSTOM) 198 | .setTitleGravity(Gravity.LEFT)//默认居左 199 | .setTitleSize(16)//设置标题样式,默认16sp 200 | .setTitleColor("#333333") 201 | .setMessageSize(14)//设置内容样式,默认14sp 202 | .setMessageColor("#333333") 203 | .setBackgroundColor("#FFFFFF")//设置背景色,默认白色 204 | .setBackgroundRadius(8)//设置背景圆角弧度,默认8dp 205 | .setBackgroundElevation(6)//设置背景阴影范围,默认6dp 206 | .setTopMargin(10)//设置距离顶部标题栏间距,默认10dp 207 | .setSideMargin(10));//设置距离屏幕两边宽度,默认10dp 208 | ```` 209 | ## 7.Other Matters needing attention 210 | 1. mAlertInfowill not automatically pop up the permission description window if it is not set. In order to meet the current increasingly strict privacy policy, please take every permission description seriously
211 | 2. Permission applications are not recommended to be obtained in onNewIntent
212 | 3. Related log tag for "EasyPermissionLog", the default output not too much information, if need to debug. Please open the EasyPermissionConfigs setDebug (true)
213 | 4. Added setAutoOpenAppDetails to automatically fire openAppDetails() when disabled if PermissionAlertInfo has a value. 214 | 5. If the openAppDetails() style doesn't suit, you can override openAppDetails() to customize pop-up content, or block it directly in onDismissAsk() 215 | 6. Because EasyPermission in the init initialization time use ActivityLifecycleCallbacks began to monitor the activity of change, so if needed in the launchMode = "singleTask" onNewIntent request permissions, You need to reset the activity. 216 | This can be done in two ways. 217 | ### The Way One: 218 | ````java 219 | EasyPermissionHelper.getInstance().updateTopActivity(mContext); 220 | easyPermission.requestPermission(); 221 | ```` 222 | ### The Way Two: 223 | ````java 224 | easyPermission.mContext(mContext).requestPermission(); 225 | ```` 226 | ## 8.Other tools 227 | ### Location Service Management :LocationTool 228 | After Android 9.0, even if you have obtained user authorization for location, you still cannot obtain location because the GPS location service is not enabled. Therefore, you need to process the location service. The LocationTool supports the following methods:
229 | 1. isLocationEnabled() Obtain whether the current location service is enabled
230 | 2. gotoAppSettings() Jump directly to the mobile - Settings - Security - Privacy - Location Services on/off page
231 | ### Notification Service Management :EasyNotificationTool 232 | Notification service permissions in Android is also more special, it is not like other permissions to apply directly, like location services need to go to the system Settings, so also to the Settings page:
233 | 1. isNotificationEnabled() Whether the notification permission of the current APP is enabled
234 | 2. gotoAppSettings() Jump directly to mobile - Settings - Notifications and status bar - Notifications Management -APP Notification Settings page
235 | > ### Hover window permission management :EasyFloatWindowTool 236 | Floating window permission in Android is also special, it is not like other permissions to apply directly, like location services need to be opened in the system Settings, so you also need to go to the Settings page:
237 | 1. isFloatWindowEnabled() Gets whether the current APP has hover window permissions
238 | 2. gotoAppSettings() Jump directly to mobile - Settings - Application Management - Special application permissions - display in the upper layer of other applications -APP Settings page
239 | # The last 240 | * If you have better plans and ideas, welcome to leave a message or private letter
241 | * Peace, happiness, family harmony and good health to all.
242 | * May the motherland complete its reunification at an early date and the world live in peace and prosperity.
243 | * If you need [lantern](https://github.com/getlantern/lantern) to F&Q ,enter my invitation code **YPH99Z5** ,to get three months of blue light pro![Download lantern]( https://github.com/getlantern/forum)
244 | * If you have any questions, please timely feedback to me.
245 | * If you like it, please thumb up.o( ̄▽ ̄)d。 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # EasyPermission 2 | README: [中文](https://github.com/githubZYQ/easypermission/blob/master/README.md) | [English](https://github.com/githubZYQ/easypermission/blob/master/README-en.md) 3 | # EasyPermission简介 4 | * 这个是一个方便Android中权限管理的库,它使得申请权限和业务代码逻辑简单分离,不去关心权限的申请和回调。
5 | * 将安卓动态权限的申请和判断简单到一句话就能完成:
6 | requestPermission(Manifest.permission.CAMERA)
7 | * 最新2.0版本主要更新:
8 | 1. 增加了支持动态设置 PermissionAlertInfo (权限提示信息)
9 | 2. 实现activity监控不在要求必须传入Activity,可在任意地方调用权限申请(Activity,Fragment,View,Service,BroadcastReceiver)
10 | 3. 增加特殊权限处理工具及演示(通知栏、悬浮窗、定位服务) 11 | 4. EasyPermissionResult回调中可直接调用 openAppDetails(); 12 | 5. 增加setAutoOpenAppDetails,如果PermissionAlertInfo有值,则在被禁止时自动触发openAppDetails(); 13 | 6. 增加EasyAppSettingDialogStyle,说明弹窗支持自定义的文本颜色、大小、按钮文本和主题颜色等; 14 | ## 效果 15 | ![展示效果](https://img-blog.csdnimg.cn/img_convert/9fd723f20fa0a3deb7105513e6fb7617.gif "这是展示效果") 16 | ## 初衷 17 | * 以前你是怎么管理Android的权限的?
18 | 1. 先判断有没有权限 19 | 2. 再申请权限 20 | 3. 最后onRequestPermissionsResult中检查一个个结果,再执行自己的业务逻辑?
21 | * 有一个打电话的权限,好多地方都要用,你每次使用打电话都要写一遍权限的判断申请和结果处理?
22 | * 应对最新隐私政策,请求权限时希望可以弹出功能说明对用户进行告知?
23 | * 那现在有一个好消息:
24 | 这儿有一个方便Android中权限管理的库,你告诉它你需要的权限,然后再告诉它你想要执行什么,就可以了。 25 | ## 解释 26 | 1. module-**easypermissionlib**是EasyPermission 的核心源码code;
27 | 2. module-**app**是EasyPermission的一个使用demo;
28 | # 集成方法 29 | 最新版本 30 | [![最新版本](https://jitpack.io/v/githubZYQ/easypermission.svg)](https://jitpack.io/#githubZYQ/easypermission) 31 | ## 第一步. 添加JitPack 32 | 将其添加到根build.gradle中. 33 | ````groovy 34 | allprojects { 35 | repositories { 36 | ... 37 | maven { url 'https://jitpack.io' } 38 | } 39 | } 40 | ```` 41 | 42 | ## 第二步. 添加依赖 43 | ````groovy 44 | dependencies { 45 | implementation 'com.github.githubZYQ:easypermission:v2.0.15' 46 | } 47 | ```` 48 | ## 第三步. 初始化配置 49 | ### 1.在Application的onCreate中完成初始化 50 | ````java 51 | @Override 52 | public void onCreate() { 53 | super.onCreate(); 54 | //首次使用权限申请之前完成初始化,建议放在Application onCreate()中完成 55 | EasyPermissionHelper.getInstance().init(this); 56 | } 57 | ```` 58 | ### 2.将要使用EasyPermission的Activity中的**onRequestPermissionsResult**和**onActivityResult**,
59 | * 在对应的Activity调用EasyPermissionHelper.getInstance().onRequestPermissionsResult和onActivityResult即可;
60 | * 如果你有BaseActivity,那么只需要在BaseActivity中设置一次即可。 61 | * 这两个方方法为了实现授权结果的自动回调,如果不需要回调可以不配置 62 | ````java 63 | @Override 64 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 65 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 66 | //使用EasyPermissionHelper注入回调 67 | EasyPermissionHelper.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults, this); 68 | } 69 | ```` 70 | ````java 71 | @Override 72 | protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { 73 | super.onActivityResult(requestCode, resultCode, data); 74 | //使用EasyPermissionHelper注入回调 75 | EasyPermissionHelper.getInstance().onActivityResult(requestCode, resultCode, data); 76 | } 77 | ```` 78 | # 功能使用 79 | 接下来看下怎么使用。 80 | ## 1.检测权限 81 | 只需要调用EasyPermission的**hasPermission**方法,支持多个权限同时传入。 82 | ````java 83 | EasyPermission.build().hasPermission(Manifest.permission.CAMERA); 84 | ```` 85 | ## 2.申请权限 86 | 如果你在应用启动时需要申请权限,而且并不关注权限的结果,
87 | 只需要调用EasyPermission的**requestPermission**方法,支持多个权限传入。
88 | ````java 89 | EasyPermission.build().requestPermission(Manifest.permission.CAMERA); 90 | ```` 91 | ## 3.需要权限的结果 92 | * 如果你需要知道申请权限后用户的选择结果,同时去执行自己的方法myVoid(),
93 | * 那么在**onPermissionsAccess**中去做就可以了,
94 | * 在**onPermissionsDismiss**是用户拒绝了权限的反馈。 95 | ````java 96 | EasyPermission.build() 97 | .mRequestCode(RC_CODE_PERMISSION) 98 | .mPerms(Manifest.permission.CAMERA) 99 | .mResult(new EasyPermissionResult() { 100 | @Override 101 | public void onPermissionsAccess(int requestCode) { 102 | super.onPermissionsAccess(requestCode); 103 | //权限已通过 104 | } 105 | 106 | @Override 107 | public void onPermissionsDismiss(int requestCode, @NonNull List permissions) { 108 | super.onPermissionsDismiss(requestCode, permissions); 109 | //权限被拒绝 110 | } 111 | }).requestPermission(); 112 | 113 | ```` 114 | ## 4.有时用户拒绝了权限,而且禁止了弹出询问,我该怎么办?想要在申请权限时弹窗告知用户权限的必要性怎么办? 115 | * 事实上,在新版本只需要通过**mAlertInfo**设置了提示文本,现在已经默认处理了弹窗的展示,也就是说不需要去重写**onDismissAsk**和**openAppDetails**方法了 116 | * 只要在**onDismissAsk**中,就可以得到被禁止的结果,同时你要注意**onDismissAsk**默认返回**false** 117 | * 如果你自己修改**return true**,将视为已经处理了禁止结果,将不再回调**onPermissionsDismiss**这个方法 118 | * 调用**openAppDetails**方法,可以弹窗引导用户去设置界面设置权限,成功后会自动回调onPermissionsAccess 119 | ````java 120 | easyPermission = EasyPermission.build() 121 | .mRequestCode(RC_CODE_PERMISSION) 122 | .mPerms(Manifest.permission.CAMERA) 123 | .setAutoOpenAppDetails(true) //被拒绝并禁止时是否自动弹窗提醒,默认是true 124 | .mAlertInfo( new PermissionAlertInfo("**需要申请摄像头权限", 125 | "**需要申请摄像头拍摄权限,以便您能够通过扫一扫实现扫描二维码;通过拍照更换您帐号的头像;拍照上传一些注册帐号需要的证件信息。拒绝或取消授权将影响以上功能,不影响使用其他服务")) 126 | .mResult(new EasyPermissionResult() { 127 | @Override 128 | public void onPermissionsAccess(int requestCode) { 129 | super.onPermissionsAccess(requestCode); 130 | //权限已通过 131 | } 132 | 133 | @Override 134 | public void onPermissionsDismiss(int requestCode, @NonNull List permissions) { 135 | super.onPermissionsDismiss(requestCode, permissions); 136 | //权限被拒绝 137 | } 138 | 139 | @Override 140 | public boolean onDismissAsk(int requestCode, @NonNull List permissions) { 141 | //权限被拒绝并禁止再次询问 142 | return super.onDismissAsk(requestCode,permissions);//这里true表示拦截处理,不再回调onPermissionsDismiss; 143 | } 144 | @Override 145 | public void openAppDetails() { 146 | //弹出默认的权限详情设置提示弹出框,在设置页完成允许操作后,会自动回调到onPermissionsAccess() 147 | super.openAppDetails(); 148 | //如果样式不满意,可以弹出自定义明弹窗,在用户确认时调用 goToAppSettings();完成跳转设置页 149 | }).requestPermission(); 150 | ```` 151 | ## 5.弹窗样式自定义 152 | 权限库用起来蛮方便的,但是弹窗的文字颜色需要改一下,又不像大动干戈地每次自己去写弹窗,能不能设置一下文字大小、颜色?没问题,咱们支持弹窗自定义样式。 153 | ### 使用默认经典样式(类似京东、小红书) 154 | ````java 155 | EasyPermissionHelper.getInstance().setDialogStyle(new EasyAppSettingDialogStyle(EasyAppSettingDialogStyle.DialogStyle.STYLE_DEFAULT)); 156 | ```` 157 | ### 使用系统自带弹窗样式 158 | ````java 159 | EasyPermissionHelper.getInstance().setDialogStyle(new EasyAppSettingDialogStyle(EasyAppSettingDialogStyle.DialogStyle.STYLE_SYSTEM)); 160 | ```` 161 | ### 使用自定义弹窗样式 162 | ````java 163 | EasyPermissionHelper.getInstance().setDialogStyle( 164 | new EasyAppSettingDialogStyle(EasyAppSettingDialogStyle.DialogStyle.STYLE_CUSTOM) 165 | .setTitleGravity(Gravity.CENTER)//设置居中 166 | .setTitleSize(17)//设置标题样式 167 | .setTitleColor("#333333") 168 | .setMessageSize(14)//设置内容样式 169 | .setMessageColor("#666666") 170 | .setButtonTextSize(14)//设置按钮样式 171 | .setButtonThemeColor("#FF0000") 172 | .setCancelText("取消")//设置按钮文本 173 | .setConfirmText("去打开")); 174 | ```` 175 | ### 完全自定义弹窗 176 | 以上方式只需要在初始话后设置一次,全局生效。如果以上方式依然满足不了你胃口,那只能自己去控制弹窗拉。
177 | 在**EasyPermissionResult**中重写**openAppDetails** 178 | ````java 179 | @Override 180 | public void openAppDetails() { 181 | //在前往应用设置详情页展示自己的弹窗告知用户我们需要哪些权限打开 182 | //在用户点击确认时调用easyPermission.goToAppSettings();完成跳转设置页 183 | } 184 | ```` 185 | ## 6.顶部提示信息样式自定义 186 | 权限库用起来蛮方便的,但是顶部提示的背景颜色需要改一下,能不能设置一下文字大小、颜色、背景色?没问题,咱们支持顶部提示信息自定义样式。 187 | ### 使用默认经典样式 188 | ````java 189 | EasyPermissionHelper.getInstance().setTopAlertStyle( 190 | new EasyTopAlertStyle(EasyTopAlertStyle.AlertStyle.STYLE_DEFAULT)); 191 | ```` 192 | 193 | ### 使用自定义提示样式 194 | ````java 195 | EasyPermissionHelper.getInstance().setTopAlertStyle( 196 | new EasyTopAlertStyle(EasyTopAlertStyle.AlertStyle.STYLE_CUSTOM) 197 | .setTitleGravity(Gravity.LEFT)//默认居左 198 | .setTitleSize(16)//设置标题样式,默认16sp 199 | .setTitleColor("#333333") 200 | .setMessageSize(14)//设置内容样式,默认14sp 201 | .setMessageColor("#333333") 202 | .setBackgroundColor("#FFFFFF")//设置背景色,默认白色 203 | .setBackgroundRadius(8)//设置背景圆角弧度,默认8dp 204 | .setBackgroundElevation(6)//设置背景阴影范围,默认6dp 205 | .setTopMargin(10)//设置距离顶部标题栏间距,默认10dp 206 | .setSideMargin(10));//设置距离屏幕两边宽度,默认10dp 207 | ```` 208 | ## 7.其它注意事项 209 | 1. mAlertInfo不设置将不会自动弹出权限说明弹窗,为了满足当前的日益严格的隐私政策,请对认真对待每一个权限说明
210 | 2. 权限的申请不建议在onNewIntent中获取
211 | 3. 相关日志tag为"EasyPermissionLog",默认不输出太多信息,如果需要调试请打开EasyPermissionConfigs.setDebug(true)
212 | 4. 增加setAutoOpenAppDetails,如果PermissionAlertInfo有值,则在被禁止时自动触发 openAppDetails() 213 | 5. 如果openAppDetails()样式不满足,可以重写openAppDetails()自定义弹出内容,也可以直接在onDismissAsk()拦截 214 | 6. 由于EasyPermission在init初始化时使用ActivityLifecycleCallbacks开始监听activity变化,所以在launchMode="singleTask" onNewIntent中如果需要请求权限,需要重新设置activity。 215 | 可以使用两种方式完成。 216 | ### 方式一: 217 | ````java 218 | EasyPermissionHelper.getInstance().updateTopActivity(mContext); 219 | easyPermission.requestPermission(); 220 | ```` 221 | ### 方式二: 222 | ````java 223 | easyPermission.mContext(mContext).requestPermission(); 224 | ```` 225 | ## 8.其它工具 226 | ### 定位服务管理 EasyLocationTool 227 | Android 9.0以后即使已经获得了用户授权定位权限,由于GPS定位服务未打开,依然获取不到定位,所以还需要对定位服务进行处理,LocationTool支持以下方法:
228 | 1. isLocationEnabled() 获取当前定位服务是否开启
229 | 2. gotoAppSettings() 直接跳转到手机-设置-安全和隐私-定位服务开启/关闭的页面
230 | ### 通知服务管理 EasyNotificationTool 231 | 通知服务的权限在Android中也比较特殊,它不像其它权限那样去直接申请,像定位服务一样需要去系统设置中开启,所以也要去设置页:
232 | 1. isNotificationEnabled() 获取当前APP的通知权限是否开启
233 | 2. gotoAppSettings() 直接跳转到手机-设置-通知和状态栏-通知管理-APP通知设置页
234 | ### 悬浮窗权限管理 EasyFloatWindowTool 235 | 悬浮窗权限在Android中也比较特殊,它不像其它权限那样去直接申请,像定位服务一样需要去系统设置中开启,所以也要去设置页:
236 | 1. isFloatWindowEnabled() 获取当前APP的是否有悬浮窗权限
237 | 2. gotoAppSettings() 直接跳转到手机-设置-应用管理-特殊应用权限-显示在其他应用的上层-APP设置页
238 | # 结束语 239 | * 如果又更好的方案和思路,欢迎留言或者私信
240 | * 祝所有人平安幸福、家庭和睦、身体健康。
241 | * 愿祖国早日完成统一大业,世界和平共处,繁荣发展。
242 | * 有任何疑问,可以及时反馈给我;
243 | * 如果你觉得还不错,请点赞o( ̄▽ ̄)d。 244 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 30 5 | buildToolsVersion "30.0.1" 6 | 7 | defaultConfig { 8 | applicationId "cn.nova.permissiondemo" 9 | minSdkVersion 19 10 | targetSdkVersion 30 11 | versionCode 1 12 | versionName "1.0" 13 | testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner' 14 | } 15 | buildTypes { 16 | release { 17 | minifyEnabled false 18 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro' 19 | } 20 | } 21 | } 22 | 23 | dependencies { 24 | implementation fileTree(include: ['*.jar'], dir: 'libs') 25 | implementation 'androidx.appcompat:appcompat:1.0.0' 26 | implementation 'androidx.constraintlayout:constraintlayout:1.1.3' 27 | testImplementation 'junit:junit:4.12' 28 | androidTestImplementation 'androidx.test.ext:junit:1.1.1' 29 | androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0' 30 | implementation project(':easypermissionlib') 31 | } 32 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/zyq/permissiondemo/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.zyq.permissiondemo; 2 | 3 | import android.content.Context; 4 | import androidx.test.platform.app.InstrumentationRegistry; 5 | import androidx.test.ext.junit.runners.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumented test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("cn.nova.permissiondemo", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 25 | 26 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyq/permissiondemo/BaseActivity.java: -------------------------------------------------------------------------------- 1 | package com.zyq.permissiondemo; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import androidx.annotation.NonNull; 6 | import androidx.annotation.Nullable; 7 | import androidx.appcompat.app.AppCompatActivity; 8 | import android.widget.Toast; 9 | 10 | import com.zyq.easypermission.EasyPermissionHelper; 11 | 12 | /** 13 | * @author Zhang YanQiang 14 | * @date 2019/6/3 14:49. 15 | */ 16 | public class BaseActivity extends AppCompatActivity { 17 | /** 18 | * 不同的权限应该有不同的回调code 19 | */ 20 | protected static final int RC_CODE_PERMISSION = 1024; 21 | protected BaseActivity mContext; 22 | 23 | @Override 24 | protected void onCreate(@Nullable Bundle savedInstanceState) { 25 | super.onCreate(savedInstanceState); 26 | mContext = this; 27 | } 28 | 29 | /** 30 | * 在baseActivity覆盖该方法,或者每个活动需要使用EasyPermission 31 | * Override this method in baseActivity ,or each Activity who need use EasyPermission 32 | */ 33 | @Override 34 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { 35 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 36 | //使用EasyPermissionHelper注入回调 37 | // Inject the callback using EasyPermissionHelper 38 | EasyPermissionHelper.getInstance().onRequestPermissionsResult(requestCode, permissions, grantResults, this); 39 | } 40 | 41 | /** 42 | * 需要从系统设置界面返回时判断权限情况的,在baseActivity覆盖该方法,或者每个活动需要使用EasyPermission 43 | * Override this method in baseActivity ,or each Activity who need use EasyPermission, 44 | * if you want to get the new state form settings page 45 | */ 46 | @Override 47 | protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { 48 | super.onActivityResult(requestCode, resultCode, data); 49 | //使用EasyPermissionHelper注入回调 50 | // Inject the callback using EasyPermissionHelper 51 | EasyPermissionHelper.getInstance().onActivityResult(requestCode, resultCode, data); 52 | } 53 | 54 | /** 55 | * start one activity 56 | * @param newClass activity's name 57 | */ 58 | protected void startOneActivity(@NonNull Class newClass) { 59 | startActivity(new Intent(this, newClass)); 60 | } 61 | 62 | /** 63 | * A Tip 64 | * @param msg 65 | */ 66 | protected void mToast(CharSequence msg){ 67 | Toast.makeText(mContext, msg, Toast.LENGTH_LONG).show(); 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyq/permissiondemo/ChekOnlyActivity.java: -------------------------------------------------------------------------------- 1 | package com.zyq.permissiondemo; 2 | 3 | import android.Manifest; 4 | import androidx.appcompat.app.AppCompatActivity; 5 | 6 | import android.content.Intent; 7 | import android.os.Bundle; 8 | import android.view.View; 9 | import android.widget.TextView; 10 | 11 | import com.zyq.easypermission.EasyPermission; 12 | 13 | /** 14 | * 只检查权限和结果并显示它 15 | * only check the permission and result and show it 16 | * @author zyq 17 | * @date 2019 06 03 18 | */ 19 | public class ChekOnlyActivity extends BaseActivity { 20 | 21 | @Override 22 | protected void onCreate(Bundle savedInstanceState) { 23 | super.onCreate(savedInstanceState); 24 | setContentView(R.layout.activity_chekonly); 25 | TextView textView = findViewById(R.id.textView); 26 | textView.setText(EasyPermission.build().hasPermission(Manifest.permission.CAMERA) ? "已允许" : "未允许"); 27 | findViewById(R.id.btnRequest).setOnClickListener(new View.OnClickListener() { 28 | @Override 29 | public void onClick(View v) { 30 | startActivity(new Intent(mContext,RequestWithExplainActivity.class)); 31 | } 32 | }); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyq/permissiondemo/DismissAskActivity.java: -------------------------------------------------------------------------------- 1 | package com.zyq.permissiondemo; 2 | 3 | import android.Manifest; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import androidx.annotation.NonNull; 7 | 8 | import android.view.View; 9 | import android.widget.TextView; 10 | 11 | import com.zyq.easypermission.EasyPermission; 12 | import com.zyq.easypermission.EasyPermissionResult; 13 | 14 | import java.util.List; 15 | 16 | /** 17 | * 演示对DismissAsk 18 | * demo for DismissAsk 19 | * 20 | * @author Zhang YanQiang 21 | * @date 2019 06 03 22 | */ 23 | public class DismissAskActivity extends BaseActivity { 24 | TextView textView2; 25 | EasyPermission easyPermission; 26 | 27 | @Override 28 | protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | setContentView(R.layout.activity_need_reslut); 31 | textView2 = findViewById(R.id.textView2); 32 | 33 | findViewById(R.id.btnRequest).setOnClickListener(new View.OnClickListener() { 34 | @Override 35 | public void onClick(View v) { 36 | easyPermission.requestPermission(); 37 | } 38 | }); 39 | findViewById(R.id.btnShowResult).setOnClickListener(new View.OnClickListener() { 40 | @Override 41 | public void onClick(View v) { 42 | startOneActivity(ChekOnlyActivity.class); 43 | } 44 | }); 45 | 46 | easyPermission = EasyPermission.build() 47 | .mRequestCode(RC_CODE_PERMISSION) 48 | // .mContext(DismissAskActivity.this) 49 | .mPerms(Manifest.permission.CAMERA) 50 | .mResult(new EasyPermissionResult() { 51 | @Override 52 | public void onPermissionsAccess(int requestCode) { 53 | super.onPermissionsAccess(requestCode); 54 | mToast("权限已通过"); 55 | //做你想做的 56 | textView2.setText("权限已通过"); 57 | } 58 | 59 | @Override 60 | public void onPermissionsDismiss(int requestCode, @NonNull List permissions) { 61 | super.onPermissionsDismiss(requestCode, permissions); 62 | mToast("权限被拒绝"); 63 | //你的权限被用户拒绝了你怎么办 64 | textView2.setText(permissions.toString() + " 被拒绝了"); 65 | } 66 | 67 | @Override 68 | public boolean onDismissAsk(int requestCode, @NonNull List permissions) { 69 | mToast("权限被禁止"); 70 | //你的权限被用户禁止了并且不能请求了你怎么办 71 | textView2.setText(permissions.toString() + " 被禁止了,需要向用户说明情况"); 72 | //弹出默认的权限详情设置提示弹出框,在设置页完成允许操作后,会自动回调到onPermissionsAccess() 73 | easyPermission.openAppDetails(getString(R.string.permission_explain_camera)); 74 | //如果样式不满意,可以弹出自己的说明弹窗,在用户确认时调用easyPermission.goToAppSettings();完成跳转设置页 75 | return true;//这里true表示拦截处理,不再回调onPermissionsDismiss; 76 | } 77 | }); 78 | easyPermission.requestPermission(); 79 | } 80 | 81 | // @Override 82 | // protected void onActivityResult(int requestCode, int resultCode, Intent data) { 83 | // super.onActivityResult(requestCode, resultCode, data); 84 | // if (easyPermission.getRequestCode() == requestCode) { 85 | // //设置界面返回 86 | // //Result from system setting 87 | // if (easyPermission.hasPermission(DismissAskActivity.this)) { 88 | // //做你想做的 89 | // textView2.setText("权限已通过"); 90 | // } else { 91 | // //从设置回来还是没给你权限 92 | // textView2.setText("从设置回来还是没打开权限"); 93 | // } 94 | // 95 | // } 96 | // } 97 | } 98 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyq/permissiondemo/EndlessActivity.java: -------------------------------------------------------------------------------- 1 | package com.zyq.permissiondemo; 2 | 3 | import android.Manifest; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import androidx.annotation.NonNull; 7 | 8 | import android.view.View; 9 | import android.widget.TextView; 10 | 11 | import com.zyq.easypermission.EasyPermission; 12 | import com.zyq.easypermission.EasyPermissionResult; 13 | 14 | import java.util.List; 15 | 16 | /** 17 | * 当权限必需时你才能进行下一步你怎么办 18 | * demo for endless 19 | * 20 | * @author Zhang YanQiang 21 | * @date 2019 06 03 22 | */ 23 | public class EndlessActivity extends BaseActivity { 24 | TextView textView2; 25 | EasyPermission easyPermission; 26 | 27 | @Override 28 | protected void onCreate(Bundle savedInstanceState) { 29 | super.onCreate(savedInstanceState); 30 | setContentView(R.layout.activity_need_reslut); 31 | textView2 = findViewById(R.id.textView2); 32 | 33 | findViewById(R.id.btnRequest).setOnClickListener(new View.OnClickListener() { 34 | @Override 35 | public void onClick(View v) { 36 | easyPermission.requestPermission(); 37 | } 38 | }); 39 | findViewById(R.id.btnShowResult).setOnClickListener(new View.OnClickListener() { 40 | @Override 41 | public void onClick(View v) { 42 | startOneActivity(ChekOnlyActivity.class); 43 | } 44 | }); 45 | 46 | easyPermission = EasyPermission.build() 47 | .mRequestCode(RC_CODE_PERMISSION) 48 | // .mContext(EndlessActivity.this) 49 | .mPerms(Manifest.permission.CAMERA) 50 | .mResult(new EasyPermissionResult() { 51 | @Override 52 | public void onPermissionsAccess(int requestCode) { 53 | super.onPermissionsAccess(requestCode); 54 | //做你想做的 55 | textView2.setText("权限已通过"); 56 | mToast("权限通过"); 57 | } 58 | 59 | @Override 60 | public void onPermissionsDismiss(int requestCode, @NonNull List permissions) { 61 | super.onPermissionsDismiss(requestCode, permissions); 62 | mToast("权限被拒绝"); 63 | //你的权限被用户拒绝了你怎么办 64 | textView2.setText(permissions.toString() + " 被拒绝了"); 65 | //just request it again 66 | easyPermission.requestPermission(); 67 | } 68 | 69 | @Override 70 | public boolean onDismissAsk(int requestCode, @NonNull List permissions) { 71 | mToast("权限被禁止"); 72 | //你的权限被用户禁止了并且不能请求了你怎么办 73 | textView2.setText(permissions.toString() + " 被禁止了,需要向用户说明情况"); 74 | easyPermission.openAppDetails( getString(R.string.permission_explain_camera)); 75 | return true; 76 | } 77 | }); 78 | easyPermission.requestPermission(); 79 | } 80 | 81 | @Override 82 | protected void onActivityResult(int requestCode, int resultCode, Intent data) { 83 | super.onActivityResult(requestCode, resultCode, data); 84 | if (easyPermission.getRequestCode() == requestCode) { 85 | //设置界面返回 86 | //Result from system setting 87 | if (easyPermission.hasPermission()) { 88 | //做你想做的 89 | textView2.setText("权限已通过"); 90 | } else { 91 | //从设置回来还是没给你权限 92 | textView2.setText("I still don't have access from setting"); 93 | } 94 | 95 | } 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyq/permissiondemo/FloatWindowActivity.java: -------------------------------------------------------------------------------- 1 | package com.zyq.permissiondemo; 2 | 3 | import android.os.Bundle; 4 | import android.view.View; 5 | import android.widget.TextView; 6 | 7 | import com.zyq.easypermission.util.EasyFloatWindowTool; 8 | 9 | /** 10 | * 演示悬浮窗权限 11 | * demo for FloatWindow 12 | * @author Zhang YanQiang 13 | * @date 2022 03 22 14 | */ 15 | public class FloatWindowActivity extends BaseActivity { 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_need_reslut); 20 | findViewById(R.id.btnRequest).setOnClickListener(new View.OnClickListener() { 21 | @Override 22 | public void onClick(View v) { 23 | EasyFloatWindowTool.gotoAppSettings(mContext); 24 | } 25 | }); 26 | findViewById(R.id.btnShowResult).setOnClickListener(new View.OnClickListener() { 27 | @Override 28 | public void onClick(View v) { 29 | showResult(); 30 | } 31 | }); 32 | } 33 | 34 | @Override 35 | protected void onResume() { 36 | super.onResume(); 37 | showResult(); 38 | } 39 | private void showResult(){ 40 | TextView textView2 = findViewById(R.id.textView2); 41 | textView2.setText(EasyFloatWindowTool.isFloatWindowEnabled(mContext)?"已通过":"未通过"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyq/permissiondemo/LocationServiceActivity.java: -------------------------------------------------------------------------------- 1 | package com.zyq.permissiondemo; 2 | 3 | import android.Manifest; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | import android.widget.TextView; 7 | 8 | import androidx.annotation.NonNull; 9 | 10 | import com.zyq.easypermission.EasyPermission; 11 | import com.zyq.easypermission.EasyPermissionResult; 12 | import com.zyq.easypermission.bean.PermissionAlertInfo; 13 | import com.zyq.easypermission.util.EasyLocationTool; 14 | 15 | import java.util.List; 16 | 17 | /** 18 | * 演示定位服务GPS 19 | * demo for LocationService 20 | * @author Zhang YanQiang 21 | * @date 2022 03 22 22 | */ 23 | public class LocationServiceActivity extends BaseActivity { 24 | @Override 25 | protected void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | setContentView(R.layout.activity_need_reslut); 28 | findViewById(R.id.btnRequest).setOnClickListener(new View.OnClickListener() { 29 | @Override 30 | public void onClick(View v) { 31 | //申请权限 32 | //To apply for permission 33 | requestPermission(); 34 | // openGps(); 35 | } 36 | }); 37 | findViewById(R.id.btnShowResult).setOnClickListener(new View.OnClickListener() { 38 | @Override 39 | public void onClick(View v) { 40 | showResult(); 41 | } 42 | }); 43 | } 44 | 45 | @Override 46 | protected void onResume() { 47 | super.onResume(); 48 | showResult(); 49 | } 50 | 51 | /** 52 | * Display the results 53 | * 展示结果 54 | */ 55 | private void showResult(){ 56 | TextView textView2 = findViewById(R.id.textView2); 57 | if(EasyPermission.build().hasPermission(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION)){ 58 | textView2.setText(EasyLocationTool.isLocationEnabled(mContext)?"定位服务已打开":"定位服务关闭中"); 59 | }else { 60 | textView2.setText("还没有获取到定位权限哦"); 61 | } 62 | } 63 | EasyPermission mEasyPermission; 64 | /** 65 | * To apply for permission 66 | * 申请权限 67 | */ 68 | private void requestPermission(){ 69 | mEasyPermission = EasyPermission.build() 70 | .mPerms(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION) 71 | .mAlertInfo(new PermissionAlertInfo("权限极简需要定位权限","当前功能以及以后许多功能都需要您的位置信息,请允许定位权限。如果不允许将无法使用某些位置相关功能,其它不相关服务不受影响")) 72 | .mResult(new EasyPermissionResult() { 73 | @Override 74 | public void onPermissionsDismiss(int requestCode, @NonNull List permissions) { 75 | super.onPermissionsDismiss(requestCode, permissions); 76 | mToast("权限被拒绝"); 77 | } 78 | 79 | @Override 80 | public void onPermissionsAccess(int requestCode) { 81 | super.onPermissionsAccess(requestCode); 82 | //开启定位服务 83 | //Enabling the Location Service 84 | openGps(); 85 | } 86 | }).requestPermission(); 87 | } 88 | /** 89 | * Enable GPS service 90 | * 打开GPS服务 91 | */ 92 | private void openGps(){ 93 | showResult(); 94 | if(!EasyLocationTool.isLocationEnabled(mContext)){ 95 | //去打开GPS服务 96 | //Turn on the GPS service 97 | EasyLocationTool.gotoAppSettings(mContext); 98 | } 99 | } 100 | 101 | } 102 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyq/permissiondemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.zyq.permissiondemo; 2 | 3 | import android.Manifest; 4 | import android.os.Bundle; 5 | import android.view.View; 6 | import android.widget.Toast; 7 | 8 | import com.zyq.easypermission.EasyPermission; 9 | import com.zyq.easypermission.EasyPermissionHelper; 10 | 11 | /** 12 | * @author Zhang YanQiang 13 | * @date 2019 06 03 14 | */ 15 | public class MainActivity extends BaseActivity { 16 | 17 | @Override 18 | protected void onCreate(Bundle savedInstanceState) { 19 | super.onCreate(savedInstanceState); 20 | setContentView(R.layout.activity_main); 21 | initOnclick(); 22 | } 23 | 24 | private void initOnclick() { 25 | //buttonCheck 只检测权限状态 26 | findViewById(R.id.buttonCheck).setOnClickListener(new View.OnClickListener() { 27 | @Override 28 | public void onClick(View v) { 29 | //demo for only check 30 | //演示只供检查 31 | startOneActivity(ChekOnlyActivity.class); 32 | } 33 | }); 34 | //buttonOnlyApply 仅申请,不需要结果 35 | findViewById(R.id.buttonOnlyApply).setOnClickListener(new View.OnClickListener() { 36 | @Override 37 | public void onClick(View v) { 38 | Toast.makeText(MainActivity.this, "Only requestPermission", Toast.LENGTH_SHORT).show(); 39 | //demo for only apply needn't result 40 | //演示只适用于不需要结果 41 | EasyPermission.build().requestPermission(Manifest.permission.CAMERA); 42 | } 43 | }); 44 | //buttonNeedResult 需要权限申请通过/拒绝回调 45 | findViewById(R.id.buttonNeedResult).setOnClickListener(new View.OnClickListener() { 46 | @Override 47 | public void onClick(View v) { 48 | startOneActivity(NeedReslutActivity.class); 49 | } 50 | }); 51 | //buttonDisMissAsk 需要权限禁止询问回调 52 | findViewById(R.id.buttonDisMissAsk).setOnClickListener(new View.OnClickListener() { 53 | @Override 54 | public void onClick(View v) { 55 | startOneActivity(DismissAskActivity.class); 56 | } 57 | }); 58 | //buttonEndlessly 强制需要权限(慎用) 59 | findViewById(R.id.buttonEndlessly).setOnClickListener(new View.OnClickListener() { 60 | @Override 61 | public void onClick(View v) { 62 | // When you need a permission endlessly. 63 | startOneActivity(EndlessActivity.class); 64 | } 65 | }); 66 | //buttonWithExplain 申请权限附带说明(推荐) 67 | findViewById(R.id.buttonWithExplain).setOnClickListener(new View.OnClickListener() { 68 | @Override 69 | public void onClick(View v) { 70 | // When you need a permission with explain. 71 | startOneActivity(RequestWithExplainActivity.class); 72 | } 73 | }); 74 | //buttonForFloatWindow 通知栏权限 75 | findViewById(R.id.buttonForFloatWindow).setOnClickListener(new View.OnClickListener() { 76 | @Override 77 | public void onClick(View v) { 78 | startOneActivity(FloatWindowActivity.class); 79 | } 80 | }); 81 | //buttonForNotification 悬浮窗权限 82 | findViewById(R.id.buttonForNotification).setOnClickListener(new View.OnClickListener() { 83 | @Override 84 | public void onClick(View v) { 85 | startOneActivity(NotificationActivity.class); 86 | } 87 | }); 88 | //buttonForGps 定位服务 89 | findViewById(R.id.buttonForGps).setOnClickListener(new View.OnClickListener() { 90 | @Override 91 | public void onClick(View v) { 92 | startOneActivity(LocationServiceActivity.class); 93 | } 94 | }); 95 | 96 | } 97 | } 98 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyq/permissiondemo/MyApplication.java: -------------------------------------------------------------------------------- 1 | package com.zyq.permissiondemo; 2 | 3 | import android.app.Application; 4 | import android.view.Gravity; 5 | 6 | import com.zyq.easypermission.EasyPermissionHelper; 7 | import com.zyq.easypermission.bean.EasyAppSettingDialogStyle; 8 | import com.zyq.easypermission.bean.EasyTopAlertStyle; 9 | 10 | /** 11 | * @author by Zhang YanQiang 12 | * @date 2022/3/21. 13 | */ 14 | public class MyApplication extends Application { 15 | @Override 16 | public void onCreate() { 17 | super.onCreate(); 18 | //首次使用权限申请之前完成初始化,建议放在Application onCreate()中完成 19 | EasyPermissionHelper.getInstance().init(this); 20 | 21 | //设置被禁止时说明弹窗的样式,可以不设置 使用默认的STYLE_DEFAULT,STYLE_SYSTEM 22 | //默认样式,可以不用设置 23 | // EasyPermissionHelper.getInstance().setDialogStyle( 24 | // new EasyAppSettingDialogStyle(EasyAppSettingDialogStyle.DialogStyle.STYLE_DEFAULT)); 25 | //系统样式 26 | // EasyPermissionHelper.getInstance().setDialogStyle( 27 | // new EasyAppSettingDialogStyle(EasyAppSettingDialogStyle.DialogStyle.STYLE_SYSTEM)); 28 | //自定义样式 29 | EasyPermissionHelper.getInstance().setDialogStyle( 30 | new EasyAppSettingDialogStyle(EasyAppSettingDialogStyle.DialogStyle.STYLE_CUSTOM) 31 | .setTitleGravity(Gravity.CENTER)//设置居中 32 | .setTitleSize(17)//设置标题样式 33 | .setTitleColor("#333333") 34 | .setMessageSize(14)//设置内容样式 35 | .setMessageColor("#666666") 36 | .setButtonTextSize(14)//设置按钮样式 37 | .setButtonThemeColor("#FF0000") 38 | .setCancelText("取消")//设置按钮文本 39 | .setConfirmText("去打开")); 40 | 41 | //设置申请权限时顶部提示框样式,可以不设置 使用默认的STYLE_DEFAULT 42 | //默认样式,可以不用设置 43 | EasyPermissionHelper.getInstance().setTopAlertStyle( 44 | new EasyTopAlertStyle(EasyTopAlertStyle.AlertStyle.STYLE_DEFAULT)); 45 | //自定义样式 46 | // EasyPermissionHelper.getInstance().setTopAlertStyle( 47 | // new EasyTopAlertStyle(EasyTopAlertStyle.AlertStyle.STYLE_CUSTOM) 48 | // .setTitleGravity(Gravity.LEFT)//默认居左 49 | // .setTitleSize(16)//设置标题样式,默认16sp 50 | // .setTitleColor("#333333") 51 | // .setMessageSize(14)//设置内容样式,默认14sp 52 | // .setMessageColor("#333333") 53 | // .setBackgroundColor("#FFFFFF")//设置背景色,默认白色 54 | // .setBackgroundRadius(8)//设置背景圆角弧度,默认8dp 55 | // .setBackgroundElevation(6)//设置背景阴影范围,默认6dp 56 | // .setTopMargin(10)//设置距离顶部标题栏间距,默认10dp 57 | // .setSideMargin(10));//设置距离屏幕两边宽度,默认10dp 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyq/permissiondemo/NeedReslutActivity.java: -------------------------------------------------------------------------------- 1 | package com.zyq.permissiondemo; 2 | 3 | import android.Manifest; 4 | import android.os.Bundle; 5 | import androidx.annotation.NonNull; 6 | 7 | import android.view.View; 8 | import android.widget.TextView; 9 | 10 | import com.zyq.easypermission.EasyPermission; 11 | import com.zyq.easypermission.EasyPermissionResult; 12 | 13 | import java.util.List; 14 | 15 | /** 16 | * 演示需要结果的 17 | * demo for NeedReslut 18 | * @author Zhang YanQiang 19 | * @date 2019 06 03 20 | */ 21 | public class NeedReslutActivity extends BaseActivity { 22 | TextView textView2; 23 | EasyPermission easyPermission; 24 | 25 | @Override 26 | protected void onCreate(Bundle savedInstanceState) { 27 | super.onCreate(savedInstanceState); 28 | setContentView(R.layout.activity_need_reslut); 29 | textView2 = findViewById(R.id.textView2); 30 | findViewById(R.id.btnRequest).setOnClickListener(new View.OnClickListener() { 31 | @Override 32 | public void onClick(View v) { 33 | easyPermission.requestPermission(); 34 | } 35 | }); 36 | findViewById(R.id.btnShowResult).setOnClickListener(new View.OnClickListener() { 37 | @Override 38 | public void onClick(View v) { 39 | startOneActivity(ChekOnlyActivity.class); 40 | } 41 | }); 42 | //权限请求 43 | easyPermission = EasyPermission.build() 44 | .mRequestCode(RC_CODE_PERMISSION) 45 | // .mContext(NeedReslutActivity.this) 46 | .mPerms(Manifest.permission.CAMERA) 47 | .mResult(new EasyPermissionResult() { 48 | @Override 49 | public void onPermissionsAccess(int requestCode) { 50 | super.onPermissionsAccess(requestCode); 51 | mToast("权限通过"); 52 | //做你想做的 53 | textView2.setText("权限已通过"); 54 | } 55 | 56 | @Override 57 | public void onPermissionsDismiss(int requestCode, @NonNull List permissions) { 58 | super.onPermissionsDismiss(requestCode, permissions); 59 | mToast("权限被拒绝"); 60 | //你的权限被用户拒绝了你怎么办 61 | textView2.setText(permissions.toString() + " 被拒绝了"); 62 | } 63 | }).requestPermission(); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyq/permissiondemo/NotificationActivity.java: -------------------------------------------------------------------------------- 1 | package com.zyq.permissiondemo; 2 | 3 | import android.os.Bundle; 4 | import android.view.View; 5 | import android.widget.TextView; 6 | 7 | import com.zyq.easypermission.util.EasyNotificationTool; 8 | 9 | /** 10 | * 演示通知栏权限 11 | * demo for Notification 12 | * @author Zhang YanQiang 13 | * @date 2022 03 22 14 | */ 15 | public class NotificationActivity extends BaseActivity { 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_need_reslut); 20 | findViewById(R.id.btnRequest).setOnClickListener(new View.OnClickListener() { 21 | @Override 22 | public void onClick(View v) { 23 | EasyNotificationTool.gotoAppSettings(mContext); 24 | } 25 | }); 26 | findViewById(R.id.btnShowResult).setOnClickListener(new View.OnClickListener() { 27 | @Override 28 | public void onClick(View v) { 29 | showResult(); 30 | } 31 | }); 32 | } 33 | 34 | @Override 35 | protected void onResume() { 36 | super.onResume(); 37 | showResult(); 38 | } 39 | private void showResult(){ 40 | TextView textView2 = findViewById(R.id.textView2); 41 | textView2.setText(EasyNotificationTool.isNotificationEnabled(mContext)?"已通过":"未通过"); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /app/src/main/java/com/zyq/permissiondemo/RequestWithExplainActivity.java: -------------------------------------------------------------------------------- 1 | package com.zyq.permissiondemo; 2 | 3 | import android.Manifest; 4 | import android.content.Intent; 5 | import android.os.Bundle; 6 | import androidx.annotation.NonNull; 7 | 8 | import android.view.View; 9 | import android.widget.TextView; 10 | 11 | import com.zyq.easypermission.EasyPermission; 12 | import com.zyq.easypermission.EasyPermissionHelper; 13 | import com.zyq.easypermission.EasyPermissionLog; 14 | import com.zyq.easypermission.EasyPermissionResult; 15 | import com.zyq.easypermission.bean.PermissionAlertInfo; 16 | 17 | import java.util.List; 18 | 19 | /** 20 | * 演示带解释说明申请权限 21 | * demo for Request With Explain 22 | * 23 | * @author Zhang YanQiang 24 | * @date 2022 03 21 25 | */ 26 | public class RequestWithExplainActivity extends BaseActivity { 27 | TextView textView2; 28 | EasyPermission easyPermission; 29 | 30 | @Override 31 | protected void onCreate(Bundle savedInstanceState) { 32 | super.onCreate(savedInstanceState); 33 | setContentView(R.layout.activity_need_reslut); 34 | textView2 = findViewById(R.id.textView2); 35 | findViewById(R.id.btnRequest).setOnClickListener(new View.OnClickListener() { 36 | @Override 37 | public void onClick(View v) { 38 | easyPermission.requestPermission(); 39 | } 40 | }); 41 | findViewById(R.id.btnShowResult).setOnClickListener(new View.OnClickListener() { 42 | @Override 43 | public void onClick(View v) { 44 | startOneActivity(ChekOnlyActivity.class); 45 | } 46 | }); 47 | //设置权限描述 48 | PermissionAlertInfo alertInfo = new PermissionAlertInfo("**需要申请摄像头权限", 49 | "**需要申请摄像头拍摄权限,以便您能够通过扫一扫实现扫描二维码;通过拍照更换您帐号的头像;拍照上传一些注册帐号需要的证件信息。拒绝或取消授权将影响以上功能,不影响使用其他服务"); 50 | easyPermission = EasyPermission.build() 51 | .mRequestCode(RC_CODE_PERMISSION) 52 | .mAlertInfo(alertInfo) 53 | .setAutoOpenAppDetails(true) 54 | .mPerms(Manifest.permission.CAMERA) 55 | .mResult(new EasyPermissionResult() { 56 | @Override 57 | public void onPermissionsAccess(int requestCode) { 58 | super.onPermissionsAccess(requestCode); 59 | mToast("权限已通过"); 60 | //做你想做的 61 | textView2.setText("权限已通过"); 62 | } 63 | 64 | @Override 65 | public void onPermissionsDismiss(int requestCode, @NonNull List permissions) { 66 | super.onPermissionsDismiss(requestCode, permissions); 67 | mToast("权限被拒绝"); 68 | //你的权限被用户拒绝了你怎么办 69 | textView2.setText(permissions.toString() + " 被拒绝了"); 70 | } 71 | 72 | @Override 73 | public boolean onDismissAsk(int requestCode, @NonNull List permissions) { 74 | mToast("权限被禁止"); 75 | //你的权限被用户禁止了并且不能请求了你怎么办,提示去系统-设置打开 76 | textView2.setText(permissions.toString() + " 被禁止了,需要向用户说明情况"); 77 | return super.onDismissAsk(requestCode,permissions);//这里true表示拦截处理,不再回调onPermissionsDismiss; 78 | } 79 | @Override 80 | public void openAppDetails() { 81 | //弹出默认的权限详情设置提示弹出框,在设置页完成允许操作后,会自动回调到onPermissionsAccess() 82 | super.openAppDetails(); 83 | //如果样式不满意,可以弹出自定义明弹窗,在用户确认时调用easyPermission.goToAppSettings();完成跳转设置页 84 | } 85 | }).requestPermission(); 86 | } 87 | 88 | @Override 89 | protected void onNewIntent(Intent intent) { 90 | super.onNewIntent(intent); 91 | EasyPermissionLog.d(mContext.getLocalClassName()+": onNewIntent"); 92 | //launchMode="singleTask" onNewIntent中如果需要请求权限,需要重新设置activity 93 | //注意:这里并不是说必须在onNewIntent这样做,而是非要在onNewIntent执行权限请求的话,才需要设置mContext。 94 | //正常情况下在onCreate和onResume中不需要设置 95 | //方式一: 96 | EasyPermissionHelper.getInstance().updateTopActivity(mContext); 97 | easyPermission.requestPermission(); 98 | // //方式二: 99 | // easyPermission.mContext(mContext).requestPermission(); 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_chekonly.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 15 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 13 | 17 |