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.
To get a Git project into your build:
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'
}
First declare your permissions in the manifest file. Example:
<uses-permission android:name="android.permission.CAMERA" />
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();
}
});
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();
}
});
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);
}
On permanent deny of any permission requested, navigating to application settings to enable the permanent denied permission promt is enabled by default.
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.
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.