android.permission.ACTIVITY_RECOGNITION not getting recognized while trying to request for user permission

1.5k Views Asked by At

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.

1

There are 1 best solutions below

0
On BEST ANSWER

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