RafikiPermissions is to simplify Android runtime permission request. The name Rafiki comes from the baboon elder who manages permission in LION KING :D
Content
Setup
中文文档
Adding jitpack repository in your project's build.gradle
file:
allprojects {
repositories {
maven { url 'https://www.jitpack.io' }
}
}
Adding the following dependency to app build.gradle
file:
dependencies {
implementation 'com.github.nasduck:RafikiPermissions:1.2.0'
}
Usage
Here's a minimum example, in which you need to take a photo which requires Manifest.permission.CAMERA
1、Declare the required permission
Declare camera permission in AndroidManifest.xml
:
<uses-permission android:name="android.permission.CAMERA" />
2、Uniformly process permission requests' results in the base activity class
public class BaseActivity extends AppCompatActivity {
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
RafikiPermissions.getInstance(this).onResult(requestCode, permissions, grantResults);
}
}
3、Request permissions
// Return true if the permission is granted
if (RafikiPermissions.getInstance(this)
.setResultStrategy(new PermissionResultCustomStrategy(this)) // Set customized permission handling strategy, There are also another two preset strategies
.requestCamera()) {
// Permissions Already Granted
...
}
4、Implement callbacks to the results
Implement interface OnPermissionResultListener
:
@Override
public void onPermissionsResultGrant(int requestCode) {
switch (requestCode) {
case RafikiResultCode.RESULT_CODE_CAMERA:
// Permissions Granted
}
}
@Override
public void onPermissionsResultDenied(int requestCode) {
switch (requestCode) {
case RafikiResultCode.RESULT_CODE_CAMERA:
// Permissions Denied
}
}
Permission handling strategies
Three strategies are offered:
- PermissionResultNothingStrategy By default. Whether permission granted or not, just do nothing.
- PermissionResultGuideStrategy Show an additional dialog to guide users to app setting to grant permissions again if permission not granted.
- PermissionResultCustomStrategy Customized strategy. Implement interface
OnPermissionResultListener
to customize permission handling
Different ways to request permissions
Request one single permission
Provide .requestXX()
method corresponding to each permission. For example, request for Manifest.permission.CAMERA
, use requestCamera()
to request camera permission directly.
Request multiple permissions
For more than one permissions,just use .addXXX
method corresponding to different permission to add them one by one. Call request()
in the end. The parameter passed is the user-defined requestCode(By default, RafikiResultCode.RAFIKI_PERMISSION_RESULT_CODE
if not passed), to recognized this request in the callbacks:
if (RafikiPermissions.getInstance(this)
.addReadExternalStorage()
.addWriteExternalStorage()
.setResultStrategy(new PermissionResultCustomStrategy(this))
.request(RESULT_CODE)) {
// Permissions Already Granted
...
}
or call addPermissions()
to add a permission list directly
List<String> permissions = new ArrayList();
permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (RafikiPermissions.getInstance(this)
.addPermissions(permissions)
.setResultStrategy(new PermissionResultCustomStrategy(this))
.request(RESULT_CODE)) {
// Permissions Already Granted
...
}
Contributer
- Lihao Zhou
- Chuan DONG
- Si Cheng(Art Designer)
LICENSE
Copyright (2019) Chuan Dong, Lihao Zhou
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.