Android 权限旨在保护 Android 用户的隐私。当应用想要访问敏感数据类型(如联系人或照片)以及系统功能(如位置信息或步数检测)时,系统会提示用户。用户在首次下载您的应用时授予这些权限。
如果您的应用需要访问任何这些类型的数据(Google 健身的一部分数据类型),请先请求相关的 Android 权限,然后再请求 OAuth 权限。请参阅下文。
需要 Android 权限的数据类型
若要访问这些类型的身体活动数据,您需要请求 ACTIVITY_RECOGNITION
Android 权限:
-
com.google.step_count.delta
com.google.step_count.cumulative
com.google.step_count.cadence
com.google.activity.segment
com.google.calories.expended
-
com.google.step_count.delta
com.google.step_count.cumulative
com.google.step_count.cadence
com.google.activity.segment
com.google.activity.exercise
若要访问这些数据类型,您需要请求 ACCESS_FINE_LOCATION
Android 权限:
- 如需读取这些类型的数据,请执行以下操作:
com.google.distance.delta
com.google.location.sample
com.google.location.bounding_box
com.google.speed
若要记录这些数据类型,您需要请求 BODY_SENSORS
Android 权限:
com.google.heart_rate.bpm
请求 Android 权限
了解如何请求 Android 权限、身体活动识别权限、精确位置信息权限和身体传感器权限。
如需使用 Google Fit API 访问上述数据类型,您需要实现为 Android 10 及之前版本的 Android 请求 Android 权限的逻辑。这些示例使用了 ACTIVITY_RECOGNITION
权限。
Android 10
因此,您的应用能够以 API 级别 29 或更高级别为目标平台,向用户请求权限,并在应用清单文件中注册该权限。
-
<uses-permission android:name="android.permission.ACTIVITY_RECOGNITION"/>
-
if (ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.ACTIVITY_RECOGNITION) != PackageManager.PERMISSION_GRANTED) { // Permission is not granted }
如果尚未授予权限,请请求此权限:
ActivityCompat.requestPermissions(thisActivity, arrayOf(Manifest.permission.ACTIVITY_RECOGNITION), MY_PERMISSIONS_REQUEST_ACTIVITY_RECOGNITION)
Android 9 及更低版本
因此,您的应用能够以 API 级别 28 或更低级别为目标平台:
请求
com.google.android.gms.permission.ACTIVITY_RECOGNITION
权限。-
<uses-permission android:name="android.gms.permission.ACTIVITY_RECOGNITION"/>