├── .gitignore ├── .idea ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── wanjian │ │ └── permission │ │ └── demo │ │ ├── App.java │ │ ├── MainActivity.java │ │ └── SecondAct.java │ └── res │ ├── drawable-v24 │ └── ic_launcher_foreground.xml │ ├── drawable │ └── ic_launcher_background.xml │ ├── layout │ ├── activity_main.xml │ └── activity_sec.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 │ ├── colors.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── permission ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── wanjian │ │ └── permission │ │ ├── OneKeyPerm.java │ │ ├── PermissionActivity.java │ │ └── WatchAuthorizationActivity.java │ └── res │ └── values │ ├── colors.xml │ ├── strings.xml │ └── styles.xml └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/android-notes/OneKeyPerm/6706322a2d7ee100a49d9d06d4e9442913aaa7ca/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | xmlns:android 14 | 15 | ^$ 16 | 17 | 18 | 19 |
20 |
21 | 22 | 23 | 24 | xmlns:.* 25 | 26 | ^$ 27 | 28 | 29 | BY_NAME 30 | 31 |
32 |
33 | 34 | 35 | 36 | .*:id 37 | 38 | http://schemas.android.com/apk/res/android 39 | 40 | 41 | 42 |
43 |
44 | 45 | 46 | 47 | .*:name 48 | 49 | http://schemas.android.com/apk/res/android 50 | 51 | 52 | 53 |
54 |
55 | 56 | 57 | 58 | name 59 | 60 | ^$ 61 | 62 | 63 | 64 |
65 |
66 | 67 | 68 | 69 | style 70 | 71 | ^$ 72 | 73 | 74 | 75 |
76 |
77 | 78 | 79 | 80 | .* 81 | 82 | ^$ 83 | 84 | 85 | BY_NAME 86 | 87 |
88 |
89 | 90 | 91 | 92 | .* 93 | 94 | http://schemas.android.com/apk/res/android 95 | 96 | 97 | ANDROID_ATTRIBUTE_ORDER 98 | 99 |
100 |
101 | 102 | 103 | 104 | .* 105 | 106 | .* 107 | 108 | 109 | BY_NAME 110 | 111 |
112 |
113 |
114 |
115 |
116 |
-------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 20 | 21 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 23 | 40 | 41 | 42 | 43 | 44 | 45 | 47 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 wanjian 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OneKeyPerm 2 | 一键申请Android权限 3 | 4 | 5 | ### OneKeyPerm接入说明 6 | 7 | 8 | 9 | OneKeyPerm不依赖任何业务Activity,支持多进程,只需一句静态代码就可以了 10 | 11 | 例如 12 | 13 | ```java 14 | 申请权限被拒绝后 不会 自动开启设置页面让用户手动开启权限 15 | 16 | OneKeyPerm.request(application, Manifest.permission.CAMERA, "您需要允许相机权限,否则无法使用扫码功能", new OneKeyPerm.OnPermResultListener() { 17 | @Override 18 | public void onPermResult(String perm, boolean isGrant) { 19 | Toast.makeText(MainActivity.this, "请求相机权限 " + isGrant, Toast.LENGTH_SHORT).show(); 20 | } 21 | }); 22 | ``` 23 | 24 | 或者 25 | 26 | ```java 27 | 申请权限被拒绝后 会 自动开启设置页面让用户手动开启权限 28 | 29 | OneKeyPerm.request(application, Manifest.permission.CAMERA, "您需要允许相机权限,否则无法使用扫码功能", new OneKeyPerm.OnPermResultListener() { 30 | @Override 31 | public void onPermResult(String perm, boolean isGrant) { 32 | Toast.makeText(MainActivity.this, "请求相机权限 " + isGrant, Toast.LENGTH_SHORT).show(); 33 | } 34 | },true); 35 | ``` 36 | 37 | 38 | 39 | ### 原理分析 40 | 41 | * 每次通过context启动透明Activity`(PermissionActivity)`请求权限 42 | 43 | * 当权限被拒绝后启动另一个透明Activity `(WatchAuthorizationActivity)`,在`WatchAuthorizationActivity`中再次启动应用详情设置Activity,然后在`WatchAuthorizationActivity`的`onActivityResult`方法中再次检查是否已经手动授权,并通过Binder(解决多进程问题)通知调用者 44 | 45 | 46 | 备注:收回授权后Android会重启App 47 | 48 | 49 | 50 | 51 | 52 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 33 5 | defaultConfig { 6 | applicationId "com.wanjian.permission.demo" 7 | minSdkVersion 18 8 | targetSdkVersion 33 9 | versionCode 1 10 | versionName "1.0" 11 | } 12 | buildTypes { 13 | release { 14 | minifyEnabled false 15 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 16 | } 17 | } 18 | } 19 | 20 | dependencies { 21 | implementation fileTree(include: ['*.jar'], dir: 'libs') 22 | implementation 'com.android.support:appcompat-v7:26+' 23 | implementation 'com.android.support.constraint:constraint-layout:1.0.2' 24 | implementation project(':permission') 25 | } 26 | -------------------------------------------------------------------------------- /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/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 8 | 9 | 17 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /app/src/main/java/com/wanjian/permission/demo/App.java: -------------------------------------------------------------------------------- 1 | package com.wanjian.permission.demo; 2 | 3 | import android.app.Application; 4 | 5 | import com.wanjian.permission.OneKeyPerm; 6 | 7 | /** 8 | * Created by wanjian on 2018/1/2. 9 | */ 10 | 11 | public class App extends Application { 12 | 13 | @Override 14 | public void onCreate() { 15 | super.onCreate(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/com/wanjian/permission/demo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.wanjian.permission.demo; 2 | 3 | import android.Manifest; 4 | import android.app.ActivityManager; 5 | import android.content.Context; 6 | import android.content.Intent; 7 | import android.os.Bundle; 8 | import android.os.Handler; 9 | import android.support.v7.app.AppCompatActivity; 10 | import android.view.View; 11 | import android.widget.TextView; 12 | import android.widget.Toast; 13 | 14 | import com.wanjian.permission.OneKeyPerm; 15 | 16 | 17 | public class MainActivity extends AppCompatActivity { 18 | 19 | 20 | @Override 21 | protected void onCreate(Bundle savedInstanceState) { 22 | super.onCreate(savedInstanceState); 23 | setContentView(R.layout.activity_main); 24 | findViewById(R.id.btn1).setOnClickListener(v -> requestCameraPermission()); 25 | 26 | findViewById(R.id.btn2).setOnClickListener(v -> requestStoragePermission()); 27 | 28 | findViewById(R.id.btn3).setOnClickListener(v -> { 29 | showHomeScreen(); 30 | new Handler().postDelayed(() -> requestCameraPermission(), 2000); 31 | }); 32 | 33 | findViewById(R.id.btn4).setOnClickListener(v -> { 34 | showHomeScreen(); 35 | new Handler().postDelayed(() -> requestStoragePermission(), 2000); 36 | }); 37 | 38 | findViewById(R.id.btn5).setOnClickListener(v -> { 39 | finish(); 40 | new Handler().postDelayed(() -> requestCameraPermission(), 2000); 41 | }); 42 | 43 | findViewById(R.id.btn6).setOnClickListener(v -> { 44 | finish(); 45 | new Handler().postDelayed(() -> requestStoragePermission(), 2000); 46 | }); 47 | 48 | 49 | findViewById(R.id.newAct).setOnClickListener(v -> startActivity(new Intent(getApplicationContext(), SecondAct.class))); 50 | ((TextView) findViewById(R.id.tv)).setText(processName() + " " + android.os.Process.myPid()); 51 | 52 | } 53 | 54 | private void requestStoragePermission() { 55 | OneKeyPerm.request(getApplication(), Manifest.permission.READ_EXTERNAL_STORAGE, "您需要允许读取文件权限,否则无法查看图片", new OneKeyPerm.OnPermResultListener() { 56 | @Override 57 | public void onPermResult(String perm, boolean isGrant) { 58 | Toast.makeText(MainActivity.this, "请求读取权限 " + isGrant, Toast.LENGTH_SHORT).show(); 59 | } 60 | }, true); 61 | } 62 | 63 | private void showHomeScreen() { 64 | Intent intent = new Intent(Intent.ACTION_MAIN); 65 | intent.addCategory(Intent.CATEGORY_HOME); 66 | intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 67 | startActivity(intent); 68 | } 69 | 70 | private String processName() { 71 | int pid = android.os.Process.myPid(); 72 | String processName; 73 | ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 74 | for (ActivityManager.RunningAppProcessInfo appProcess : am.getRunningAppProcesses()) { 75 | if (appProcess.pid == pid) { 76 | processName = appProcess.processName; 77 | return processName; 78 | } 79 | } 80 | return null; 81 | } 82 | 83 | private void requestCameraPermission() { 84 | OneKeyPerm.request(getApplication(), Manifest.permission.CAMERA, "您需要允许相机权限,否则无法使用扫码功能", new OneKeyPerm.OnPermResultListener() { 85 | @Override 86 | public void onPermResult(String perm, boolean isGrant) { 87 | Toast.makeText(MainActivity.this, "请求相机权限 " + isGrant, Toast.LENGTH_SHORT).show(); 88 | } 89 | }, true); 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /app/src/main/java/com/wanjian/permission/demo/SecondAct.java: -------------------------------------------------------------------------------- 1 | package com.wanjian.permission.demo; 2 | 3 | import android.Manifest; 4 | import android.app.Activity; 5 | import android.app.ActivityManager; 6 | import android.content.Context; 7 | import android.os.Bundle; 8 | import android.support.annotation.Nullable; 9 | import android.view.View; 10 | import android.widget.TextView; 11 | import android.widget.Toast; 12 | 13 | import com.wanjian.permission.OneKeyPerm; 14 | 15 | 16 | /** 17 | * Created by wanjian on 2018/1/2. 18 | */ 19 | 20 | public class SecondAct extends Activity { 21 | 22 | @Override 23 | protected void onCreate(@Nullable Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | 26 | setContentView(R.layout.activity_sec); 27 | 28 | findViewById(R.id.btn).setOnClickListener(new View.OnClickListener() { 29 | @Override 30 | public void onClick(View v) { 31 | OneKeyPerm.request(getApplication(), Manifest.permission.READ_EXTERNAL_STORAGE, "需要读取文件", new OneKeyPerm.OnPermResultListener() { 32 | @Override 33 | public void onPermResult(String perm, boolean isGrant) { 34 | Toast.makeText(SecondAct.this, "请求读取权限 " + isGrant, Toast.LENGTH_SHORT).show(); 35 | } 36 | },true); 37 | } 38 | }); 39 | ((TextView) findViewById(R.id.tv)).setText(processName() + " " + android.os.Process.myPid()); 40 | 41 | 42 | } 43 | 44 | private String processName() { 45 | int pid = android.os.Process.myPid(); 46 | String processName; 47 | ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); 48 | for (ActivityManager.RunningAppProcessInfo appProcess : am.getRunningAppProcesses()) { 49 | if (appProcess.pid == pid) { 50 | processName = appProcess.processName; 51 | return processName; 52 | } 53 | } 54 | return null; 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /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_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 9 | 10 | 17 | 18 |