One, foreword

The purpose of this document is to tease out several major changes and adaptations to android1.0~ Android11 location permissions.

If you are learning the adaptation of android11, Android10, android6 positioning permissions, then this is the best for you.

If you want to learn the mechanism of Android permissions, then this article is very suitable for you, through the local look at the global, positioning permissions is a very typical Android permissions.

If the above are not, you are just a passing Android development, then gently you come, click a collection and then gently walk.

If you’re not an Android developer, go away, you’re in the wrong place.

Positioning authority adapter pit will be more, and is not easy to describe, read a lot of related blog, mostly write more limited not easy to understand;

So uncle himself, try to write an easy to understand and practical blog.

If I do that, give it a thumbs up. Give it credit.

Second, several major changes to the Android location permission mechanism

The image above shows several major changes to the location permission application for native Android.

It is important to understand how to adapt location permissions.

Three, android in the three location permissions detailed explanation

<! -- Allows an app to access approximate location. Approximate location permission, API1, for example: network location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<! -- Allows an app to access precise location, api1, e.g. GPS location -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<! -- Allows an app to access location in the background. Background location permissions,api29, Android10 added -->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
Copy the code

For ease of expression:

We refer to ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION as common location permissions

ACCESS_BACKGROUND_LOCATION is called background location permission

  1. The network location permission belongs to ACCESS_COARSE_LOCATION

  2. The GPS location permission belongs to ACCESS_FINE_LOCATION

  3. Background location permission: ACCESS_BACKGROUND_LOCATION

    Google added the background location permission ACCESS_BACKGROUND_LOCATION in android10.

    Background location permission. As the name implies, if the APP does not obtain the background location permission, it will fail to obtain the location when the APP is in the background.

Four, need to pay special attention to three points:

  1. The ACCESS_BACKGROUND_LOCATION is sensitive and different from other new permissions.

    In theory, the new permissions added to android10 will not apply to apps with targetSdkVersion smaller than android10.

    But: Google to background location permissions to do very strict processing. Android10 phones on targetSDK less than Android10 also need to do both.

    But: Google to background location permissions to do very strict processing. Android10 phones on targetSDK less than Android10 also need to do both.

    But: Google to background location permissions to do very strict processing. Android10 phones on targetSDK less than Android10 also need to do both.

    For phones above Android10, even if targetSdkVersion is smaller than Android10, if the user does not take the initiative to authorize background positioning, the APP in the background will not be able to obtain positioning.

    Also, targetSdkVersion is smaller than Android10, and the popover for applying for normal location permissions has changed.

    Notice the first two options of the popover above;

    Notice the first two options of the popover above;

    Notice the first two options of the popover above;

    APP targetSdkVersion=26, Android10 system. ACCESS_FINE_LOCATION permission only:

    Allow only while using the app and Allow all the time:

  2. On Android 10, you can apply for ACCESS_FINE_LOCATION and ACCESS_BACKGROUND_LOCATION permissions in the same permission popup.

    The following code will pop up

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?). {
            super.onCreate(savedInstanceState)
            requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION), 100)}}Copy the code

    Only request Manifest. Permission. ACCESS_FINE_LOCATION permissions, only two buttons.

  3. On Android 11, the background location permissions have been adjusted again.

    When targetSdkVersion=android11, you cannot apply for common location permission and background location permission at the same time.

    You can apply for background location rights only after you have successfully applied for common location rights.

    Here be sure to note that targetSdkVersion = android11, in the Activity. The requestPermissions (), apply at the same time, the ordinary position location permissions, permissions, and the background will not play a window, authorization failure directly.

    Here be sure to note that targetSdkVersion = android11, in the Activity. The requestPermissions (), apply at the same time, the ordinary position location permissions, permissions, and the background will not play a window, authorization failure directly.

    Here be sure to note that targetSdkVersion = android11, in the Activity. The requestPermissions (), apply at the same time, the ordinary position location permissions, permissions, and the background will not play a window, authorization failure directly.

    The same code as the second point will fail this time

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?). {
            super.onCreate(savedInstanceState)
            //targetSdkVersion=android11, on Android11 phones, will directly authorize failure
         requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_BACKGROUND_LOCATION), 100)}}Copy the code

    In other words, if you want to get background location permission, you must pop the window twice:

    For the first time, apply for the popup of general location permission.

    The second time, after the user has agreed to the general location permission, the window pops up when applying for background location permission.

    Here also must pay attention to, in the absence of ordinary positioning rights, directly apply for background positioning rights, will not pop up directly authorized failure.

5. How to position the APP in the background if you only have general positioning permission?

Android 10 or higher device, only ordinary location permission, no Activity in the foreground, can the APP still get location?

The answer is yes.

Through location services.

The background location service must meet the following three conditions:

  1. The APP has at least general location permission

  2. Service must be the foregroundService foregroundService

  3. ForegroundServiceType must be declared in androidMenifest. XML; The following

    <service
        android:name="com.vivo.health.v2.notification.SportingNotificationService"
        android:foregroundServiceType="location"/>
    Copy the code

In this way, even if there is no foreground Activity, as long as the location service in the background, we can still get mobile phone location data.

Note: A flaw in locating a Service is that the Service must now be a foreground Service. That means you have to have a notification bar.

Note: A flaw in locating a Service is that the Service must now be a foreground Service. That means you have to have a notification bar.

Note: A flaw in locating a Service is that the Service must now be a foreground Service. That means you have to have a notification bar.