Code Monkey home page Code Monkey logo

vinodbaste / permission_handler Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 164 KB

On most operating systems, permissions aren't just granted to apps at install time. Rather, developers have to ask the user for permissions while the app is running. This plugin provides a API to request permissions and check their status.

License: MIT License

Java 100.00%
android android-permissions googledevelopers jitpack-library library permissions permissions-android runtime-permissions

permission_handler's Introduction

permission_handler

On most operating systems, permissions aren't just granted to apps at install time. Rather, developers have to ask the user for permissions while the app is running.

This plugin provides a API to request permissions and check their status. You can also open the device's app settings so users can grant a permission. and you can show a rationale for requesting a permission.

API GitHub tag License News - Android Weekly Story - Medium GitHub - VinodBaste

How to implement

To get a Git project into your build:

Gradle

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

allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}

Step 2: Add the dependency in your project build.gradle

dependencies {
    implementation 'com.github.vinodbaste:permission_handler:1.0.0'
}

Usage:


First declare your permissions in the manifest file. Example:

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

Single permission

Pass the permission you want to request.

PermissionsHandler.requestPermission(this, Manifest.permission.CALL_PHONE, null, new PermissionHandler() {
@Override
public void onPermissionGranted() {
        Toast.makeText(MainActivity.this, "Phone granted.", Toast.LENGTH_SHORT).show();
        }
});

Multiple permissions

pass multiple requests in a list

String[] permissions = {Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
        PermissionsHandler.requestPermission(this,permissions, null, new PermissionHandler() {
@Override
public void onPermissionGranted() {
        Toast.makeText(MainActivity.this, "Permissions granted.", Toast.LENGTH_SHORT).show();
        }
});

override methods

onPermissionGranted: This override method is called after all the permissions are granted by the user.

public void onPermissionGranted(){
        Toast.makeText(MainActivity.this,"Permissions granted.",Toast.LENGTH_SHORT).show();
        }

onPermissionDenied: This override method is called when any of the permission is denied by the user.

 @Override
public void onPermissionDenied(Context context, ArrayList<String> deniedPermissions) {
        Toast.makeText(MainActivity.this, "Location denied.", Toast.LENGTH_SHORT).show();
        }

onPermissionDeniedOnce: This override method is called when any of the permission is denied by the user once or on the first time of the permisssion promt window is displayed.

@Override
public void onPermissionDeniedOnce(Context context, ArrayList<String> justBlockedList, ArrayList<String> deniedPermissions) {
        super.onPermissionDeniedOnce(context, justBlockedList, deniedPermissions);

        Log.e("onJustPermissions1", justBlockedList.toString());
        Log.e("onJustPermissions2", deniedPermissions.toString());
        }

onPermissionNeverAskAgain: This override method is called when any of the permission is denied perminentaly by the user.

 @Override
public boolean onPermissionNeverAskAgain(Context context, ArrayList<String> blockedList) {
        return super.onPermissionNeverAskAgain(context, blockedList);
        }

Note

On permanent deny of any permission requested, navigating to application settings to enable the permanent denied permission promt is enabled by default.

Customized permissions request

you can customize the permission request by using options.

  • rationale: short message for why the permission is needed.
  • setRationaleDialogTitle: title for rational dialog.
  • setSettingsDialogTitle: title for settings dialog.
String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
        String rationale = "Please provide location permission so that you can ...";
        PermissionsHandler.Options options = new PermissionsHandler.Options()
        .setRationaleDialogTitle("Info")
        .setSettingsDialogTitle("Warning");

        PermissionsHandler.requestPermission(this, permissions, rationale, options, new PermissionHandler() {
@Override
public void onPermissionGranted() {
        Toast.makeText(MainActivity.this, "Location granted.", Toast.LENGTH_SHORT).show();
        }

@Override
public void onPermissionDenied(Context context, ArrayList<String> deniedPermissions) {
        Toast.makeText(MainActivity.this, "Location denied.", Toast.LENGTH_SHORT).show();
        }
        });
	

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

Support my work

Buy Me A Coffee

License

Copyright [2022] [Vinod Baste]

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.

permission_handler's People

Contributors

vinodbaste avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

permission_handler's Issues

It is crashing when I'm trying multiple permission.

I looks like some instructions are missing in you documentation.
When I'm trying to have multiple permission, the library will crash with the output:

--------- beginning of crash

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.alainsergenagni.bebetter, PID: 27841
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.alainsergenagni.bebetter/com.library.permission_handler.PermissionsActivity}; have you declared this activity in your AndroidManifest.xml, or does your intent not match its declared ?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2171)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1805)
at android.app.Activity.startActivityForResult(Activity.java:5583)
at android.app.Activity.startActivityForResult(Activity.java:5541)
at android.app.Activity.startActivity(Activity.java:6039)
at android.app.Activity.startActivity(Activity.java:6006)
at com.library.permission_handler.PermissionsHandler.requestPermission(PermissionsHandler.java:121)
at com.library.permission_handler.PermissionsHandler.requestPermission(PermissionsHandler.java:52)
at org.cocos2dx.cpp.AppActivity.showCameraOnUI(AppActivity.java:1622)
at org.cocos2dx.cpp.AppActivity$8.run(AppActivity.java:1608)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8741)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
I/Process: Sending signal. PID: 27841 SIG: 9

Android 13: READ_MEDIA_IMAGES, READ_MEDIA_VIDEO, READ_MEDIA_AUDIO

Hi,
This library seams to not work with Android 13+ for asking permission. Specifically for these:

<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"
    android:minSdkVersion="33"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"
    android:minSdkVersion="33"/>
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO"
    android:minSdkVersion="33"/>

Thank you,
Stay safe

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.