{"id":3650,"url":"https://github.com/googlesamples/easypermissions","last_synced_at":"2025-11-06T03:30:25.447Z","repository":{"id":37359381,"uuid":"48446680","full_name":"googlesamples/easypermissions","owner":"googlesamples","description":"Simplify Android M system permissions","archived":true,"fork":false,"pushed_at":"2023-08-01T18:11:12.000Z","size":635,"stargazers_count":9882,"open_issues_count":40,"forks_count":1458,"subscribers_count":278,"default_branch":"master","last_synced_at":"2025-02-09T05:24:30.813Z","etag":null,"topics":["android","android-library","permissions"],"latest_commit_sha":null,"homepage":"https://firebaseopensource.com/projects/googlesamples/easypermissions/","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/googlesamples.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2015-12-22T18:05:53.000Z","updated_at":"2025-02-05T15:06:39.000Z","dependencies_parsed_at":"2023-02-15T23:25:14.738Z","dependency_job_id":"3db1b0ce-1948-4aee-a93d-814dde93fc87","html_url":"https://github.com/googlesamples/easypermissions","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googlesamples%2Feasypermissions","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googlesamples%2Feasypermissions/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googlesamples%2Feasypermissions/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/googlesamples%2Feasypermissions/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/googlesamples","download_url":"https://codeload.github.com/googlesamples/easypermissions/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239479519,"owners_count":19645758,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["android","android-library","permissions"],"created_at":"2024-01-05T20:16:47.504Z","updated_at":"2025-11-06T03:30:25.392Z","avatar_url":"https://github.com/googlesamples.png","language":"Java","funding_links":[],"categories":["Java","Android 应用","Libraries"],"sub_categories":["网络服务_其他","Runtime Permissions"],"readme":"# EasyPermissions [![Build Status][1]][2] [![Android Weekly][3]][4]\n\nEasyPermissions is a wrapper library to simplify basic system permissions logic when targeting\nAndroid M or higher.\n\n**Note:** If your app is written in Kotlin consider the [easypermissions-ktx](https://github.com/VMadalin/easypermissions-ktx)\nlibrary which adds Kotlin extensions to the core EasyPermissions library.\n\n## Installation\n\nEasyPermissions is installed by adding the following dependency to your `build.gradle` file:\n\n```groovy\ndependencies {\n    // For developers using AndroidX in their applications\n    implementation 'pub.devrel:easypermissions:3.0.0'\n \n    // For developers using the Android Support Library\n    implementation 'pub.devrel:easypermissions:2.0.1'\n}\n```\n\n## Usage\n\n### Basic\n\nTo begin using EasyPermissions, have your `Activity` (or `Fragment`) override the `onRequestPermissionsResult` method:\n\n```java\npublic class MainActivity extends AppCompatActivity {\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n    }\n\n    @Override\n    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {\n        super.onRequestPermissionsResult(requestCode, permissions, grantResults);\n\n        // Forward results to EasyPermissions\n        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);\n    }\n}\n```\n\n### Request Permissions\n\nThe example below shows how to request permissions for a method that requires both\n`CAMERA` and `ACCESS_FINE_LOCATION` permissions. There are a few things to note:\n\n  * Using `EasyPermissions#hasPermissions(...)` to check if the app already has the\n    required permissions. This method can take any number of permissions as its final\n    argument.\n  * Requesting permissions with `EasyPermissions#requestPermissions`. This method\n    will request the system permissions and show the rationale string provided if\n    necessary. The request code provided should be unique to this request, and the method\n    can take any number of permissions as its final argument.\n  * Use of the `AfterPermissionGranted` annotation. This is optional, but provided for\n    convenience. If all of the permissions in a given request are granted, *all* methods\n    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\n    flow of needing to run the requesting method after all of its permissions have been granted.\n    This can also be achieved by adding logic on the `onPermissionsGranted` callback.\n\n```java\n@AfterPermissionGranted(RC_CAMERA_AND_LOCATION)\nprivate void methodRequiresTwoPermission() {\n    String[] perms = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION};\n    if (EasyPermissions.hasPermissions(this, perms)) {\n        // Already have permission, do the thing\n        // ...\n    } else {\n        // Do not have permissions, request them now\n        EasyPermissions.requestPermissions(this, getString(R.string.camera_and_location_rationale),\n                RC_CAMERA_AND_LOCATION, perms);\n    }\n}\n```\n\nOr for finer control over the rationale dialog, use a `PermissionRequest`:\n\n```java\nEasyPermissions.requestPermissions(\n        new PermissionRequest.Builder(this, RC_CAMERA_AND_LOCATION, perms)\n                .setRationale(R.string.camera_and_location_rationale)\n                .setPositiveButtonText(R.string.rationale_ask_ok)\n                .setNegativeButtonText(R.string.rationale_ask_cancel)\n                .setTheme(R.style.my_fancy_style)\n                .build());\n```\n\nOptionally, for a finer control, you can have your `Activity` / `Fragment` implement\nthe `PermissionCallbacks` interface.\n\n```java\npublic class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {\n\n    @Override\n    protected void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.activity_main);\n    }\n\n    @Override\n    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {\n        super.onRequestPermissionsResult(requestCode, permissions, grantResults);\n\n        // Forward results to EasyPermissions\n        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);\n    }\n\n    @Override\n    public void onPermissionsGranted(int requestCode, List\u003cString\u003e list) {\n        // Some permissions have been granted\n        // ...\n    }\n\n    @Override\n    public void onPermissionsDenied(int requestCode, List\u003cString\u003e list) {\n        // Some permissions have been denied\n        // ...\n    }\n}\n```\n\n### Required Permissions\n\nIn some cases your app will not function properly without certain permissions. If the user\ndenies these permissions with the \"Never Ask Again\" option, you will be unable to request\nthese permissions from the user and they must be changed in app settings. You can use the\nmethod `EasyPermissions.somePermissionPermanentlyDenied(...)` to display a dialog to the\nuser in this situation and direct them to the system setting screen for your app:\n\n**Note**: Due to a limitation in the information provided by the Android\nframework permissions API, the `somePermissionPermanentlyDenied` method only\nworks after the permission has been denied and your app has received\nthe `onPermissionsDenied` callback. Otherwise the library cannot distinguish\npermanent denial from the \"not yet denied\" case.\n\n```java\n@Override\npublic void onPermissionsDenied(int requestCode, List\u003cString\u003e perms) {\n    Log.d(TAG, \"onPermissionsDenied:\" + requestCode + \":\" + perms.size());\n\n    // (Optional) Check whether the user denied any permissions and checked \"NEVER ASK AGAIN.\"\n    // This will display a dialog directing them to enable the permission in app settings.\n    if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {\n        new AppSettingsDialog.Builder(this).build().show();\n    }\n}\n\n@Override\npublic void onActivityResult(int requestCode, int resultCode, Intent data) {\n    super.onActivityResult(requestCode, resultCode, data);\n\n    if (requestCode == AppSettingsDialog.DEFAULT_SETTINGS_REQ_CODE) {\n        // Do something after user returned from app settings screen, like showing a Toast.\n        Toast.makeText(this, R.string.returned_from_app_settings_to_activity, Toast.LENGTH_SHORT)\n                .show();\n    }\n}\n```\n\n### Interacting with the rationale dialog\n\nImplement the `EasyPermissions.RationaleCallbacks` if you want to interact with the rationale dialog.\n\n```java\n@Override\npublic void onRationaleAccepted(int requestCode) {\n    // Rationale accepted to request some permissions\n    // ...\n}\n\n@Override\npublic void onRationaleDenied(int requestCode) {\n    // Rationale denied to request some permissions\n    // ...\n}\n```\n\nRationale callbacks don't necessarily imply permission changes. To check for those, see the `EasyPermissions.PermissionCallbacks`.\n\n## LICENSE\n\n```\n\tCopyright 2017 Google\n\n   Licensed under the Apache License, Version 2.0 (the \"License\");\n   you may not use this file except in compliance with the License.\n   You may obtain a copy of the License at\n\n     http://www.apache.org/licenses/LICENSE-2.0\n\n   Unless required by applicable law or agreed to in writing, software\n   distributed under the License is distributed on an \"AS IS\" BASIS,\n   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n   See the License for the specific language governing permissions and\n   limitations under the License.\n\n```\n\n[1]: https://github.com/googlesamples/easypermissions/workflows/test/badge.svg\n[2]: https://github.com/googlesamples/easypermissions/actions\n[3]: https://img.shields.io/badge/Android%20Weekly-%23185-2CB3E5.svg?style=flat\n[4]: http://androidweekly.net/issues/issue-185\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgooglesamples%2Feasypermissions","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgooglesamples%2Feasypermissions","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgooglesamples%2Feasypermissions/lists"}