Library for easy handling of android run-time permissions.

Overview

Android Runtime Permission Library

Easily handle runtime permissions in android.

  • Very short code.
  • Handle "don't ask again" condition.
  • Can request from any context (Activity, Service, Fragment, etc).
  • Can check multiple permissions at once.
  • Light weight (12 KB).
  • Used by hundreds of developers.
  • Quick support.
  • Open source and fully customizable.

Dependency:

Gradle (Jcenter)

implementation 'com.nabinbhandari.android:permissions:3.8'

Usage:

First declare your permissions in the manifest. Example:

<uses-permission android:name="android.permission.CAMERA" />

Single permission:

Permissions.check(this/*context*/, Manifest.permission.CALL_PHONE, null, new PermissionHandler() {
    @Override
    public void onGranted() {
        // do your task.
    }
});

Multiple permissions:

String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
Permissions.check(this/*context*/, permissions, null/*rationale*/, null/*options*/, new PermissionHandler() {
    @Override
    public void onGranted() {
        // do your task.
    }
});

Customized permissions request:

String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION};
String rationale = "Please provide location permission so that you can ...";
Permissions.Options options = new Permissions.Options()
        .setRationaleDialogTitle("Info")
        .setSettingsDialogTitle("Warning");

Permissions.check(this/*context*/, permissions, rationale, options, new PermissionHandler() {
    @Override
    public void onGranted() {
        // do your task.
    }

    @Override
    public void onDenied(Context context, ArrayList<String> deniedPermissions) {
        // permission denied, block the feature.
    }
});
  • You can also override other methods like onDenied, onJustBlocked, etc if you want to change the default behaviour.
  • Dialog messages and texts can be modified by building the options parameter.
  • See documentation in the source code for more customizations.

If you find this library useful, please consider starring this repository from the top of this page.


LICENSE

Copyright 2018 Nabin Bhandari

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.

Developed by:

Nabin Bhandari
Email | Facebook | Paypal

