- Notifications
You must be signed in to change notification settings - Fork1.5k
Simplify Android M system permissions
License
googlesamples/easypermissions
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
EasyPermissions is a wrapper library to simplify basic system permissions logic when targetingAndroid M or higher.
Note: If your app is written in Kotlin consider theeasypermissions-ktxlibrary which adds Kotlin extensions to the core EasyPermissions library.
EasyPermissions is installed by adding the following dependency to yourbuild.gradle file:
dependencies {// For developers using AndroidX in their applications implementation'pub.devrel:easypermissions:3.0.0'// For developers using the Android Support Library implementation'pub.devrel:easypermissions:2.0.1'}To begin using EasyPermissions, have yourActivity (orFragment) override theonRequestPermissionsResult method:
publicclassMainActivityextendsAppCompatActivity {@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); }@OverridepublicvoidonRequestPermissionsResult(intrequestCode,String[]permissions,int[]grantResults) {super.onRequestPermissionsResult(requestCode,permissions,grantResults);// Forward results to EasyPermissionsEasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,this); }}
The example below shows how to request permissions for a method that requires bothCAMERA andACCESS_FINE_LOCATION permissions. There are a few things to note:
- Using
EasyPermissions#hasPermissions(...)to check if the app already has therequired permissions. This method can take any number of permissions as its finalargument. - Requesting permissions with
EasyPermissions#requestPermissions. This methodwill request the system permissions and show the rationale string provided ifnecessary. The request code provided should be unique to this request, and the methodcan take any number of permissions as its final argument. - Use of the
AfterPermissionGrantedannotation. This is optional, but provided forconvenience. If all of the permissions in a given request are granted,all methodsannotated with the proper request code will be executed(be sure to have an unique request code). The annotated method needs to bevoid andwithout input parameters (instead, you can useonSaveInstanceState in order to keep the state of your suppressed parameters). This is to simplify the commonflow of needing to run the requesting method after all of its permissions have been granted.This can also be achieved by adding logic on theonPermissionsGrantedcallback.
@AfterPermissionGranted(RC_CAMERA_AND_LOCATION)privatevoidmethodRequiresTwoPermission() {String[]perms = {Manifest.permission.CAMERA,Manifest.permission.ACCESS_FINE_LOCATION};if (EasyPermissions.hasPermissions(this,perms)) {// Already have permission, do the thing// ... }else {// Do not have permissions, request them nowEasyPermissions.requestPermissions(this,getString(R.string.camera_and_location_rationale),RC_CAMERA_AND_LOCATION,perms); }}
Or for finer control over the rationale dialog, use aPermissionRequest:
EasyPermissions.requestPermissions(newPermissionRequest.Builder(this,RC_CAMERA_AND_LOCATION,perms) .setRationale(R.string.camera_and_location_rationale) .setPositiveButtonText(R.string.rationale_ask_ok) .setNegativeButtonText(R.string.rationale_ask_cancel) .setTheme(R.style.my_fancy_style) .build());
Optionally, for a finer control, you can have yourActivity /Fragment implementthePermissionCallbacks interface.
publicclassMainActivityextendsAppCompatActivityimplementsEasyPermissions.PermissionCallbacks {@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main); }@OverridepublicvoidonRequestPermissionsResult(intrequestCode,String[]permissions,int[]grantResults) {super.onRequestPermissionsResult(requestCode,permissions,grantResults);// Forward results to EasyPermissionsEasyPermissions.onRequestPermissionsResult(requestCode,permissions,grantResults,this); }@OverridepublicvoidonPermissionsGranted(intrequestCode,List<String>list) {// Some permissions have been granted// ... }@OverridepublicvoidonPermissionsDenied(intrequestCode,List<String>list) {// Some permissions have been denied// ... }}
In some cases your app will not function properly without certain permissions. If the userdenies these permissions with the "Never Ask Again" option, you will be unable to requestthese permissions from the user and they must be changed in app settings. You can use themethodEasyPermissions.somePermissionPermanentlyDenied(...) to display a dialog to theuser 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 Androidframework permissions API, thesomePermissionPermanentlyDenied method onlyworks after the permission has been denied and your app has receivedtheonPermissionsDenied callback. Otherwise the library cannot distinguishpermanent denial from the "not yet denied" case.
@OverridepublicvoidonPermissionsDenied(intrequestCode,List<String>perms) {Log.d(TAG,"onPermissionsDenied:" +requestCode +":" +perms.size());// (Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN."// This will display a dialog directing them to enable the permission in app settings.if (EasyPermissions.somePermissionPermanentlyDenied(this,perms)) {newAppSettingsDialog.Builder(this).build().show(); }}@OverridepublicvoidonActivityResult(intrequestCode,intresultCode,Intentdata) {super.onActivityResult(requestCode,resultCode,data);if (requestCode ==AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {// Do something after user returned from app settings screen, like showing a Toast.Toast.makeText(this,R.string.returned_from_app_settings_to_activity,Toast.LENGTH_SHORT) .show(); }}
Implement theEasyPermissions.RationaleCallbacks if you want to interact with the rationale dialog.
@OverridepublicvoidonRationaleAccepted(intrequestCode) {// Rationale accepted to request some permissions// ...}@OverridepublicvoidonRationaleDenied(intrequestCode) {// Rationale denied to request some permissions// ...}
Rationale callbacks don't necessarily imply permission changes. To check for those, see theEasyPermissions.PermissionCallbacks.
Copyright 2017 Google 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.About
Simplify Android M system permissions
Topics
Resources
License
Contributing
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.