├── .gitignore ├── .travis.yml ├── LICENSE.txt ├── README.md ├── README_EN.md ├── app-debug.apk ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── joker │ │ └── permissions4m │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── joker │ │ │ └── permissions4m │ │ │ ├── MainActivity.java │ │ │ ├── NormalFragment.java │ │ │ ├── SupportFragment.java │ │ │ └── other │ │ │ ├── App.java │ │ │ └── ToastUtil.java │ └── res │ │ ├── layout │ │ ├── activity_main.xml │ │ ├── activity_second.xml │ │ ├── activity_single.xml │ │ ├── fragment_normal.xml │ │ └── fragment_support.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 │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── joker │ └── permissions4m │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── permissions4m-annotation ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── joker │ └── annotation │ ├── PermissionsCustomRationale.java │ ├── PermissionsDenied.java │ ├── PermissionsGranted.java │ ├── PermissionsNonRationale.java │ ├── PermissionsRationale.java │ └── PermissionsRequestSync.java ├── permissions4m-api ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ └── java │ └── com │ └── joker │ └── api │ ├── Permissions4M.java │ ├── apply │ ├── ForceApplyPermissions.java │ ├── NormalApplyPermissions.java │ ├── PermissionsChecker.java │ └── util │ │ ├── AudioRecordManager.java │ │ └── SupportUtil.java │ ├── support │ ├── ManufacturerSupportUtil.java │ ├── PermissionsPageManager.java │ └── manufacturer │ │ ├── HUAWEI.java │ │ ├── MEIZU.java │ │ ├── OPPO.java │ │ ├── PermissionsPage.java │ │ ├── Protogenesis.java │ │ ├── VIVO.java │ │ └── XIAOMI.java │ └── wrapper │ ├── AbstractWrapper.java │ ├── ActivityWrapper.java │ ├── AnnotationWrapper.java │ ├── FragmentBaseWrapper.java │ ├── FragmentWrapper.java │ ├── ListenerWrapper.java │ ├── PermissionWrapper.java │ ├── SupportFragmentWrapper.java │ └── Wrapper.java ├── permissions4m-processor ├── .gitignore ├── build.gradle └── src │ └── main │ └── java │ └── com │ └── joker │ └── processor │ ├── AnnotationProcessor.java │ └── ProxyInfo.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea 5 | .DS_Store 6 | /build 7 | /captures 8 | .externalNativeBuild 9 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: android 2 | 3 | android: 4 | components: 5 | - tools 6 | - platform-tools 7 | - build-tools-25.0.2 8 | - android-25 9 | - extra-android-m2repository 10 | 11 | jdk: 12 | - oraclejdk8 13 | 14 | notifications: 15 | email: false 16 | 17 | install: 18 | - travis_wait 30 mvn install 19 | 20 | before_script: 21 | - chmod +x gradlew 22 | - $ANDROID_HOME/tools/bin/sdkmanager "extras;m2repository;com;android;support;constraint;constraint-layout;1.0.2" 23 | - $ANDROID_HOME/tools/bin/sdkmanager "extras;m2repository;com;android;support;constraint;constraint-layout-solver;1.0.2" 24 | 25 | script: 26 | - ./gradlew assemble check -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 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 requestCode, 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 requestCode, 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 requestCode 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 | 203 | -------------------------------------------------------------------------------- /app-debug.apk: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jokermonn/permissions4m/cc4996efc7b2f26d13465764eb5e4dc28a5a5640/app-debug.apk -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.2" 6 | defaultConfig { 7 | applicationId "com.joker.permissions4m" 8 | minSdkVersion 21 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | lintOptions { 21 | abortOnError false 22 | } 23 | } 24 | 25 | dependencies { 26 | compile fileTree(dir: 'libs', include: ['*.jar']) 27 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 28 | exclude group: 'com.android.support', module: 'support-annotations' 29 | }) 30 | // umeng 31 | // annotationProcessor 'com.github.jokermonn:permissions4m:2.1.1-processor' 32 | // compile 'com.github.jokermonn:permissions4m:2.1.1-lib' 33 | compile project(':permissions4m-api') 34 | annotationProcessor project(':permissions4m-processor') 35 | compile 'com.umeng.analytics:analytics:6.1.2' 36 | compile 'com.android.support:appcompat-v7:25.3.1' 37 | compile 'com.android.support.constraint:constraint-layout:1.0.2' 38 | compile 'com.android.support:support-v4:25.3.1' 39 | testCompile 'junit:junit:4.12' 40 | } -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in F:\sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | 19 | # Uncomment this to preserve the line number information for 20 | # debugging stack traces. 21 | #-keepattributes SourceFile,LineNumberTable 22 | 23 | # If you keep the line number information, uncomment this to 24 | # hide the original source file name. 25 | #-renamesourcefileattribute SourceFile 26 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/joker/permissions4m/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.joker.permissions4m; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation 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() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.joker.permissions4m", 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 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 64 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /app/src/main/java/com/joker/permissions4m/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.joker.permissions4m; 2 | 3 | import android.Manifest; 4 | import android.content.DialogInterface; 5 | import android.content.Intent; 6 | import android.os.Bundle; 7 | import android.support.annotation.NonNull; 8 | import android.support.v7.app.AlertDialog; 9 | import android.support.v7.app.AppCompatActivity; 10 | import android.util.Log; 11 | import android.view.View; 12 | import android.widget.Button; 13 | 14 | import com.joker.annotation.PermissionsCustomRationale; 15 | import com.joker.annotation.PermissionsDenied; 16 | import com.joker.annotation.PermissionsGranted; 17 | import com.joker.annotation.PermissionsNonRationale; 18 | import com.joker.annotation.PermissionsRationale; 19 | import com.joker.annotation.PermissionsRequestSync; 20 | import com.joker.api.Permissions4M; 21 | import com.joker.api.support.PermissionsPageManager; 22 | import com.joker.api.wrapper.ListenerWrapper; 23 | import com.joker.api.wrapper.Wrapper; 24 | import com.joker.permissions4m.other.ToastUtil; 25 | 26 | import static com.joker.permissions4m.MainActivity.CALENDAR_CODE; 27 | import static com.joker.permissions4m.MainActivity.LOCATION_CODE; 28 | import static com.joker.permissions4m.MainActivity.SENSORS_CODE; 29 | 30 | @PermissionsRequestSync(permission = {Manifest.permission.BODY_SENSORS, Manifest.permission 31 | .ACCESS_FINE_LOCATION, Manifest.permission.READ_CALENDAR}, 32 | value = {SENSORS_CODE, LOCATION_CODE, CALENDAR_CODE}) 33 | public class MainActivity extends AppCompatActivity { 34 | public static final int CALENDAR_CODE = 7; 35 | public static final int SENSORS_CODE = 8; 36 | public static final int LOCATION_CODE = 9; 37 | private static final int CALL_LOG_CODE = 2; 38 | private static final int AUDIO_CODE = 6; 39 | private static final int READ_CONTACTS_CODE = 10; 40 | private Button mCallButton; 41 | private Button mAudioButton; 42 | private Button mOneButton; 43 | private Button mManagerButton; 44 | private Button mPermissionPageButton; 45 | private Button mPageListenerButton; 46 | 47 | @Override 48 | protected void onCreate(Bundle savedInstanceState) { 49 | super.onCreate(savedInstanceState); 50 | setContentView(R.layout.activity_main); 51 | mCallButton = (Button) findViewById(R.id.btn_calendar); 52 | mAudioButton = (Button) findViewById(R.id.btn_audio); 53 | mOneButton = (Button) findViewById(R.id.btn_one); 54 | mManagerButton = (Button) findViewById(R.id.btn_manager); 55 | mPermissionPageButton = (Button) findViewById(R.id.btn_permission_page); 56 | mPageListenerButton = (Button) findViewById(R.id.btn_page_listener); 57 | 58 | // 通话记录 59 | mCallButton.setOnClickListener(new View.OnClickListener() { 60 | @Override 61 | public void onClick(View v) { 62 | Permissions4M.get(MainActivity.this) 63 | .requestPermissions(Manifest.permission.READ_CALL_LOG) 64 | .requestCodes(CALL_LOG_CODE) 65 | .requestPageType(Permissions4M.PageType.MANAGER_PAGE) 66 | .request(); 67 | } 68 | }); 69 | 70 | // 录音 71 | mAudioButton.setOnClickListener(new View.OnClickListener() { 72 | @Override 73 | public void onClick(View v) { 74 | Permissions4M.get(MainActivity.this) 75 | .requestPermissions(Manifest.permission.RECORD_AUDIO) 76 | .requestCodes(AUDIO_CODE) 77 | .requestPageType(Permissions4M.PageType.ANDROID_SETTING_PAGE) 78 | .request(); 79 | } 80 | }); 81 | 82 | // 一键申请 83 | mOneButton.setOnClickListener(new View.OnClickListener() { 84 | @Override 85 | public void onClick(View v) { 86 | Permissions4M 87 | .get(MainActivity.this) 88 | .requestSync(); 89 | } 90 | }); 91 | 92 | mManagerButton.setOnClickListener(new View.OnClickListener() { 93 | @Override 94 | public void onClick(View v) { 95 | startActivity(PermissionsPageManager.getIntent(MainActivity.this)); 96 | } 97 | }); 98 | 99 | mPermissionPageButton.setOnClickListener(new View.OnClickListener() { 100 | @Override 101 | public void onClick(View v) { 102 | startActivity(PermissionsPageManager.getSettingIntent(MainActivity.this)); 103 | } 104 | }); 105 | 106 | mPageListenerButton.setOnClickListener(new View.OnClickListener() { 107 | @Override 108 | public void onClick(View v) { 109 | Permissions4M.get(MainActivity.this) 110 | .requestPermissions(Manifest.permission.READ_CONTACTS) 111 | .requestCodes(READ_CONTACTS_CODE) 112 | .requestListener(new ListenerWrapper.PermissionRequestListener() { 113 | @Override 114 | public void permissionGranted(int code) { 115 | ToastUtil.show("读取通讯录权限成功 in activity with listener"); 116 | } 117 | 118 | @Override 119 | public void permissionDenied(int code) { 120 | ToastUtil.show("读取通讯录权失败 in activity with listener"); 121 | } 122 | 123 | @Override 124 | public void permissionRationale(int code) { 125 | ToastUtil.show("请打开读取通讯录权限 in activity with listener"); 126 | } 127 | }) 128 | .requestPageType(Permissions4M.PageType.MANAGER_PAGE) 129 | .requestPage(new Wrapper.PermissionPageListener() { 130 | @Override 131 | public void pageIntent(int code, final Intent intent) { 132 | new AlertDialog.Builder(MainActivity.this) 133 | .setMessage("用户您好,我们需要您开启读取通讯录权限申请:\n请点击前往设置页面\n(in activity with" + 134 | " listener)") 135 | .setPositiveButton("前往设置页面", new DialogInterface.OnClickListener() { 136 | @Override 137 | public void onClick(DialogInterface dialog, int which) { 138 | startActivity(intent); 139 | } 140 | }) 141 | .setNegativeButton("取消", new DialogInterface.OnClickListener() { 142 | @Override 143 | public void onClick(DialogInterface dialog, int which) { 144 | dialog.dismiss(); 145 | } 146 | }) 147 | .show(); 148 | } 149 | }) 150 | .request(); 151 | } 152 | }); 153 | } 154 | 155 | @Override 156 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] 157 | grantResults) { 158 | Permissions4M.onRequestPermissionsResult(MainActivity.this, requestCode, grantResults); 159 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 160 | } 161 | 162 | //==================================================================== 163 | @PermissionsGranted({LOCATION_CODE, SENSORS_CODE, CALENDAR_CODE}) 164 | public void syncGranted(int code) { 165 | switch (code) { 166 | case LOCATION_CODE: 167 | ToastUtil.show("地理位置权限授权成功 in activity with annotation"); 168 | Log.d("TAG", "syncGranted: 地理位置权限授权成功 in activity with annotation"); 169 | break; 170 | case SENSORS_CODE: 171 | ToastUtil.show("传感器权限授权成功 in activity with annotation"); 172 | Log.d("TAG", "syncGranted: 传感器权限授权成功 in activity with annotation"); 173 | break; 174 | case CALENDAR_CODE: 175 | ToastUtil.show("读取日历权限授权成功 in activity with annotation"); 176 | Log.d("TAG", "syncGranted: 读取日历权限授权成功 in activity with annotation"); 177 | break; 178 | default: 179 | break; 180 | } 181 | } 182 | 183 | @PermissionsDenied({LOCATION_CODE, SENSORS_CODE, CALENDAR_CODE}) 184 | public void syncDenied(int code) { 185 | switch (code) { 186 | case LOCATION_CODE: 187 | ToastUtil.show("地理位置权限授权失败 in activity with annotation"); 188 | Log.d("TAG", "syncDenied: 地理位置权限授权失败 in activity with annotation"); 189 | break; 190 | case SENSORS_CODE: 191 | ToastUtil.show("传感器权限授权失败 in activity with annotation"); 192 | Log.d("TAG", "syncDenied: 传感器权限授权失败 in activity with annotation"); 193 | break; 194 | case CALENDAR_CODE: 195 | ToastUtil.show("读取日历权限授权失败 in activity with annotation"); 196 | Log.d("TAG", "syncDenied: 读取日历权限授权失败 in activity with annotation"); 197 | break; 198 | default: 199 | break; 200 | } 201 | } 202 | 203 | @PermissionsRationale({LOCATION_CODE, SENSORS_CODE, CALENDAR_CODE}) 204 | public void syncRationale(int code) { 205 | switch (code) { 206 | case LOCATION_CODE: 207 | ToastUtil.show("请开启地理位置权限 in activity with annotation"); 208 | Log.d("TAG", "syncRationale: 请开启地理位置权限 in activity with annotation"); 209 | break; 210 | case SENSORS_CODE: 211 | ToastUtil.show("请开启传感器权限 in activity with annotation"); 212 | Log.d("TAG", "syncRationale: 请开启传感器权限 in activity with annotation"); 213 | break; 214 | case CALENDAR_CODE: 215 | ToastUtil.show("请开启读取日历权限 in activity with annotation"); 216 | Log.d("TAG", "syncRationale: 请开启读取日历权限 in activity with annotation"); 217 | break; 218 | default: 219 | break; 220 | } 221 | } 222 | 223 | //==================================================================== 224 | @PermissionsGranted(CALL_LOG_CODE) 225 | public void storageAndCallGranted() { 226 | ToastUtil.show("读取通话记录权限授权成功 in activity with annotation"); 227 | } 228 | 229 | @PermissionsDenied(CALL_LOG_CODE) 230 | public void storageAndCallDenied() { 231 | ToastUtil.show("读取通话记录权限授权失败 in activity with annotation"); 232 | } 233 | 234 | @PermissionsRationale(CALL_LOG_CODE) 235 | public void storageAndCallNonRationale() { 236 | ToastUtil.show("请开启读取通话记录权限授权 in activity with annotation"); 237 | } 238 | 239 | //==================================================================== 240 | @PermissionsGranted(AUDIO_CODE) 241 | public void smsAndAudioGranted() { 242 | ToastUtil.show("录音权限申请成功 in activity with annotation"); 243 | } 244 | 245 | @PermissionsDenied(AUDIO_CODE) 246 | public void smsAndAudioDenied() { 247 | ToastUtil.show("录音权限申请失败 in activity with annotation"); 248 | } 249 | 250 | @PermissionsCustomRationale(AUDIO_CODE) 251 | public void smsAndAudioCustomRationale() { 252 | new AlertDialog.Builder(this) 253 | .setMessage("录音权限申请:\n我们需要您开启录音权限(in activity with annotation)") 254 | .setPositiveButton("确定", new DialogInterface.OnClickListener() { 255 | @Override 256 | public void onClick(DialogInterface dialog, int which) { 257 | Permissions4M.get(MainActivity.this) 258 | .requestOnRationale() 259 | .requestPermissions(Manifest.permission.RECORD_AUDIO) 260 | .requestCodes(AUDIO_CODE) 261 | .request(); 262 | } 263 | }) 264 | .show(); 265 | } 266 | 267 | //=================================================================== 268 | @PermissionsNonRationale({AUDIO_CODE, CALL_LOG_CODE}) 269 | public void storageAndCallRationale(int code, final Intent intent) { 270 | switch (code) { 271 | case AUDIO_CODE: 272 | new AlertDialog.Builder(MainActivity.this) 273 | .setMessage("用户您好,我们需要您开启读取录音权限\n请点击前往设置页面\n(in activity with listener)") 274 | .setPositiveButton("确定", new DialogInterface.OnClickListener() { 275 | @Override 276 | public void onClick(DialogInterface dialog, int which) { 277 | startActivity(intent); 278 | } 279 | }) 280 | .setNegativeButton("取消", new DialogInterface.OnClickListener() { 281 | @Override 282 | public void onClick(DialogInterface dialog, int which) { 283 | dialog.cancel(); 284 | } 285 | }) 286 | .show(); 287 | break; 288 | case CALL_LOG_CODE: 289 | new AlertDialog.Builder(MainActivity.this) 290 | .setMessage("用户您好,我们需要您开启读取通话记录权限\n请点击前往设置页面\n(in activity with listener)") 291 | .setPositiveButton("确定", new DialogInterface.OnClickListener() { 292 | @Override 293 | public void onClick(DialogInterface dialog, int which) { 294 | startActivity(intent); 295 | } 296 | }) 297 | .setNegativeButton("取消", new DialogInterface.OnClickListener() { 298 | @Override 299 | public void onClick(DialogInterface dialog, int which) { 300 | dialog.cancel(); 301 | } 302 | }) 303 | .show(); 304 | break; 305 | default: 306 | break; 307 | } 308 | } 309 | } 310 | -------------------------------------------------------------------------------- /app/src/main/java/com/joker/permissions4m/NormalFragment.java: -------------------------------------------------------------------------------- 1 | package com.joker.permissions4m; 2 | 3 | 4 | import android.Manifest; 5 | import android.app.Fragment; 6 | import android.content.DialogInterface; 7 | import android.content.Intent; 8 | import android.os.Bundle; 9 | import android.support.annotation.NonNull; 10 | import android.support.v7.app.AlertDialog; 11 | import android.util.Log; 12 | import android.view.LayoutInflater; 13 | import android.view.View; 14 | import android.view.ViewGroup; 15 | import android.widget.Button; 16 | 17 | import com.joker.annotation.PermissionsCustomRationale; 18 | import com.joker.annotation.PermissionsDenied; 19 | import com.joker.annotation.PermissionsGranted; 20 | import com.joker.annotation.PermissionsNonRationale; 21 | import com.joker.api.Permissions4M; 22 | import com.joker.api.wrapper.Wrapper; 23 | import com.joker.permissions4m.other.ToastUtil; 24 | 25 | /** 26 | * A simple {@link Fragment} subclass. 27 | */ 28 | public class NormalFragment extends Fragment { 29 | private static final int SENSORS_CODE = 700; 30 | private static final int LOCATION_CODE = 800; 31 | private static final int CALENDAR_CODE = 900; 32 | private static final int SMS_CODE = 500; 33 | private static final int AUDIO_CODE = 600; 34 | private static final int PHONE_STATE_CODE = 1000; 35 | private Button mSmsButton; 36 | private Button mOneButton; 37 | private Button mStateButton; 38 | 39 | public NormalFragment() { 40 | // Required empty public constructor 41 | } 42 | 43 | @Override 44 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 45 | Bundle savedInstanceState) { 46 | View view = inflater.inflate(R.layout.fragment_normal, container, false); 47 | mSmsButton = (Button) view.findViewById(R.id.btn_sms); 48 | mOneButton = (Button) view.findViewById(R.id.btn_one); 49 | mStateButton = (Button) view.findViewById(R.id.btn_state); 50 | 51 | // 短信申请 52 | mSmsButton.setOnClickListener(new View.OnClickListener() { 53 | @Override 54 | public void onClick(View v) { 55 | Permissions4M.get(NormalFragment.this) 56 | .requestPermissions(Manifest.permission.READ_SMS) 57 | .requestCodes(SMS_CODE) 58 | .requestPageType(Permissions4M.PageType.ANDROID_SETTING_PAGE) 59 | .request(); 60 | } 61 | }); 62 | 63 | // 同步申请 64 | mOneButton.setOnClickListener(new View.OnClickListener() { 65 | @Override 66 | public void onClick(View v) { 67 | Permissions4M.get(NormalFragment.this) 68 | .requestPermissions(Manifest.permission.BODY_SENSORS, Manifest.permission 69 | .ACCESS_FINE_LOCATION, Manifest.permission.READ_CALENDAR) 70 | .requestCodes(SENSORS_CODE, LOCATION_CODE, CALENDAR_CODE) 71 | .requestListener(new Wrapper.PermissionRequestListener() { 72 | @Override 73 | public void permissionGranted(int code) { 74 | switch (code) { 75 | case LOCATION_CODE: 76 | ToastUtil.show("地理位置权限授权成功 in fragment with annotation"); 77 | Log.e("TAG", "permissionGranted: 地理位置权限授权成功 "); 78 | break; 79 | case SENSORS_CODE: 80 | ToastUtil.show("传感器权限授权成功 in fragment with annotation"); 81 | Log.e("TAG", "permissionGranted: 传感器权限授权成功 "); 82 | break; 83 | case CALENDAR_CODE: 84 | ToastUtil.show("读取日历权限授权成功 in fragment with annotation"); 85 | Log.e("TAG", "permissionGranted: 读取日历权限授权成功 "); 86 | break; 87 | default: 88 | break; 89 | } 90 | } 91 | 92 | @Override 93 | public void permissionDenied(int code) { 94 | switch (code) { 95 | case LOCATION_CODE: 96 | ToastUtil.show("地理位置权限授权失败 in fragment with annotation"); 97 | Log.e("TAG", "permissionDenied: 地理位置权限授权失败 "); 98 | break; 99 | case SENSORS_CODE: 100 | ToastUtil.show("传感器权限授权失败 in fragment with annotation"); 101 | Log.e("TAG", "permissionDenied: 传感器权限授权失败 "); 102 | break; 103 | case CALENDAR_CODE: 104 | ToastUtil.show("读取日历权限授权失败 in fragment with annotation"); 105 | Log.e("TAG", "permissionDenied: 读取日历权限授权失败 "); 106 | break; 107 | default: 108 | break; 109 | } 110 | } 111 | 112 | @Override 113 | public void permissionRationale(int code) { 114 | switch (code) { 115 | case LOCATION_CODE: 116 | ToastUtil.show("请开启地理位置权限 in fragment with annotation"); 117 | Log.e("TAG", "permissionRationale: 请开启地理位置权限 "); 118 | break; 119 | case SENSORS_CODE: 120 | ToastUtil.show("请开启传感器权限 in fragment with annotation"); 121 | Log.e("TAG", "permissionRationale: 请开启传感器权限 "); 122 | break; 123 | case CALENDAR_CODE: 124 | ToastUtil.show("请开启读取日历权限 in fragment with annotation"); 125 | Log.e("TAG", "permissionRationale: 请开启读取日历权限 "); 126 | break; 127 | default: 128 | break; 129 | } 130 | } 131 | }) 132 | .requestCustomRationaleListener(new Wrapper.PermissionCustomRationaleListener() { 133 | @Override 134 | public void permissionCustomRationale(int code) { 135 | switch (code) { 136 | case LOCATION_CODE: 137 | ToastUtil.show("请开启地理位置权限 in fragment with annotation"); 138 | Log.e("TAG", "permissionRationale: 请开启地理位置权限 "); 139 | 140 | new AlertDialog.Builder(getActivity()) 141 | .setMessage("地理位置权限权限申请:\n我们需要您开启地理位置权限(in fragment with " + 142 | "annotation)") 143 | .setPositiveButton("确定", new DialogInterface 144 | .OnClickListener() { 145 | @Override 146 | public void onClick(DialogInterface dialog, int which) { 147 | Permissions4M.get(NormalFragment.this) 148 | .requestOnRationale() 149 | .requestPermissions(Manifest.permission 150 | .ACCESS_FINE_LOCATION) 151 | .requestCodes(LOCATION_CODE) 152 | .request(); 153 | } 154 | }) 155 | .show(); 156 | break; 157 | case SENSORS_CODE: 158 | ToastUtil.show("请开启传感器权限 in fragment with annotation"); 159 | Log.e("TAG", "permissionRationale: 请开启传感器权限 "); 160 | 161 | new AlertDialog.Builder(getActivity()) 162 | .setMessage("传感器权限申请:\n我们需要您开启传感器权限(in fragment with " + 163 | "annotation)") 164 | .setPositiveButton("确定", new DialogInterface 165 | .OnClickListener() { 166 | @Override 167 | public void onClick(DialogInterface dialog, int which) { 168 | Permissions4M.get(NormalFragment.this) 169 | .requestOnRationale() 170 | .requestPermissions(Manifest.permission 171 | .BODY_SENSORS) 172 | .requestCodes(SENSORS_CODE) 173 | .request(); 174 | } 175 | }) 176 | .show(); 177 | break; 178 | case CALENDAR_CODE: 179 | ToastUtil.show("请开启读取日历权限 in fragment with annotation"); 180 | Log.e("TAG", "permissionRationale: 请开启读取日历权限 "); 181 | 182 | new AlertDialog.Builder(getActivity()) 183 | .setMessage("日历权限申请:\n我们需要您开启日历权限(in fragment with " + 184 | "annotation)") 185 | .setPositiveButton("确定", new DialogInterface 186 | .OnClickListener() { 187 | @Override 188 | public void onClick(DialogInterface dialog, int which) { 189 | Permissions4M.get(NormalFragment.this) 190 | .requestOnRationale() 191 | .requestPermissions(Manifest.permission 192 | .READ_CALENDAR) 193 | .requestCodes(CALENDAR_CODE) 194 | .request(); 195 | } 196 | }) 197 | .show(); 198 | break; 199 | default: 200 | break; 201 | } 202 | 203 | } 204 | }) 205 | .request(); 206 | } 207 | }); 208 | 209 | mStateButton.setOnClickListener(new View.OnClickListener() { 210 | @Override 211 | public void onClick(View v) { 212 | Permissions4M.get(NormalFragment.this) 213 | .requestPermissions(Manifest.permission.READ_PHONE_STATE) 214 | .requestCodes(PHONE_STATE_CODE) 215 | .requestListener(new Wrapper.PermissionRequestListener() { 216 | @Override 217 | public void permissionGranted(int code) { 218 | ToastUtil.show("读取手机状态权限成功 in activity with listener"); 219 | } 220 | 221 | @Override 222 | public void permissionDenied(int code) { 223 | ToastUtil.show("读取手机状态权失败 in activity with listener"); 224 | } 225 | 226 | @Override 227 | public void permissionRationale(int code) { 228 | ToastUtil.show("请打开读取手机状态权限 in activity with listener"); 229 | } 230 | }) 231 | .requestPageType(Permissions4M.PageType.MANAGER_PAGE) 232 | .requestPage(new Wrapper.PermissionPageListener() { 233 | @Override 234 | public void pageIntent(int code, final Intent intent) { 235 | new AlertDialog.Builder(getActivity()) 236 | .setMessage("用户您好,我们需要您开启读取手机状态权限:\n请点击前往设置页面\n(in activity with " + 237 | "listener)") 238 | .setPositiveButton("前往设置页面", new DialogInterface.OnClickListener() { 239 | @Override 240 | public void onClick(DialogInterface dialog, int which) { 241 | startActivity(intent); 242 | } 243 | }) 244 | .setNegativeButton("取消", new DialogInterface.OnClickListener() { 245 | @Override 246 | public void onClick(DialogInterface dialog, int which) { 247 | dialog.dismiss(); 248 | } 249 | }) 250 | .show(); 251 | } 252 | }) 253 | .requestCustomRationaleListener(new Wrapper.PermissionCustomRationaleListener() { 254 | @Override 255 | public void permissionCustomRationale(int code) { 256 | new AlertDialog.Builder(getActivity()) 257 | .setMessage("手机状态权限申请:\n我们需要您开启手机状态权限(in fragment with annotation)") 258 | .setPositiveButton("确定", new DialogInterface.OnClickListener() { 259 | @Override 260 | public void onClick(DialogInterface dialog, int which) { 261 | Permissions4M.get(NormalFragment.this) 262 | .requestOnRationale() 263 | .requestPermissions(Manifest.permission 264 | .READ_PHONE_STATE) 265 | .requestCodes(PHONE_STATE_CODE) 266 | .request(); 267 | } 268 | }) 269 | .show(); 270 | } 271 | }) 272 | .request(); 273 | } 274 | }); 275 | 276 | return view; 277 | } 278 | 279 | @Override 280 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] 281 | grantResults) { 282 | Permissions4M.onRequestPermissionsResult(NormalFragment.this, requestCode, grantResults); 283 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 284 | } 285 | 286 | //==================================================================== 287 | @PermissionsGranted({SMS_CODE, AUDIO_CODE}) 288 | public void smsAndAudioGranted(int code) { 289 | switch (code) { 290 | case SMS_CODE: 291 | ToastUtil.show("短信权限申请成功 in fragment with annotation"); 292 | break; 293 | case AUDIO_CODE: 294 | ToastUtil.show("录音权限申请成功 in fragment with annotation"); 295 | break; 296 | default: 297 | break; 298 | } 299 | } 300 | 301 | @PermissionsDenied({SMS_CODE, AUDIO_CODE}) 302 | public void smsAndAudioDenied(int code) { 303 | switch (code) { 304 | case SMS_CODE: 305 | ToastUtil.show("短信权限申请失败 in fragment with annotation"); 306 | break; 307 | case AUDIO_CODE: 308 | ToastUtil.show("录音权限申请失败 in fragment with annotation"); 309 | break; 310 | default: 311 | break; 312 | } 313 | } 314 | 315 | @PermissionsCustomRationale({SMS_CODE, AUDIO_CODE}) 316 | public void smsAndAudioCustomRationale(int code) { 317 | switch (code) { 318 | case SMS_CODE: 319 | new AlertDialog.Builder(getActivity()) 320 | .setMessage("短信权限申请:\n我们需要您开启短信权限(in fragment with annotation)") 321 | .setPositiveButton("确定", new DialogInterface.OnClickListener() { 322 | @Override 323 | public void onClick(DialogInterface dialog, int which) { 324 | Permissions4M.get(NormalFragment.this) 325 | .requestOnRationale() 326 | .requestPermissions(Manifest.permission.READ_SMS) 327 | .requestCodes(SMS_CODE) 328 | .request(); 329 | } 330 | }) 331 | .show(); 332 | break; 333 | case AUDIO_CODE: 334 | new AlertDialog.Builder(getActivity()) 335 | .setMessage("录音权限申请:\n我们需要您开启录音权限(in fragment with annotation)") 336 | .setPositiveButton("确定", new DialogInterface.OnClickListener() { 337 | @Override 338 | public void onClick(DialogInterface dialog, int which) { 339 | Permissions4M.get(NormalFragment.this) 340 | .requestOnRationale() 341 | .requestPermissions(Manifest.permission.RECORD_AUDIO) 342 | .requestCodes(AUDIO_CODE) 343 | .request(); 344 | } 345 | }) 346 | .show(); 347 | break; 348 | default: 349 | 350 | break; 351 | } 352 | } 353 | 354 | @PermissionsNonRationale({SMS_CODE}) 355 | public void nonRationale(int code, final Intent intent) { 356 | switch (code) { 357 | case SMS_CODE: 358 | new AlertDialog.Builder(getActivity()) 359 | .setMessage("用户您好,我们需要您开启读取短信权限申请:\n请点击前往设置页面\n(in fragment with annotation)") 360 | .setPositiveButton("确定", new DialogInterface.OnClickListener() { 361 | @Override 362 | public void onClick(DialogInterface dialog, int which) { 363 | startActivity(intent); 364 | } 365 | }) 366 | .setNegativeButton("取消", new DialogInterface.OnClickListener() { 367 | @Override 368 | public void onClick(DialogInterface dialog, int which) { 369 | dialog.dismiss(); 370 | } 371 | }) 372 | .show(); 373 | break; 374 | } 375 | } 376 | } 377 | -------------------------------------------------------------------------------- /app/src/main/java/com/joker/permissions4m/SupportFragment.java: -------------------------------------------------------------------------------- 1 | package com.joker.permissions4m; 2 | 3 | 4 | import android.Manifest; 5 | import android.content.DialogInterface; 6 | import android.content.Intent; 7 | import android.os.Bundle; 8 | import android.support.annotation.NonNull; 9 | import android.support.v4.app.Fragment; 10 | import android.support.v7.app.AlertDialog; 11 | import android.view.LayoutInflater; 12 | import android.view.View; 13 | import android.view.ViewGroup; 14 | import android.widget.Button; 15 | 16 | import com.joker.annotation.PermissionsCustomRationale; 17 | import com.joker.annotation.PermissionsDenied; 18 | import com.joker.annotation.PermissionsGranted; 19 | import com.joker.annotation.PermissionsNonRationale; 20 | import com.joker.annotation.PermissionsRationale; 21 | import com.joker.api.Permissions4M; 22 | import com.joker.permissions4m.other.ToastUtil; 23 | 24 | // support Fragment 25 | public class SupportFragment extends Fragment { 26 | private static final int LOCATION_CODE = 13; 27 | private static final int CAMERA_CODE = 14; 28 | private Button mLocationButton; 29 | private Button mCameraButton; 30 | 31 | public SupportFragment() { 32 | // Required empty public constructor 33 | } 34 | 35 | @Override 36 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 37 | Bundle savedInstanceState) { 38 | // Inflate the layout for this fragment 39 | View view = inflater.inflate(R.layout.fragment_support, container, false); 40 | mLocationButton = (Button) view.findViewById(R.id.btn_location); 41 | mCameraButton = (Button) view.findViewById(R.id.btn_camera); 42 | 43 | // 地理位置申请 44 | mLocationButton.setOnClickListener(new View.OnClickListener() { 45 | @Override 46 | public void onClick(View v) { 47 | Permissions4M.get(SupportFragment.this) 48 | .requestUnderM(true) 49 | .requestPermissions(Manifest.permission.ACCESS_FINE_LOCATION) 50 | .requestCodes(LOCATION_CODE) 51 | .requestPageType(Permissions4M.PageType.MANAGER_PAGE) 52 | .request(); 53 | } 54 | }); 55 | 56 | // 相机申请 57 | mCameraButton.setOnClickListener(new View.OnClickListener() { 58 | @Override 59 | public void onClick(View v) { 60 | Permissions4M.get(SupportFragment.this) 61 | .requestPermissions(Manifest.permission.CAMERA) 62 | .requestCodes(CAMERA_CODE) 63 | .requestPageType(Permissions4M.PageType.MANAGER_PAGE) 64 | .request(); 65 | } 66 | }); 67 | 68 | return view; 69 | } 70 | 71 | @Override 72 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] 73 | grantResults) { 74 | Permissions4M.onRequestPermissionsResult(SupportFragment.this, requestCode, grantResults); 75 | super.onRequestPermissionsResult(requestCode, permissions, grantResults); 76 | } 77 | 78 | //==================================================================== 79 | @PermissionsGranted(LOCATION_CODE) 80 | public void contactGranted() { 81 | ToastUtil.show("读取地理位置权限成功 in fragment"); 82 | } 83 | 84 | @PermissionsDenied(LOCATION_CODE) 85 | public void contactDenied() { 86 | ToastUtil.show("读取地理位置权限失败 in fragment"); 87 | } 88 | 89 | @PermissionsRationale(LOCATION_CODE) 90 | public void contactRationale() { 91 | ToastUtil.show("请开启读取地理位置权限 in fragment"); 92 | } 93 | 94 | //==================================================================== 95 | @PermissionsGranted(CAMERA_CODE) 96 | public void cameraGranted() { 97 | ToastUtil.show("相机权限授权成功 in fragment"); 98 | } 99 | 100 | @PermissionsDenied(CAMERA_CODE) 101 | public void cameraDenied() { 102 | ToastUtil.show("相机权限授权失败 in fragment"); 103 | } 104 | 105 | @PermissionsCustomRationale(CAMERA_CODE) 106 | public void cameraCustomRationale() { 107 | new AlertDialog.Builder(getActivity()) 108 | .setMessage("相机权限申请:\n我们需要您开启相机信息权限(in fragment)") 109 | .setPositiveButton("确定", new DialogInterface.OnClickListener() { 110 | @Override 111 | public void onClick(DialogInterface dialog, int which) { 112 | Permissions4M.get(SupportFragment.this) 113 | .requestOnRationale() 114 | .requestPermissions(Manifest.permission.CAMERA) 115 | .requestCodes(CAMERA_CODE) 116 | .request(); 117 | } 118 | }) 119 | .show(); 120 | } 121 | 122 | @PermissionsNonRationale({LOCATION_CODE, CAMERA_CODE}) 123 | public void non(int code, final Intent intent) { 124 | switch (code) { 125 | case LOCATION_CODE: 126 | new AlertDialog.Builder(getActivity()) 127 | .setMessage("读取地理位置权限申请:\n我们需要您开启读取地理位置权限(in fragment with annotation)") 128 | .setPositiveButton("确定", new DialogInterface.OnClickListener() { 129 | @Override 130 | public void onClick(DialogInterface dialog, int which) { 131 | startActivity(intent); 132 | } 133 | }) 134 | .setNegativeButton("取消", new DialogInterface.OnClickListener() { 135 | @Override 136 | public void onClick(DialogInterface dialog, int which) { 137 | dialog.dismiss(); 138 | } 139 | }) 140 | .show(); 141 | break; 142 | case CAMERA_CODE: 143 | new AlertDialog.Builder(getActivity()) 144 | .setMessage("读取相机权限申请:\n我们需要您开启读取相机权限(in fragment with annotation)") 145 | .setPositiveButton("确定", new DialogInterface.OnClickListener() { 146 | @Override 147 | public void onClick(DialogInterface dialog, int which) { 148 | startActivity(intent); 149 | 150 | } 151 | }) 152 | .setNegativeButton("取消", new DialogInterface.OnClickListener() { 153 | @Override 154 | public void onClick(DialogInterface dialog, int which) { 155 | dialog.dismiss(); 156 | } 157 | }) 158 | .show(); 159 | break; 160 | } 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /app/src/main/java/com/joker/permissions4m/other/App.java: -------------------------------------------------------------------------------- 1 | package com.joker.permissions4m.other; 2 | 3 | import android.app.Activity; 4 | import android.app.Application; 5 | import android.content.Context; 6 | import android.os.Bundle; 7 | 8 | import com.umeng.analytics.MobclickAgent; 9 | 10 | /** 11 | * Created by joker on 2017/7/27. 12 | */ 13 | 14 | public class App extends Application { 15 | private static App instance; 16 | 17 | public static Context getAppContext() { 18 | return instance.getApplicationContext(); 19 | } 20 | 21 | @Override 22 | public void onCreate() { 23 | super.onCreate(); 24 | instance = this; 25 | MobclickAgent.setCatchUncaughtExceptions(true); 26 | registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() { 27 | @Override 28 | public void onActivityCreated(Activity activity, Bundle savedInstanceState) { 29 | 30 | } 31 | 32 | @Override 33 | public void onActivityStarted(Activity activity) { 34 | 35 | } 36 | 37 | @Override 38 | public void onActivityResumed(Activity activity) { 39 | MobclickAgent.onResume(activity); 40 | } 41 | 42 | @Override 43 | public void onActivityPaused(Activity activity) { 44 | MobclickAgent.onPause(activity); 45 | } 46 | 47 | @Override 48 | public void onActivityStopped(Activity activity) { 49 | 50 | } 51 | 52 | @Override 53 | public void onActivitySaveInstanceState(Activity activity, Bundle outState) { 54 | 55 | } 56 | 57 | @Override 58 | public void onActivityDestroyed(Activity activity) { 59 | 60 | } 61 | }); 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /app/src/main/java/com/joker/permissions4m/other/ToastUtil.java: -------------------------------------------------------------------------------- 1 | package com.joker.permissions4m.other; 2 | 3 | import android.widget.Toast; 4 | 5 | /** 6 | * Created by joker on 2017/7/27. 7 | */ 8 | 9 | public class ToastUtil { 10 | private static Toast toast; 11 | 12 | public static void show(String content) { 13 | if (toast == null) { 14 | toast = Toast.makeText(App.getAppContext(), content, Toast.LENGTH_SHORT); 15 | } else { 16 | toast.setText(content); 17 | } 18 | toast.show(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 8 | 9 | 15 | 16 | 29 | 30 |