3

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();

        }
Geordie Wicks
  • 1,065
  • 1
  • 11
  • 27

3 Answers3

2

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

Mitch
  • 576
  • 5
  • 11
1

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.

Abel Stuker
  • 89
  • 1
  • 3
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/32311625) – Bruno Rohée Jul 28 '22 at 12:23
0

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?

Geordie Wicks
  • 1,065
  • 1
  • 11
  • 27
  • If your app targets Android 9 (API level 28) or lower, the system auto-grants the android.permission.ACTIVITY_RECOGNITION permission to your app, as needed, if your app satisfies each of the following conditions: 1. The manifest file includes the com.google.android.gms.permission.ACTIVITY_RECOGNITION permission. 2. The manifest file doesn't include the android.permission.ACTIVITY_RECOGNITION permission. If the system-auto grants the android.permission.ACTIVITY_RECOGNITION permission, your app retains the permission after you update your app to target Android 10. – Mitch Jun 13 '20 at 15:20
  • My app targets 29. It contains both android.permission.ACTIVITY_RECOGNITION and com.google.android.gms.permission.ACTIVITY_REC‌​OGNITION. The weird issue was, if i comment out the bottom permission above, the app does not work, if I comment out the top permission it does work, despite them seemingly being exactly the same. I have reproduced this issue multiple times. Just one of those super weird android issues I guess, I just wasted a whole day on it. One string does not work, an identical string does. – Geordie Wicks Jun 13 '20 at 15:23
  • Ok another weird discovery....there are two invisible characters between the "C" and the "O" in the top permission. These characters do not show up in any text editor, but when you move the cursor via arrow keys, they are there. Basically it was not working due to a spelling error in the permission that you can't see. – Geordie Wicks Jun 13 '20 at 15:31
  • It kinda sounds like manifest is bugged. You could give clean project a try Build -> Clean Project. – Mitch Jun 13 '20 at 15:41
  • Because your using Target API level 29 I would ditch android:name="com.google.android.gms.permission.ACTIVITY_RECOGNITION" and use android:name="android.permission.ACTIVITY_RECOGNITION" – Mitch Jun 13 '20 at 15:44
  • you need both for phones that are running less than Android 9. On more research it appears somehow my manifest got a few Zero Width Space characters in there, I have no idea how. A clean and rebuild did not help. They are almost impossible to find. It's a new one for me. Thanks for all your help @this.mitch – Geordie Wicks Jun 14 '20 at 00:37