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

后端 未结 3 1050
无人及你
无人及你 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:19

    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.

    0 讨论(0)
  • 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
    0 讨论(0)
  • 2021-02-06 06:33

    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).

    0 讨论(0)
提交回复
热议问题