问题
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
回答1:
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.
回答2:
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:
- migrate to target SDK 29 (and use android.permission.ACTIVITY_RECOGNITION in manifest and checks/request) - we tested it and it works
- at the start of main activity or any suitable activity run the call above which will ask user for permission
回答3:
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).
来源:https://stackoverflow.com/questions/58017585/android-activity-recognition-permission-sdk-28-running-on-android-10-q-sdk-29