Comments
  • App Crashes - Runtime Exception

    App Crashes - Runtime Exception

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.blah.blah.debug/com.nabinbhandari.android.permissions.PermissionsActivity}: java.lang.IllegalArgumentException: permission cannot be null or empty
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
            at android.app.ActivityThread.-wrap11(Unknown Source:0)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
            at android.os.Handler.dispatchMessage(Handler.java:106)
            at android.os.Looper.loop(Looper.java:164)
            at android.app.ActivityThread.main(ActivityThread.java:6494)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
         Caused by: java.lang.IllegalArgumentException: permission cannot be null or empty
            at android.content.pm.PackageManager.buildRequestPermissionsIntent(PackageManager.java:3544)
            at android.app.Activity.requestPermissions(Activity.java:4381)
            at com.nabinbhandari.android.permissions.PermissionsActivity.onCreate(PermissionsActivity.java:72)
            at android.app.Activity.performCreate(Activity.java:7009)
            at android.app.Activity.performCreate(Activity.java:7000)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
    

    I am observing on Permission Checks in my background service. While service is running and app is removed from recent apps, If I try to revoke permission it shows the dialog which makes total sense. But at the same time it also make the App alive in background and irrespective of pressing "Cancel" or "Ok", if I bring the app to foreground it crashes. In that state, I don't know why lib is getting null or empty permission.

    I am using app in production environment so it's kind of critical for me. if you have more questions to understand the problem I would be more than happy to help you out.

    good first issue 
    opened by wahibhaq 9
  • Permission dialog quits the second time around.

    Permission dialog quits the second time around.

    When I deny the all permissions I requested: First time round - Opens a dialog with the user specific method. Second time round - Quits and no callback methods are triggered.

    My goal is to continue to ask the user to accept the permissions or block them outright.

    opened by ChinHairSaintClair 7
  • Crash on Android 9.0 when using this library outside of an Activity

    Crash on Android 9.0 when using this library outside of an Activity

    android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? at android.app.ContextImpl.startActivity(ContextImpl.java:1072) at android.app.ContextImpl.startActivity(ContextImpl.java:1041)

    With Android 9, you cannot start an activity from a non-activity context unless you pass the intent flag FLAG_ACTIVITY_NEW_TASK. If you attempt to start an activity without passing this flag, the activity does not start, and the system prints a message to the log.

    opened by zztmercury 5
  • Multiple permissions not working

    Multiple permissions not working

    Hi, this library don't work for me. Can't ask for multiple permissions at the same time. It asks only for the first one in the array. I'm trying to allow both the READ and WRITE storage permissions at once, but without any success.

    opened by sunbyte 3
  • Exceptions arisies

    Exceptions arisies

    java.lang.RuntimeException: at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2421) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2481) at android.app.ActivityThread.access$900 (ActivityThread.java:155) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1349) at android.os.Handler.dispatchMessage (Handler.java:102) at android.os.Looper.loop (Looper.java:174) at android.app.ActivityThread.main (ActivityThread.java:5440) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616) Caused by: java.lang.NullPointerException: at android.content.pm.PackageManager.buildRequestPermissionsIntent (PackageManager.java:2659) at android.app.Activity.requestPermissions (Activity.java:3860) at com.nabinbhandari.android.permissions.PermissionsActivity.onCreate (PermissionsActivity.java:72) at android.app.Activity.performCreate (Activity.java:6285) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1108) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2374)

    java.lang.RuntimeException: at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2955) at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3030) at android.app.ActivityThread.-wrap11 (Unknown Source) at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1696) at android.os.Handler.dispatchMessage (Handler.java:105) at android.os.Looper.loop (Looper.java:164) at android.app.ActivityThread.main (ActivityThread.java:6938) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374) Caused by: java.lang.IllegalArgumentException: at android.content.pm.PackageManager.buildRequestPermissionsIntent (PackageManager.java:4007) at android.app.Activity.requestPermissions (Activity.java:4458) at com.nabinbhandari.android.permissions.PermissionsActivity.onCreate (PermissionsActivity.java:72) at android.app.Activity.performCreate (Activity.java:7174) at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1220) at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2908)

    opened by Hanzala717 3
  • Runtime Exception

    Runtime Exception

    Hi. I am using your library in my app. I got a Runtime Exception only on some devices. I cant figure out why... Here's the log

    `Fatal Exception: java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=6937, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.ytheekshana.deviceinfo/com.nabinbhandari.android.permissions.PermissionsActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
       at android.app.ActivityThread.deliverResults(ActivityThread.java:4926)
       at android.app.ActivityThread.handleSendResult(ActivityThread.java:4969)
       at android.app.ActivityThread.access$1600(ActivityThread.java:222)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1850)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:158)
       at android.app.ActivityThread.main(ActivityThread.java:7230)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)`
    

    In my fragment

            `HorizontalScrollView scrollViewCamera = rootView.findViewById(R.id.scrollViewCamera);
            cameraButtonGroup = rootView.findViewById(R.id.cameraButtonGroup);
            recyclerCamera = rootView.findViewById(R.id.recyclerCamera);
            Button btnCameraPermission = rootView.findViewById(R.id.btnCameraPermission);
    
            if (ContextCompat.checkSelfPermission(context, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
                btnCameraPermission.setVisibility(View.VISIBLE);
                scrollViewCamera.setVisibility(View.GONE);
                recyclerCamera.setVisibility(View.GONE);
            } else {
                btnCameraPermission.setVisibility(View.GONE);
                scrollViewCamera.setVisibility(View.VISIBLE);
                recyclerCamera.setVisibility(View.VISIBLE);
                loadCameraAll();
            }
            btnCameraPermission.setOnClickListener(view -> Permissions.check(context, Manifest.permission.CAMERA, null, new PermissionHandler() {
                @Override
                public void onGranted() {
                    btnCameraPermission.setVisibility(View.GONE);
                    scrollViewCamera.setVisibility(View.VISIBLE);
                    recyclerCamera.setVisibility(View.VISIBLE);
                    loadCameraAll();
                }
    
                @Override
                public void onDenied(Context context, ArrayList<String> deniedPermissions) {
                }
            }));`
    

    Any fix for this ? Happened in 4 devices with Android 6.0. Looks like this bug is only in Marshmallow devices.

    opened by ytheekshana 2
  • App Crashes on Android 6.0 Only (android.util.AndroidRuntimeException:)

    App Crashes on Android 6.0 Only (android.util.AndroidRuntimeException:)

    at android.app.ContextImpl.startActivity (ContextImpl.java:672) at android.app.ContextImpl.startActivity (ContextImpl.java:659) at android.content.ContextWrapper.startActivity (ContextWrapper.java:331) at com.nabinbhandari.android.permissions.Permissions.check (Permissions.java:121) at nigeria.comedy.sound.effect.meme.Player$2.onClick (Player.java:83) at android.view.View.performClick (View.java:5205) at android.view.View$PerformClick.run (View.java:21162) at android.os.Handler.handleCallback (Handler.java:739) at android.os.Handler.dispatchMessage (Handler.java:95) at android.os.Looper.loop (Looper.java:148) at android.app.ActivityThread.main (ActivityThread.java:5452) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:762) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:652)

    opened by oluwabajio 2
  • Unable to see the custom rationale dialog on app start or at least it's random

    Unable to see the custom rationale dialog on app start or at least it's random

    Hi.

    I moved back to my side project and even updated to version 3.7 but I don't think it is about the version. I have Android 7.0 and using it like this in my Activity.

    Permissions.check(this,
                arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                R.string.permission_required_body,
                Permissions.Options()
                        .setSettingsDialogTitle(getString(R.string.permission_required_title))
                        .setSettingsDialogMessage(getString(R.string.permission_blocked_body))
                        .setRationaleDialogTitle(getString(R.string.permission_required_title)),
                permissionHandler)
    

    and instead of seeing the custom dialog, I see the native permission dialog to allow/deny permission but without the "Don't ask again" option. It happens irrespective of GPS is already enabled or disabled.

    Can you please look into this matter because I am writing my blog post on a use case which uses your lib and now I am worried about this strange behaviour which was not there when I last worked on it 2 months ago.

    opened by wahibhaq 2
  • targetSdkVersion

    targetSdkVersion

    Hi, targetSdkVersion is 27 in this library and for uploading apps to playstore this library needs to have targetSdkVersion = 29 at least. I will be thankfull if you could change it, so wen use it and upload our app

    opened by bitamirshafiee 1
  • Not showing rational message

    Not showing rational message

    Hi, I have version 3.8 the first time I require the permission message is not showing but from the second time message is showing on all requests. and on some devices not showing the rational message at all. I am using kotlin by the way.

    opened by bitamirshafiee 1
  • Options when checking for single permission

    Options when checking for single permission

    Hi, I was suffering of issue #11 and fixed it by using setCreateNewTask(). But I can surely not be the only one calling the check from a fragment in example. In IMHO the option is hidden to deep and is not well documented enough.

    But my mayor issue is that check function only supports Options parameter in combination with String[] not with String. Can you change that? Of course I can use String[] permissions = {single permission}

    opened by soenkegissel 1
  • Migrate from JCenter

    Migrate from JCenter

    In may this library won't be available from JCenter: https://jfrog.com/blog/into-the-sunset-bintray-jcenter-gocenter-and-chartcenter/

    Please migrate this library to some other repository

    opened by chrjsorg 18
  • Option to prevent rationale dialog from being cancelled by the user

    Option to prevent rationale dialog from being cancelled by the user

    Hi,

    I had a scenario in my App, where the user would keep getting the rationale dialog over and over again for each attempt to run a specific action that required a certain permission (if denied). When the user hit "cancel" in the rationale dialog, neither the system nor the Android-Permissions library would keep any persisted record of the denial. Thus I needed to force the user to continue to the system permission request dialog, so that the system could mark the permission as permanently denied (when denied again), and automatically respond with denial for any further requests.

    For this I introduced an option to control whether or not the rationale dialog should be cancelable or not. Along with this option I also added options to provide text for the rationale dialog's buttons. I used this in my app to change the positive button to "Continue".

    opened by jepschmidt 0
  • Add Kotlin and Extensions support

    Add Kotlin and Extensions support

    Type of change

    Please delete options that are not relevant.

    • [ ] Bug fix (non-breaking change which fixes an issue)
    • [x] Refactor
    • [x] New feature (non-breaking change which adds functionality)
    • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
    • [ ] This change requires a documentation update

    How Has This Been Tested?

    Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

    • [x] Provide permissions using the :sample app

    Test Configuration:

    • Hardware: Emulator
    • SDK: 29

    Checklist:

    • [x] My code follows the style guidelines of this project
    • [x] I have performed a self-review of my own code
    • [ ] I have commented my code, particularly in hard-to-understand areas
    • [x] I have made corresponding changes to the documentation
    • [x] My changes generate no new warnings
    • [ ] I have added tests that prove my fix is effective or that my feature works
    • [ ] New and existing unit tests pass locally with my changes
    • [ ] Any dependent changes have been merged and published in downstream modules
    opened by dhruvnagarajan 2
  • Add support for permission check call as an extension of Activity

    Add support for permission check call as an extension of Activity

    Type of change

    Please delete options that are not relevant.

    • [ ] Bug fix (non-breaking change which fixes an issue)
    • [x] New feature (non-breaking change which adds functionality)
    • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
    • [ ] This change requires a documentation update

    How Has This Been Tested?

    Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

    • [x] Provide permissions using the Sample app in the codebase

    Test Configuration:

    • Hardware: Emulator
    • SDK: 28

    Checklist:

    • [ ] My code follows the style guidelines of this project
    • [x] I have performed a self-review of my own code
    • [ ] I have commented my code, particularly in hard-to-understand areas
    • [ ] I have made corresponding changes to the documentation
    • [x] My changes generate no new warnings
    • [ ] I have added tests that prove my fix is effective or that my feature works
    • [ ] New and existing unit tests pass locally with my changes
    • [ ] Any dependent changes have been merged and published in downstream modules
    opened by dhruvnagarajan 0
  • Crash on Android Q

    Crash on Android Q

    Hey there,

    I recently updated my Test Pixel 3 to Android Q Beta 3 and I'm requesting the Location (Fine) Permission within my app.

    Unfortunately the process crashes after I accept the permission. Within the logs logs I can't really see the cause: https://gist.github.com/chrjsorg/334223e49aff5ccf1ff129657d7aff1f

    if (!BuildConfig.DEBUG) Permissions.disableLogging()
        Permissions.check(this, Array(1) { permission },
                getString(rationale),
                Permissions.Options().setRationaleDialogTitle("string")
                        .setSettingsDialogTitle("string2")
                        .setSettingsDialogMessage("string3")
                        .setSettingsText("string3"),
                object : PermissionHandler() {
                    override fun onDenied(context: Context?, deniedPermissions: ArrayList<String>?) {
                    }
    
                    override fun onGranted() {
                        grantedCallback()
                    }
                })
    

    Do you have any idea? I'd be happy to support you with more informations.

    opened by chrjsorg 1
