Top Related Projects
A declarative API to handle Android runtime permissions.
Android runtime permissions powered by RxJava2
Simplify Android M system permissions
Android library that simplifies the process of requesting permissions at runtime.
Quick Overview
TedPermission is an Android library that simplifies the process of requesting runtime permissions in Android applications. It provides an easy-to-use API for handling permission requests, making it more convenient for developers to implement and manage permissions in their apps.
Pros
- Simplifies the implementation of runtime permissions in Android apps
- Supports both Kotlin and Java
- Offers customizable UI for permission dialogs
- Provides callback methods for handling permission results
Cons
- May add unnecessary overhead for simple permission requests
- Requires additional dependency in the project
- Limited customization options for complex permission scenarios
- May not be necessary for developers comfortable with Android's native permission system
Code Examples
- Basic permission request:
TedPermission.create()
.setPermissions(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
.check()
- Permission request with callbacks:
TedPermission.create()
.setPermissions(Manifest.permission.CAMERA)
.setDeniedMessage("Camera permission is required for this feature")
.setPermissionListener(object : PermissionListener {
override fun onPermissionGranted() {
// Permission granted, proceed with camera functionality
}
override fun onPermissionDenied(deniedPermissions: List<String>) {
// Handle denied permissions
}
})
.check()
- Custom UI for permission dialog:
TedPermission.create()
.setPermissions(Manifest.permission.ACCESS_FINE_LOCATION)
.setRationaleTitle(R.string.rationale_title)
.setRationaleMessage(R.string.rationale_message)
.setDeniedTitle(R.string.denied_title)
.setDeniedMessage(R.string.denied_message)
.setGotoSettingButtonText(R.string.settings_button_text)
.check()
Getting Started
- Add the dependency to your app's
build.gradle
file:
dependencies {
implementation 'io.github.ParkSangGwon:tedpermission-normal:3.3.0'
}
- Initialize TedPermission in your activity or fragment:
import com.gun0912.tedpermission.PermissionListener
import com.gun0912.tedpermission.normal.TedPermission
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
TedPermission.create()
.setPermissions(Manifest.permission.CAMERA)
.setPermissionListener(object : PermissionListener {
override fun onPermissionGranted() {
// Permission granted
}
override fun onPermissionDenied(deniedPermissions: List<String>) {
// Permission denied
}
})
.check()
}
}
Competitor Comparisons
A declarative API to handle Android runtime permissions.
Pros of PermissionsDispatcher
- Annotation-based API: PermissionsDispatcher provides an annotation-based API, which makes it easier to manage runtime permissions in Android applications. This can lead to more concise and readable code.
- Automatic code generation: PermissionsDispatcher automatically generates the necessary boilerplate code for handling runtime permissions, reducing the amount of manual work required.
- Flexible configuration: PermissionsDispatcher allows for flexible configuration, such as setting custom rationale messages and request codes.
Cons of PermissionsDispatcher
- Dependency on annotation processing: PermissionsDispatcher relies on annotation processing, which can increase the build time of your project.
- Limited support for Kotlin: While PermissionsDispatcher does support Kotlin, the annotation-based approach may not be as natural as in Java.
- Potential for conflicts with other libraries: PermissionsDispatcher may not always play well with other libraries that also use annotation processing.
Code Comparison
Here's a brief code comparison between PermissionsDispatcher and TedPermission:
PermissionsDispatcher (Java):
@NeedsPermission(Manifest.permission.CAMERA)
void showCamera() {
// Camera functionality
}
@OnShowRationale(Manifest.permission.CAMERA)
void showRationaleForCamera(PermissionRequest request) {
// Explain why the permission is needed
}
TedPermission (Kotlin):
TedPermission.with(this)
.setPermissions(Manifest.permission.CAMERA)
.setRationaleMessage("This app needs access to your camera.")
.check {
if (it.isGranted) {
// Camera functionality
} else {
// Handle denied permission
}
}
Android runtime permissions powered by RxJava2
Pros of RxPermissions
- Integrates seamlessly with RxJava, allowing for reactive programming patterns
- Lightweight and focused solely on runtime permissions
- Supports chaining multiple permission requests
Cons of RxPermissions
- Requires knowledge of RxJava, which may have a steeper learning curve
- Less feature-rich compared to TedPermission (e.g., no custom UI options)
- May not be suitable for projects not already using RxJava
Code Comparison
TedPermission:
TedPermission.create()
.setPermissions(Manifest.permission.CAMERA)
.setDeniedMessage("Camera permission is required")
.check()
RxPermissions:
RxPermissions(this)
.request(Manifest.permission.CAMERA)
.subscribe { granted ->
if (granted) {
// Permission granted
}
}
Both libraries simplify the process of requesting runtime permissions, but RxPermissions leverages RxJava's Observable pattern, while TedPermission uses a more traditional callback approach. TedPermission offers more built-in features like custom messages and UI, while RxPermissions focuses on integrating with reactive programming workflows.
Simplify Android M system permissions
Pros of EasyPermissions
- Simplified Permission Handling: EasyPermissions provides a straightforward API for requesting and handling runtime permissions, making it easier to manage permission-related tasks in your app.
- Compatibility with Android 6.0+: EasyPermissions is designed to work with the runtime permission model introduced in Android 6.0 (Marshmallow), ensuring compatibility with modern Android versions.
- Callback-based Approach: EasyPermissions uses a callback-based approach, allowing you to handle permission requests and responses directly in your app's code.
Cons of EasyPermissions
- Limited Customization: EasyPermissions has a relatively limited set of customization options, which may not meet the needs of all developers who require more fine-grained control over the permission handling process.
- Dependency on Google Play Services: EasyPermissions relies on the Google Play Services library, which may not be suitable for all app scenarios, particularly those targeting devices without Google Play Services.
- Potential Overhead: The use of an additional library, even a lightweight one like EasyPermissions, can add some overhead to your app's codebase and build process.
Code Comparison
EasyPermissions:
public void requestPermissions() {
String[] perms = {Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION};
if (!EasyPermissions.hasPermissions(this, perms)) {
EasyPermissions.requestPermissions(
this,
"This app needs access to your camera and location.",
REQUEST_CODE_PERMS,
perms);
}
}
TedPermission:
fun requestPermissions() {
TedPermission.with(this)
.setPermissions(Manifest.permission.CAMERA, Manifest.permission.ACCESS_FINE_LOCATION)
.setRationaleMessage("This app needs access to your camera and location.")
.setDeniedMessage("If you reject permission, you cannot use this service\n\nPlease turn on permissions at [Setting] > [Permission]")
.check()
}
Android library that simplifies the process of requesting permissions at runtime.
Pros of Dexter
- More flexible and customizable permission handling
- Supports chaining multiple permission requests
- Provides a cleaner API with less boilerplate code
Cons of Dexter
- Less actively maintained (last update in 2019)
- Fewer built-in UI options for permission dialogs
- Slightly steeper learning curve for beginners
Code Comparison
TedPermission:
TedPermission.with(context)
.setPermissionListener(permissionListener)
.setDeniedMessage("Permission denied message")
.setPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
.check()
Dexter:
Dexter.withContext(context)
.withPermissions(Manifest.permission.READ_CONTACTS, Manifest.permission.ACCESS_FINE_LOCATION)
.withListener(compositePermissionListener)
.check()
Both libraries simplify Android runtime permissions, but Dexter offers more flexibility in handling complex permission scenarios. TedPermission provides more out-of-the-box UI options and is easier for beginners to use. Dexter's API is slightly more concise and allows for more advanced permission handling, while TedPermission focuses on simplicity and ease of use. The choice between the two depends on the specific needs of your project and your preference for API design.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
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 dangerous permissons(ex. CAMERA
, READ_CONTACTS
, READ_PHONE_STATE
, ...), you must check and request them at runtime.
(https://developer.android.com/guide/topics/permissions/overview?hl=en#normal-dangerous)
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
- Edit
root/app/build.gradle
like below. - You can choose only one library depend on your code style
normal
/coroutine
/rxJava2
/rxJava3
- Replace
x.y.z
with the version shown in the 'Maven Central' button below, or the specific version you want (e.g. replacex.y.z
with3.4.2
if you want 3.4.2).
repositories {
google()
mavenCentral()
}
dependencies {
// Normal
implementation("io.github.ParkSangGwon:tedpermission-normal:3.4.2")
// Coroutine
implementation("io.github.ParkSangGwon:tedpermission-coroutine:3.4.2")
// RxJava2
implementation("io.github.ParkSangGwon:tedpermission-rx2:3.4.2")
// RxJava3
implementation("io.github.ParkSangGwon:tedpermission-rx3:3.4.2")
}
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.create()
.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();
Coroutine
If you use kotlin and coroutine, You can use check()
function.
TedPermissionResult
instance has isGranted()
, getDeniedPermissions()
methods for checking permission check result.
val permissionResult =
TedPermission.create()
.setPermissions(Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.ACCESS_FINE_LOCATION)
.check()
Also if you want know only granted result, you can use checkGranted(): boolean
RxJava
If you use RxJava, 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.
TedPermission.create()
.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 -> {
});
Customize
TedPermission supports the following methods.
setGotoSettingButton(boolean) (default: true)
setRationaleTitle(R.string.xxx or String)
setRationaleMessage(R.string.xxx or String)
setRationaleConfirmText(R.string.xxx or String) (default: confirm / íì¸)
setDeniedTitle(R.string.xxx or String)
setDeniedMessage(R.string.xxx or String)
setDeniedCloseButtonText(R.string.xxx or String) (default: close / ë«ê¸°)
setGotoSettingButtonText(R.string.xxx or String) (default: setting / ì¤ì )
Also you can use the following utility functions.
isGranted(String... permissions)
: Check if all permissions are grantedisDenied(String... permissions)
: Check if all permissions are deniedgetDeniedPermissions(String... permissions)
canRequestPermission(Activity activity, String... permissions)
: Iftrue
you can request a system popup,false
means user checkedNever ask again
.startSettingActivityForResult()
startSettingActivityForResult(int requestCode)
Number of Cases
-
Check permissions -> Already have permissions
:onPermissionGranted()
is called. -
Check permissions -> Don't have permissions
: Request dialog is shown.
-
Show request dialog -> User granted permissions
:onPermissionGranted()
is called. -
Show request dialog -> User denied one or more permissions
: Denied dialog is shown.
-
Show denied dialog -> Close the dialog
:onPermissionDenied()
called -
Show denied dialog -> Setting button clicked
:startActivityForResult()
to application Setting Activity.
-
Setting Activity ->
onActivityResult()
: Check permissions again -
Check permission -> Permissions are granted
:onPermissionGranted()
is called. -
Check permission -> There are denied permissions
:onPermissionDenied()
is called.
License
Copyright 2021 Ted Park
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.
Top Related Projects
A declarative API to handle Android runtime permissions.
Android runtime permissions powered by RxJava2
Simplify Android M system permissions
Android library that simplifies the process of requesting permissions at runtime.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot