项目作者: bachhoan88

项目描述 :
Permission for AndroidX
高级语言: Java
项目地址: git://github.com/bachhoan88/permissionsx.git
创建时间: 2018-09-13T02:14:45Z
项目社区:https://github.com/bachhoan88/permissionsx

开源协议:Apache License 2.0

下载


EasyPermissions Build Status Code Coverage Android Weekly

EasyPermissions is a wrapper library to simplify basic system permissions logic when targeting
Android M or higher.

Installation

Add it in your root build.gradle at the end of repositories:

  1. allprojects {
  2. repositories {
  3. maven { url 'https://jitpack.io' }
  4. }
  5. }

EasyPermissions is installed by adding the following dependency to your build.gradle file:

  1. dependencies {
  2. implementation 'com.github.bachhoan88:permissionsx:1.3.1'
  3. }

Usage

Basic

To begin using EasyPermissions, have your Activity (or Fragment or androidx.fragment.app.Fragment) override the onRequestPermissionsResult method:

  1. public class MainActivity extends AppCompatActivity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. }
  7. @Override
  8. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  9. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  10. // Forward results to EasyPermissions
  11. EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
  12. }
  13. }

Request Permissions

The example below shows how to request permissions for a method that requires both
CAMERA and ACCESS_FINE_LOCATION permissions. There are a few things to note:

  • Using EasyPermissions#hasPermissions(...) to check if the app already has the
    required permissions. This method can take any number of permissions as its final
    argument.
  • Requesting permissions with EasyPermissions#requestPermissions. This method
    will request the system permissions and show the rationale string provided if
    necessary. The request code provided should be unique to this request, and the method
    can take any number of permissions as its final argument.
  • Use of the AfterPermissionGranted annotation. This is optional, but provided for
    convenience. If all of the permissions in a given request are granted, all methods
    annotated with the proper request code will be executed(be sure to have an unique request code). The annotated method needs to be void and without input parameters (instead, you can use onSaveInstanceState in order to keep the state of your suppressed parameters). This is to simplify the common
    flow of needing to run the requesting method after all of its permissions have been granted.
    This can also be achieved by adding logic on the onPermissionsGranted callback.
  1. @AfterPermissionGranted(RC_CAMERA_AND_LOCATION)
  2. private void methodRequiresTwoPermission() {
  3. String[] perms = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION};
  4. if (EasyPermissions.hasPermissions(this, perms)) {
  5. // Already have permission, do the thing
  6. // ...
  7. } else {
  8. // Do not have permissions, request them now
  9. EasyPermissions.requestPermissions(this, getString(R.string.camera_and_location_rationale),
  10. RC_CAMERA_AND_LOCATION, perms);
  11. }
  12. }

Or for finer control over the rationale dialog, use a PermissionRequest:

  1. EasyPermissions.requestPermissions(
  2. new PermissionRequest.Builder(this, RC_CAMERA_AND_LOCATION, perms)
  3. .setRationale(R.string.camera_and_location_rationale)
  4. .setPositiveButtonText(R.string.rationale_ask_ok)
  5. .setNegativeButtonText(R.string.rationale_ask_cancel)
  6. .setTheme(R.style.my_fancy_style)
  7. .build());

Optionally, for a finer control, you can have your Activity / Fragment / androidx.fragment.app.Fragment implement
the PermissionCallbacks interface.

  1. public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. }
  7. @Override
  8. public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
  9. super.onRequestPermissionsResult(requestCode, permissions, grantResults);
  10. // Forward results to EasyPermissions
  11. EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
  12. }
  13. @Override
  14. public void onPermissionsGranted(int requestCode, List<String> list) {
  15. // Some permissions have been granted
  16. // ...
  17. }
  18. @Override
  19. public void onPermissionsDenied(int requestCode, List<String> list) {
  20. // Some permissions have been denied
  21. // ...
  22. }
  23. }

Required Permissions

In some cases your app will not function properly without certain permissions. If the user
denies these permissions with the “Never Ask Again” option, you will be unable to request
these permissions from the user and they must be changed in app settings. You can use the
method EasyPermissions.somePermissionPermanentlyDenied(...) to display a dialog to the
user in this situation and direct them to the system setting screen for your app:

Note: Due to a limitation in the information provided by the Android
framework permissions API, the somePermissionPermanentlyDenied method only
works after the permission has been denied and your app has received
the onPermissionsDenied callback. Otherwise the library cannot distinguish
permanent denial from the “not yet denied” case.

  1. @Override
  2. public void onPermissionsDenied(int requestCode, List<String> perms) {
  3. Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" + perms.size());
  4. // (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN."
  5. // This will display a dialog directing them to enable the permission in app settings.
  6. if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
  7. new AppSettingsDialog.Builder(this).build().show();
  8. }
  9. }
  10. @Override
  11. public void onActivityResult(int requestCode, int resultCode, Intent data) {
  12. super.onActivityResult(requestCode, resultCode, data);
  13. if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {
  14. // Do something after user returned from app settings screen, like showing a Toast.
  15. Toast.makeText(this, R.string.returned_from_app_settings_to_activity, Toast.LENGTH_SHORT)
  16. .show();
  17. }
  18. }

Interacting with the rationale dialog

Implement the EasyPermissions.RationaleCallbacks if you want to interact with the rationale dialog.

  1. @Override
  2. public void onRationaleAccepted(int requestCode) {
  3. // Rationale accpets to request some permissions
  4. // ...
  5. }
  6. @Override
  7. public void onRationaleDenied(int requestCode) {
  8. // Rationale denied to request some permissions
  9. // ...
  10. }

Rationale callbacks don’t necessarily imply permission changes. To check for those, see the EasyPermissions.PermissionCallbacks.

LICENSE

  1. Copyright 2017 Google
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.