Android ACTIVITY RECOGNITION permission missing

Viewed 5152

I am targeting SDK 29, and activity transition API works perfectly on devices running 29 (ie Android 10). According to the docs, if you are targeting SDK 29, but running on a lower SDK, the permission should be granted automatically.

I have tried both, separately and together

<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_REC‌​OGNITION"/>

I can't ask for the permission in a pop-up ( nothing happens). When I try to register the Transitions API on phones < Andorid 10, I get the error:

com.google.android.gms.common.api.b: 10: SecurityException: Activity detection usage requires the ACTIVITY_RECOGNITION permission

If I set the target SDK to 28, everything works perfectly on all phones, but I need to target 29 for other reasons. I am pulling my hair out. To be clear, it works perfectly on Android 10, just not below. Further code for clarification:

public void askForActivityPermission(View v) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            Log.d(TAG, "askForActivityPermission: q or greater");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACTIVITY_RECOGNITION}, PERMISSION_REQUEST_ACTIVITY_RECOGNITION);
        } else {
            Log.d(TAG, "askForActivityPermission: less than q");
            moveNextStep();
        }
    }

    @Override
    public void onRequestPermissionsResult(
            int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {

        String permissionResult = "Request code: " + requestCode + ", Permissions: " +
                Arrays.toString(permissions) + ", Results: " + Arrays.toString(grantResults);

        Log.d(TAG, "onRequestPermissionsResult(): " + permissionResult);

        if (requestCode == PERMISSION_REQUEST_ACTIVITY_RECOGNITION) {
            Log.d(TAG, "onRequestPermissionsResult: permission granted?");
            moveNextStep();

        }
3 Answers

There were some updates in API Level 29.

1.Add the permission to the manifest file.

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

2.Check if the permission is granted:

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

3.If permission isn't already granted, request the permission:

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

Here are some useful docs.

https://developer.android.com/about/versions/10/privacy/changes#physical-activity-recognition

https://developers.google.com/fit/android/authorization

Ok, i have got it working, but it makes no sense, what so ever, like none.

<uses-permission android:name="com.google.android.gms.permission.ACTIVITY_REC‌​OGNITION" />

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

The top one does not work, the bottom one does. Can anyone tell me why?

I have tried all the suggested solutions, but none of them worked out. I've tried all combinations of com.google.android.gms.permission.ACTIVITY_REC‌​OGNITION and com.google.android.gms.permission.ACTIVITY_RECOGNITION

with minSdkVersion 26 and targetSdkVersion 30

using an Android 9 (API 28) Wear OS emulator.

Related