0

Google Fit is giving me the following exceptions when I try subscribing to TYPE_STEP_COUNT_CUMULATIVE (cumulative steps) and TYPE_STEP_COUNT_DELTA using the RecordingClient (https://developers.google.com/android/reference/com/google/android/gms/fitness/RecordingClient)

failure: com.google.android.gms.common.api.ApiException: 10: SecurityException: com.google.step_count.cumulative requires android.permission.ACTIVITY_RECOGNITION

failure: com.google.android.gms.common.api.ApiException: 10: SecurityException: com.google.step_count.delta requires android.permission.ACTIVITY_RECOGNITION

This is Happening only for API Level 29 and above.

I tried including <uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/> and <uses-permission android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" /> in the manifest as recommended by Android ACTIVITY_RECOGNITION Permission SDK 28 running on Android 10/Q (SDK 29) but I'm not able to request permission from Main Activity.

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACTIVITY_RECOGNITION), REQUEST_CODE) // error occurs here
}

ACTIVITY_RECOGNITION is not recognized nor do I get it as a suggestion while typing.

Anirudh Ganesh
  • 446
  • 4
  • 22
  • Are you checking for the ACTIVITY_RECOGNITION permission during runtime? – EraftYps Jul 08 '20 at 04:20
  • 1
    I fixed it by changing the targetSdkVersion to 28 from 29. Although I think for 29, as mentioned in docs, I have to get runtime permission and for 28, in the manifest, mentioning should grant the permission automatically (https://developers.google.com/fit/android/authorization#android-9-and-below) although, if the user revokes the permission, that has to be handled. Will update once I give it a try. – Anirudh Ganesh Jul 08 '20 at 04:35
  • @EraftYps I tried it, but it's not even getting recognized. – Anirudh Ganesh Jul 20 '20 at 18:46

1 Answers1

2

I was targeting for API 28 in the gradle. Only after changing the target API29, the permission was getting recognized by the IDE. Google introduced permission for ACTIVITY_RECOGNITION for API 29 and above since it is considered as dangerous permission. For API 28 and below, the permission is auto granted during the run time but if the user removes the permission, it has to be handled as well. You can find more about this here and here

Anirudh Ganesh
  • 446
  • 4
  • 22