What is TedPermission?
After the update to Android 6.0 Marshmallow, we have to not only declare permissions in AndroidManifest.xml
, but also request them at runtime. Furthermore, the user can turn permissions on/off anytime in application settings. When you use (https://developer.android.com/guide/topics/permissions/overview?hl=en#normal-dangerous)dangerous permissons(ex. CAMERA,
READ_CONTACTS,
READ_PHONE_STATE, ...), you must check and request them at runtime.
You can make your own permission check logic like this, but it's very complex, mainly because functions Google offer are very hard to use: checkSelfPermission()
, requestPermissions()
, onRequestPermissionsResult()
, onActivityResult()
.
TedPermission makes it easy to check and request android permissions.
(For Korean) 아래 블로그를 통해 마시멜로우 권한 관련된 사항을 알아보세요 http://gun0912.tistory.com/55
Demo
- Request permissions.
- If user denied permissions, a message dialog with a button to go to Settings will appear.
Setup
Gradle
Edit root/app/build.gradle
like below.
Normal
dependencies {
implementation 'gun0912.ted:tedpermission:x.y.z'
}
RxJava1
dependencies {
implementation 'gun0912.ted:tedpermission-rx1:x.y.z'
}
RxJava2
dependencies {
implementation 'gun0912.ted:tedpermission-rx2:x.y.z'
}
If you think this library is useful, please press the star button at the top.
How to use
Normal
-Make PermissionListener
We will use PermissionListener
for handling permission check result. You will get the result to onPermissionGranted()
or onPermissionDenied()
depending on approved permissions.
PermissionListener permissionlistener = new PermissionListener() {
@Override
public void onPermissionGranted() {
Toast.makeText(MainActivity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionDenied(List<String> deniedPermissions) {
Toast.makeText(MainActivity.this, "Permission Denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();
}
};
-Start TedPermission
TedPermission class requires setPermissionListener()
, setPermissions()
, and check()
methods. Call check()
to start checking for permissions.
setRationaleMessage()
and setDeniedMessage()
are optional methods for displaying messages.
TedPermission.with(this)
.setPermissionListener(permissionlistener)
.setDeniedMessage("If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
.setPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
.check();
RxJava1
If you use RxJava1, You can use request()
method instead check()
. When permission check has finished, you will receive TedPermissionResult
instance. TedPermissionResult
instance has isGranted()
, getDeniedPermissions()
methods for checking permission check result.
TedRxPermission.with(this)
.setDeniedMessage(
"If you reject permission,you can not use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
.setPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
.request()
.subscribe(tedPermissionResult -> {
if (tedPermissionResult.isGranted()) {
Toast.makeText(RxJava1Activity.this, "Permission Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(RxJava1Activity.this,
"Permission Denied\n" + tedPermissionResult.getDeniedPermissions().toString(), Toast.LENGTH_SHORT)
.show();
}
}, throwable -> {
});
RxJava2
RxJava2 api is very similiar to RxJava 1. You can use request()
method to request for permissions like RxJava1.
TedRx2Permission.with(this)
.setRationaleTitle(R.string.rationale_title)
.setRationaleMessage(R.string.rationale_message) // "we need permission for read contact and find your location"
.setPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
.request()
.subscribe(tedPermissionResult -> {
if (tedPermissionResult.isGranted()) {
Toast.makeText(this, "Permission Granted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this,
"Permission Denied\n" + tedPermissionResult.getDeniedPermissions().toString(), Toast.LENGTH_SHORT)
.show();
}
}, throwable -> {
});