编程知识 cdmana.com

How to correctly request location permission in Android 11? And Android 8 - 11 location permission changes and adaptation methods!

Because now location information becomes sensitive data , therefore Android Limits its use , Especially in APP backstage .

stay Android 9 Before , Location permission is not separated according to the background ,APP Use the same resources in the front and back office .

however ,Google Starting to realize something APP Abuse of such data , So I decided to follow The front desk and backstage The way resources are separated , Add some layers to protect the user's location information data .

about Android For developers , That's why we're in APP Asking for location permission in add some extra work .

Before we go into detail , Let's take a look at the recent Android Change log in version :

  • stay Android 8 in : Applications in the background , Only a few times per hour can the user's location be retrieved .
  • Android 10 Before : Location permission is a single resource , Applications can be used everywhere with only one authorization , Use at any time ( Front and back )
  • stay Android 10 in : The background location becomes a separate resource , Applications in addition to foreground requests , This permission must also be explicitly requested .
  • Android 11 in : Background location permissions cannot be requested at the same time as others , The application must request separately . Besides , Asking for this permission will not prompt the user immediately like other permissions , It will bring users to / Settings page / Location jurisdiction conversation , So that users can update the permission level .

Based on these changes , We have to process location permission requests based on different system versions , Here you are 3 Different scenes ( In writing this article , We are using Android 11):

Android 10 Before

Location permission only needs to be requested once , At the front and back APP Can be used

@TargetApi(28)
fun Context.checkLocationPermissionAPI28(locationRequestCode : Int) {
    if (!checkSinglePermission(Manifest.permission.ACCESS_FINE_LOCATION) ||
        !checkSinglePermission(Manifest.permission.ACCESS_COARSE_LOCATION)) {
        val permList = arrayOf(
            Manifest.permission.ACCESS_FINE_LOCATION,
            Manifest.permission.ACCESS_COARSE_LOCATION
        )
        requestPermissions(permList, locationRequestCode)
    }
}

private fun Context.checkSinglePermission(permission: String) : Boolean {
    return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
}

In these versions , Users only have 2 An option : to grant authorization or Not authorized .

Android 10

In this version , Added ACCESS_BACKGROUND_LOCATION, You can request this permission to get both foreground and background permissions , Like the following :

@TargetApi(29)
private fun Context.checkLocationPermissionAPI29(locationRequestCode : Int) {
    if (checkSinglePermission(Manifest.permission.ACCESS_FINE_LOCATION) &&
        checkSinglePermission(Manifest.permission.ACCESS_COARSE_LOCATION) &&
        checkSinglePermission(Manifest.permission.ACCESS_BACKGROUND_LOCATION)) return
    val permList = arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, 
                           Manifest.permission.ACCESS_COARSE_LOCATION,
                           Manifest.permission.ACCESS_BACKGROUND_LOCATION)
    requestPermissions(permList, locationRequestCode)
    
}

private fun Context.checkSinglePermission(permission: String) : Boolean {
    return ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED
}

Again , If you ask for permission from the front desk (ACCESS_COARSE_LOCATION perhaps ACCESS_FINE_LOCATION), be Android The operating system will automatically set the background permissions (ACCESS_BACKGROUND_LOCATION) Add to request . It is similar to <uses-permission> Statement , Whether it's ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION Be declared ,ACCESS_BACKGROUND_LOCATION Will be added during installation .

Users now have three options : backstage ( At any time ) The front desk ( Use only APP period ) Refuse  .

Android 11

In addition to the above , There are other steps that developers need to add .

Here you are 2 Kinds of scenes , The first is when only foreground permissions are requested , under these circumstances , We usually use ACCESS_FINE_LOCATION  perhaps ACCESS_COARSE_LOCATION, however , The pop-up window for authorization is slightly different from the previous one , stay Android 11 in ,Google Added an option  Only this time ,

Please note that , Even if ACCESS_BACKGROUND_LOCATION Add to the list of permissions to be requested , The system will also ignore it .

The second situation is : Applications also need background permissions , So , You have to prepare your own dialog , And use clear messages to explain the use of the background location .

When the user agrees , Guide him to the application settings page , Where you can choose what he wants to award ** Authority level **.

The example is as follows :

@TargetApi(30)
private fun Context.checkBackgroundLocationPermissionAPI30(backgroundLocationRequestCode: Int) {
    if (checkSinglePermission(Manifest.permission.ACCESS_BACKGROUND_LOCATION)) return
    AlertDialog.Builder(this)
        .setTitle(R.string.background_location_permission_title)
        .setMessage(R.string.background_location_permission_message)
        .setPositiveButton(R.string.yes) { _,_ ->
            // this request will take user to Application's Setting page
            requestPermissions(arrayOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION), backgroundLocationRequestCode)
        }  
        .setNegativeButton(R.string.no) { dialog,_ ->
            dialog.dismiss()
        } 
        .create()
        .show()
        
}

As shown in the figure :

As you can see ,Android 11 in , We have 4 Permission levels are used for location information .

When the user chooses Allow all the time, APP You have the right to use location information in the background .

look , This is it. Android 11 The whole process of obtaining location permission in , And the different processing of each system version , I hope this blog will be useful to you !!!

Link to the original text : https://medium.com/swlh/request-location-permission-correctly-in-android-11-61afe95a11ad

版权声明
本文为[shzwork]所创,转载请带上原文链接,感谢

Scroll to Top