Step 1. Add the JitPack repository to your build file
Add it in your root settings.gradle at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url 'https://jitpack.io' }
}
}
Add it in your settings.gradle.kts at the end of repositories:
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
mavenCentral()
maven { url = uri("https://jitpack.io") }
}
}
Add to pom.xml
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Add it in your build.sbt at the end of resolvers:
resolvers += "jitpack" at "https://jitpack.io"
Add it in your project.clj at the end of repositories:
:repositories [["jitpack" "https://jitpack.io"]]
Step 2. Add the dependency
dependencies {
implementation 'com.github.tsmsogn:Android-Permissions-Manager:v2.0.0'
}
dependencies {
implementation("com.github.tsmsogn:Android-Permissions-Manager:v2.0.0")
}
<dependency>
<groupId>com.github.tsmsogn</groupId>
<artifactId>Android-Permissions-Manager</artifactId>
<version>v2.0.0</version>
</dependency>
libraryDependencies += "com.github.tsmsogn" % "Android-Permissions-Manager" % "v2.0.0"
:dependencies [[com.github.tsmsogn/Android-Permissions-Manager "v2.0.0"]]
Easily manage Android Marshmallow and up runtime permissions. This library uses RXJava to skip all the painful parts of the Activity
/Fragment
lifecycle management.
PermissionsManager.get()
.requestCameraPermission()
.subscribe(new Action1<PermissionsResult>() {
@Override
public void call(PermissionsResult permissionsResult) {
if (permissionsResult.isGranted()) { // always true pre-M
// do whatever
}
if (permissionsResult.hasAskedForPermissions()) { // false if pre-M
// do whatever
}
}
});
This library is backwards compatible. In pre-Marshmallow devices permissions are returned as given. This is done using the Android Support library ActivityCompat
and support Fragment
methods for permissions. I've tried to make sure this library is well tested.
Javadocs can be found in the docs folder.
JCenter is a pain to maintan, so I use Jitpack.io.
You can include it in your gradle file like so:
repositories {
maven { url "https://jitpack.io" }
}
dependencies {
compile 'com.github.ralphpina:Android-Permissions-Manager:v2.0.0'
}
This library provides an interface to request PROTECTION_DANGEROUS
Android permissions. Permissions fall into groups that are granted by the user. See which permissions fall into each group in the Android docs.Right now the library supports the following permission groups:
See Request Other Permissions to see how to use this library to request various permissions in different groups.
The API allows you to check 3 things: 1. Whether the permission has been granted. 2. Whether you've previously asked the user before. 3. Whether the user has selected the "Do not ask" option.
Check out the sample app included in the app
folder for an example of how it works.
Initialize this app on startup in your Application
class or some other singleton tied to the Application Context
.
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
PermissionsManager.init(this);
}
}
Make sure to include this Application
subclass in your AndroidManifest.xml
.
<manifest package="net.ralphpina.permissionsmanager.sample"
xmlns:android="http://schemas.android.com/apk/res/android">
<application
android:name=".MyApplication"
...
</application>
</manifest>
Make sure to include whatever permissions you will need in the AndroidManifest.xml
. <b>You still need to include the appropriate permissions in your manifest before asking for them at runtime.<b>
<manifest package="net.ralphpina.permissionsmanager.sample"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- ==== android.permission-group.CAMERA ==== -->
<uses-permission android:name="android.permission.CAMERA" />
...
</manifest>
PermissionsManager.get()
.isCalendarGranted()
PermissionsManager.get()
.hasAskedForCalendarPermission()
PermissionsManager.get()
.neverAskForCalendar(mActivity);
PermissionsManager.get()
.isCameraGranted()
PermissionsManager.get()
.hasAskedForCameraPermission()
PermissionsManager.get()
.neverAskForCamera(mActivity);
PermissionsManager.get()
.isContactsGranted()
PermissionsManager.get()
.hasAskedForContactsPermission()
PermissionsManager.get()
.neverAskForContacts(mActivity);
PermissionsManager.get()
.isLocationGranted()
PermissionsManager.get()
.hasAskedForLocationPermission()
PermissionsManager.get()
.neverAskForLocation(mActivity);
PermissionsManager.get()
.isAudioRecordingGranted()
PermissionsManager.get()
.hasAskedForAudioRecordingPermission()
PermissionsManager.get()
.neverAskForAudio(mActivity);
PermissionsManager.get()
.isCallingGranted()
PermissionsManager.get()
.hasAskedForCallingPermission()
PermissionsManager.get()
.neverAskForCalling(mActivity);
PermissionsManager.get()
.isStorageGranted()
PermissionsManager.get()
.hasAskedForStoragePermission()
PermissionsManager.get()
.neverAskForStorage(mActivity);
PermissionsManager.get()
.isBodySensorGranted()
PermissionsManager.get()
.hasAskedForBodySensorPermission()
PermissionsManager.get()
.neverAskForBodySensor(mActivity);
PermissionsManager.get()
.isSmsGranted()
PermissionsManager.get()
.hasAskedForSmsPermission()
PermissionsManager.get()
.neverAskForSms(mActivity);
If the user has selected "Never ask again" you can intent into the app's settings using the following:
PermissionsManager.get()
.intentToAppSettings(activity);
If the user selected "Never ask again", then they give you permissions in the app settings page, and then remove them, this method will return true. Even though at that point you can ask for permissions. I have not been able to figure out a way around this.
While there are methods to request some of the more common permissions, if you want to request multiple permissions at once:
PermissionsManager.get()
.requestPermissions(REQUEST_CAMERA_PERMISSION, REQUEST_LOCATION_PERMISSION)
.subscribe(new Action1<PermissionsResult>() {
@Override
public void call(PermissionsResult permissionsResult) {
if (permissionsResult.isGranted()) { // always true pre-M
// do whatever
}
if (permissionsResult.hasAskedForPermissions()) { // false if pre-M
// do whatever
}
}
});
If you plan on contributing, please make sure to update the README and Javadocs if there are API changes and add tests!
./gradlew generateReleaseJavadoc
Copyright 2015 Ralph Pina.
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.