Regarding the ACCESS_BACKGROUND_LOCATION permission application, the problem record that caused the permission dialog box to not pop up

After Android 6.0, for permissions that need to be dynamically applied for, the general steps are as follows:

1. Add the corresponding permissions in the Manifest;
2. Use the checkSelfPermission method of ActivityCompat or ContextCompat to check whether there are permissions; 3. If there is no permission, use the requestPermissions method of ActivityCompat to apply for the corresponding permissions. 4. Rewrite the onRequestPermissionsResult method to monitor the permission authorization results

For positioning permissions, you generally need to apply for the ACCESS_COARSE_LOCATION permission or the ACCESS_FINE_LOCATION permission . When targetSdkVersion >= 29 , you also need to apply for the ACCESS_BACKGROUND_LOCATION permission .
For example:

private fun checkPermissions() {
    
    
        val mutableListOf = mutableListOf<String>()
        for (permission: String in permissions) {
    
    
            if (ActivityCompat.checkSelfPermission(
                    this,
                    permission
                ) != PackageManager.PERMISSION_GRANTED
            ) {
    
    
                mutableListOf.add(permission)
                Log.d("PermissionTag", "has no $permission permission")
            }
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    
    
            if (ACCESS_BACKGROUND_LOCATION.isNotEmpty() && ActivityCompat.checkSelfPermission(
                    this,
                    ACCESS_BACKGROUND_LOCATION
                ) != PackageManager.PERMISSION_GRANTED
            ) {
    
    
                mutableListOf.add(ACCESS_BACKGROUND_LOCATION)
                Log.d("PermissionTag", "has no background location permission")
            }
        }
        if (mutableListOf.isNotEmpty()) {
    
    
            val permissions = mutableListOf.toTypedArray()
            ActivityCompat.requestPermissions(this, permissions, 0x01)
        }
    }

Among them, permissions and ACCESS_BACKGOUND_LOCATION are defined permission constants

val permissions = arrayOf(
    Manifest.permission.ACCESS_FINE_LOCATION,
    Manifest.permission.CHANGE_WIFI_STATE,
    Manifest.permission.WRITE_EXTERNAL_STORAGE
)

val ACCESS_BACKGROUND_LOCATION =
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) Manifest.permission.ACCESS_BACKGROUND_LOCATION else ""

However, when the app's targetSdkVersion>=30 , if you want to apply for the ACCESS_BACKGOUND_LOCATION permission , you need to apply for the ACCESS_COARSE_LOCATION permission or the ACCESS_FINE_LOCATION permission before you can apply for the ACCESS_BACKGROUND_LOCATION permission . If the ACCESS_BACKGROUND_LOCATION permission is applied together with the ACCESS_COARSE_LOCATION permission or the ACCESS_FINE_LOCATION permission , the permission application dialog box will not pop up. If other permissions are applied together, all permission application windows will not pop up.

Solution:
Method 1. targetSdkVersion<=2;
Method 2. First apply for the ACCESS_COARSE_LOCATION permission or the ACCESS_FINE_LOCATION permission . After the application is passed, apply for the ACCESS_BACKGROUND_LOCATION permission .

For an introduction to positioning permissions, refer to Positioning Permissions

Guess you like

Origin blog.csdn.net/fengyulinde/article/details/129556403