Permissions
Guide to Implementing and Handling Runtime Permissions for Location in Your Android SDK (CafSmartAuth)
In Android, certain permissions, especially those related to sensitive data like location, require runtime approval from users. This guide explains how to check and request location permissions programmatically in your SDK, CafSmartAuth, using the checkPermissionStatus
function provided below.
Why CafSmartAuth Requires Location Permissions
CafSmartAuth requires access to the user’s location as part of its fraud prevention features, ensuring an additional layer of security by verifying location-based information. Android distinguishes between two types of location access, allowing users to control the precision of the location data they share:
ACCESS_FINE_LOCATION: Required for precise location information from GPS, which is essential for detecting potentially fraudulent activities with high accuracy.
ACCESS_COARSE_LOCATION: Provides approximate location data based on Wi-Fi and cellular networks, suitable for basic location checks.
With these permission levels, users can provide the minimum required access for fraud prevention, while CafSmartAuth ensures compliance with Android's data privacy practices.
Step-by-Step: Adding and Using checkPermissionStatus
checkPermissionStatus
Add Permissions to the Manifest
In your app's
AndroidManifest.xml
, include both location permissions:These manifest declarations allow the app to request these permissions but don’t automatically grant them.
Define a Request Code Constant
To identify the permission request, use a constant for the request code:
Create and Use
checkPermissionStatus
to Handle PermissionsThe
checkPermissionStatus
function checks if permissions have been granted and, if not, requests them from the user. This is essential for compliance with Android’s runtime permission model introduced in Android 6.0 (API level 23).Here’s the code and an explanation of how it works:
ContextCompat.checkSelfPermission
: This checks if the app has a specific permission. If the permission isn’t granted, it returnsPackageManager.PERMISSION_DENIED
.ActivityCompat.requestPermissions
: Requests the specified permission(s) from the user, displaying a system dialog for approval.
Handle the Permission Result
When requesting permissions, you can handle the result in
onRequestPermissionsResult
to manage user responses appropriately.Call
checkPermissionStatus
at the Right TimeYou might want to call
checkPermissionStatus
withinonResume
or when location functionality is triggered, ensuring permissions are checked before they’re needed.
Last updated