Owner
Nabin Bhandari
Android app developer at @yarsa
Nabin Bhandari
Android-basics-kotlin-tip-time-app - Tip Time app from Android Basics in Kotlin

Tip Time Tip Time app from Android Basics in Kotlin at developers.google.com. It

Ramon Lima e Meira 0 Jan 2, 2022
Episodie is a TV show time tracker app with unusual design written in kotlin and clean architecture approach. Get to know how much time you spent watching tv shows.

Episodie Episodie is a TV show time tracker app with unusual design. Get to know how much time you spent watching tv shows. Track easily overall progr

Przemek 126 Dec 7, 2022
Java/Kotlin lightweight implementation of RFC-6238 and RFC-4226 to generate and validate time-based one-time passwords (TOTP).

1time Java/Kotlin lightweight implementation of RFC-6238 and RFC-4226 to generate and validate time-based one-time passwords (TOTP). Maven / gradle de

Atlassian Labs 31 Dec 21, 2022
Android Studio project wrapper around the Elixir TodoApp Desktop app to run on Android including the Erlang runtime

TodoApp Android: An Android Sample App This Android Studio project wraps the Desktop Sample App to run on an Android phone. How to build & run Install

elixir-desktop 78 Dec 10, 2022
Run yolov5s on Android device!

The implementation of yolov5s on android for the yolov5s export contest.

null 203 Dec 23, 2022
This component allows you to interact with remote machine via SSH. You can execute commands or run scripts located on that machine

th2 act ssh (1.2.0) Overview Provides user with the ability to execute specified scripts or commands with the parameter he needs Custom resources for

th2 1 Dec 18, 2022
An App based on MVVM architecture to track & store a user's runs using Google Maps, with options to view & sort the runs as per the user's choice along the with option to run the app in background.

An App based on MVVM architecture to track & store a user's runs using Google Maps, with options to view & sort the runs as per the user's choice along the with option to run the app in background.

Harshit Maheshwari 1 Jun 9, 2022
Easy-Note - Easy Note Application will help user to add and update their important notes

Easy-Note ??️ Easy Note App helps you to create your notes. You can ?? edit and

Ade Amit 0 Jan 30, 2022
This android app fetches the data from the USGS API in real time to display a list of earthquakes.

This android app fetches the data from the USGS API in real time to display a list of earthquakes. On clicking an earthquake it opens a browser window with the complete information of the earthquake along with the location on a map.

null 5 Jan 11, 2022
ZeAppp v3, created by Android enthusiasts joining the Droidcon 2021 in Berlin, coming to the GDG Booth and writing code, 15 minutes at a time

ZeThree App build at the GDG Community booth at Droidcon Berlin 2021. Come join the fun™. ZeWhat? Based on the previous success of the ZeAppp-app, thi

GDG Berlin Android 8 Sep 16, 2022
ICSx⁵ is an Android app to subscribe to remote or local iCalendar files (like time tables of your school/university or event files of your sports team).

ICSx⁵ ICSx⁵ is an Android app to subscribe to remote Webcal feeds / iCalendar files (like time tables of your school/university or event files of your

bitfire web engineering 60 Dec 28, 2022
Rahul Kesharwani 2 Jan 10, 2022
Test android startup time fluctsdk

Test android startup time fluctsdk

Shuma Yoshioka 0 Feb 4, 2022
Task Timer - An android app that helps you to keep a track of how much time you spend on each task

Task Timer - An android app that helps you to keep a track of how much time you spend on each task

Hassan Al-Shammari 1 Feb 24, 2022
Movie Time Android App (Winter 2022)

Movie Time Android App (Winter 2022) Overview Movie time is an app for Android written in Kotlin for tracking movies that the user has watched and for

Alima Roman 2 May 13, 2022
TIRO - Time Is Running Out : Productivity app and widget for Android

TIRO-2022 TIRO - Time Is Running Out : Productivity app and widget for Android The purpose of this app is to let you know how much remaining time you

Alubecki 2 Aug 28, 2022
Communicating between Wear OS and Android device using the OpWear module and a sample of displaying real-time camera on the watch and sending commands to the mobile by Wear OS.

OpWear-Cam Communicating between Wear OS and Android device using the OpWear module and a sample of displaying real-time camera on the watch and sendi

AmirHosseinAghajari 6 Nov 8, 2022
⌨️ A tool that gives you a massive head start when building Compose based apps. It saves you from time-consuming setup and configuration

⌨️ A tool that gives you a massive head start when building Compose based apps. It saves you from time-consuming setup and configuration

theapache64 467 Dec 23, 2022
This project demonstrate the use of CameraX API for in-app face time.

CameraApp This project demonstrate the use of CameraX API for in-app face time. You can click picture using main or front. I have also used yalantis.u

Shamshad Reyaz(Arman) 1 Oct 22, 2021