Android ACTIVITY_RECOGNITION Permission SDK 28 running on Android 10/Q (SDK 29)

Karringgton picture Karringgton · Sep 19, 2019 · Viewed 8.8k times · Source

My Android app targets SDK 28 and connects to Google Fit to upload data and read some other data. The app uses the HistoryAPI to read com.google.step_count.delta data.

This documentation claims that "com.google.android.gms.permission.ACTIVITY_RECOGNITION permission is converted into a pre-granted runtime permission" if the app targets SDK 28 but runs on SDK 29: https://developers.google.com/fit/android/authorization#android_permissions

I have added to the app's manifest like the documentation says to do.

When this Android app is on a device running Android 10 (SDK 29) and the user connects to Google Fit for the first time, I get a log saying:

There was a problem subscribing.com.google.android.gms.common.api.ApiException: 10: SecurityException: com.google.step_count.delta requires android.permission.ACTIVITY_RECOGNITION

Yet the documentation claims that this will be converted into a pre-granted runtime permission.

The team is not ready to migrate the app's target SDK to 29 just yet, so how can we continue to get com.google.step_count.delta data without this error?

I am assuming that this log means it didn't actually connect as there was no log statement that said:

Successfully subscribed to com.google.step_count.delta

Answer

Kaushal Sachan picture Kaushal Sachan · Dec 12, 2019

Solved: In App Api Level 28 +

<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>

The app should check if the permission is granted already:

if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.ACTIVITY_RECOGNITION)
          != PackageManager.PERMISSION_GRANTED) {
      // Permission is not granted
}

To request the permission:

ActivityCompat.requestPermissions(thisActivity,
                  arrayOf(Manifest.permission.ACTIVITY_RECOGNITION),
                  MY_PERMISSIONS_REQUEST_ACTIVITY_RECOGNITION);

Learn more about requesting Android runtime permissions.

If your app targets SDK level 28 or below, it must specify the com.google.android.gms.permission.ACTIVITY_RECOGNITION permission in its manifest file.