Android ACTIVITY_RECOGNITION Permission SDK 28 running on Android 10/Q (SDK 29)

后端 未结 3 1051
无人及你
无人及你 2021-02-06 05:30

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.

T

3条回答
  •  伪装坚强ぢ
    2021-02-06 06:20

    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:

    1. migrate to target SDK 29 (and use android.permission.ACTIVITY_RECOGNITION in manifest and checks/request) - we tested it and it works
    2. at the start of main activity or any suitable activity run the call above which will ask user for permission

提交回复
热议问题