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

13,829

Solution 1

Looks like the documentation was updated: https://developer.android.com/about/versions/10/privacy/changes#physical-activity-recognition

I was able to solve this by only using the API 28 permission in the manifest (com.google.android.gms.permission.ACTIVITY_RECOGNITION).

If the app was installed on a device running Android 10 (API 29), the system appears to correctly auto grant the permission android.permission.ACTIVITY_RECOGNITION.

Because the user can change this in settings (Settings > Apps & Notifications > Permission Manager > Physical Activity > specified app > deny), I was able to check the android.permission.ACTIVITY_RECOGNITION permission if the app was installed on a device running Android 10 (API 29).

Solution 2

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.

Solution 3

Not sure if it helps for your issue but it helps us with similar problem. First check if your app/user has Physical activity permitted - most probably not. If you permit it - your code should run without exception.

Issue for us was how to detect that com.google.android.gms.permission.ACTIVITY_RECOGNITION is permitted (running in target sdk 28 on Android 10) - since call

PermissionCompat.isPermissionGranted(context,"com.google.android.gms.permission.ACTIVITY_RECOGNITION")

always returns true (even permission is denied)

workaround (for your app target sdk 28 runnning on Android 10) is to call requestPermission (instead of isPermissionGranted) which does not do anything when permission is granted and show dialog if not

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
        PermissionCompat.requestPermission(activity, "com.google.android.gms.permission.ACTIVITY_RECOGNITION", requestCode)
}

in case you are running background code where activity is not available you have two options:

  1. migrate to target SDK 29 (and use android.permission.ACTIVITY_RECOGNITION in manifest and checks/request) - we tested it and it works
  2. at the start of main activity or any suitable activity run the call above which will ask user for permission
Share:
13,829

Related videos on Youtube

Karringgton
Author by

Karringgton

Domain knowledge is primarily in Java and Android Apps with minimal AOSP experience.

Updated on June 04, 2022

Comments

  • Karringgton
    Karringgton about 2 years

    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
    
  • Karringgton
    Karringgton over 4 years
    While this is answer works for an Android app compiling against API 29 and up, that does not solve the problem/issue of the main question (Using device running API 29 but running application compiled against API 28)
  • Anirudh Ganesh
    Anirudh Ganesh almost 4 years
    The ACTIVITY_RECOGNITION permission does not show up in the list nor is getting recognized for me. Do I need to include any gradle dependency for this? I've included both <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_REC‌​OGNITION" /> <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/> in my AndroidManifest.
  • CoolMind
    CoolMind over 3 years
    @AnirudhGanesh, thanks! Also we can ask permission "com.google.android.gms.permission.ACTIVITY_RECOGNITION" as noted in @ tritot answer.