Solve the problem that Android 10 / Android Q phones cannot locate normally in the background

Android 10 was officially released in September 2019, bringing a very significant change in GPS permissions. It provides users with the information that is only allowed when using this app. Once the user selects "allow only when using this application", the app will not be able to record the GPS track normally in the background or when locking the screen, which has a great impact on didi travel, bike sharing and running software.

In response to this change, Google has also given a new solution.

Step 1: upgrade SDK

Modify build.gradle and upgrade app's compilesdkversion and targetsdkversion.

android {
  compileSdkVersion 29
  defaultConfig {
    targetSdkVersion 29
  }
}

Step 2: add background positioning permission

Modify the androidmanifest.xml file and add access_ BACKGROUND_ Location permission, and add Android: foregroundservicetype = "location" to the corresponding service.

<manifest >
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
  <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
  <application>
    <service android:name=".ExampleService" android:foregroundServiceType="location" />
  </application>
</manifest>

Step 3: apply for background positioning permission

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
  ActivityCompat.requestPermissions(this,arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION,ACCESS_BACKGROUND_LOCATION
  ),101)
} else {
  ActivityCompat.requestPermissions(this,arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION),101)
}

Through the above methods, you can obtain whether the user allows app background positioning. If the user does not allow it, the following dialog box will be prompted.

The above is the whole content of this article. I hope it will help you in your study, and I hope you will support us a lot.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>