Android Permission

Below Android 5

Before Android 5, permission is quite simple.
Developers need to add permission

Example

<!-- Internet -->
<uses-permission android:name="android.permission.INTERNET" />

Android 6 Above

Above Android 6, developers have to care about permission carefully.
According to permission level, developers have to show Permission Dialog to get permission from user.
The user can select whether this function.

Users can change setting from Android “setting” after selecting

Permission Level

In Android, there are 2.

  • normal
  • dangerous

To use dangerous level, we need to ask users whether it is acceptable or not.
For normal, it’s same as under Android 5, just add permission to AndroidManifest.xml

Permission : Manifest.permission

Permission Check

Above Android 6, we need to care about permission is enable or not(because the user can select).
Before using permission, we recommend to check permission)
(Also, we need to handle SecurityException)

Location

As for location, we have 2 permissions (Internet and GPS)

fun locationpermission(context : Context) : Boolean {
        return ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED
        && ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED
    }

Permission Dialog and callback

To get permission from users, we need to show permission dialog using Android function.

 fun locationPermissionAction(activity: Activity) {
        var array : Array<String> = arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.ACCESS_COARSE_LOCATION)
        if (ActivityCompat.shouldShowRequestPermissionRationale(activity, android.Manifest.permission.ACCESS_FINE_LOCATION)
                && ActivityCompat.shouldShowRequestPermissionRationale(activity, android.Manifest.permission.ACCESS_COARSE_LOCATION)) {
            ActivityCompat.requestPermissions(activity, array, 0)
        } else {
            ActivityCompat.requestPermissions(activity, array, 0)
        }
    }

Callback

This request is activity base. We can use Activity Result callback.