Easy Permissions

Additional

Language
N/A
Version
N/A
Created
Apr 17, 2017
Updated
Sep 10, 2018
Owner
Newtron Labs (NewtronLabs)
Contributor
Newtron Labs (NewtronLabs)
1
Activity
Badge
Generate
Download
Source code
APK file

Promotion

Easy Permissions

Beginning with Android 6.0 (API level 23) if your app needs a dangerous permission, you must request it from the user. Android requires that these request come from an Activity. With Easy Permissions this is no longer an issue, you may request permission from anywhere as long as you provide Context. In addition, if you request a permission that is already granted the user will not be prompted.


In the build.gradle for your app.

dependencies {
    compileOnly 'com.newtronlabs.easypermissions:easypermissions:2.3.1'
}

Request Permission

As long as you have context you can request a permission from anywhere. Make sure you implement IPermissionsListener to receive information about what is happening with your permission request.

Example 1

This example uses a Service to request the permission, something native Android does not allow. It also automatically requests the permissions that you have enabled on your Manifest.

public class ExampleService extends Service implements IPermissionsListener
{
    @Override
    public void onCreate()
    {
        super.onCreate();
        
        // Will request all permissions from the Manifest automatically.
        EasyPermissions.getInstance().requestPermissions(this, this);
    }

    @Override
    public void onRequestSent(Set<String> set)
    {
    }

    @Override
    public void onFailure(IError error)
    {
    }
    
    @Override
    public void onCompleted(Set<String> grantedPermissions, Set<String> deniedPermissions)
    {
    }
}

Example 2

This example uses a Service to request the permission, something native Android does not allow. This example allows more flexibility so that you can decide which permissions you desire.

public class ExampleService extends Service implements IPermissionsListener
{
    @Override
    public void onCreate()
    {
        super.onCreate();

        // Request as many permissions as you like. You may seperate them by commas or pass an array.
        // Make sure that these permissions are in your Manifest as well.
        EasyPermissions.getInstance().requestPermissions(this, this,
                Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.CAMERA,
                Manifest.permission.CALL_PHONE,
                Manifest.permission.WRITE_EXTERNAL_STORAGE);
    }

    @Override
    public void onRequestSent(Set<String> set)
    {
    }

    @Override
    public void onFailure()
    {
    }
    
    @Override
    public void onCompleted(Set<String> grantedPermissions, Set<String> deniedPermissions)
    {
    }
}

Always add the permission to your AndroidManifest.xml

As an Android requirement permissions must be included in the Manifest.

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

Additional Sample

More detailed exmaples can be found in this repo's samples folders: EpSample.