在 Android 应用中使用 Pushwoosh 位置模块
Android 中有两种位置访问类别:
- 前台位置访问
- 后台位置访问
要在前台更新用户位置,应满足以下条件之一:
- 应用的某个活动处于活动状态
- 为应用启动了 foregroundServiceType = “location” 的前台服务
后台访问允许应用在不活动时获取用户地理位置,且未为该应用启动“location”类型的前台服务。
前台位置访问
Anchor link to如果您打算仅在应用处于活动状态时使用 Pushwoosh 位置模块更新用户位置,请将 pushwoosh-location 模块添加到您的 app/build.gradle 中:
implementation 'com.pushwoosh:pushwoosh-location:6.+'要启动该模块,请使用 startLocationTracking 方法:
PushwooshLocation.startLocationTracking();PushwooshLocation.startLocationTracking()在应用处于后台时使用 ForegroundService 发出位置请求
Anchor link to要在调用 startLocationTracking 方法时启动 ForegroundService(这允许在应用处于后台时获取用户地理位置),请按照上一段中描述的步骤操作,然后将以下元数据添加到您的 AndroidManifest.xml 中:
...<application>.... <meta-data android:name="com.pushwoosh.start_foreground_service" android:value="true" />...</application>...当 ForegroundService 启动时,会创建一个通知。您可以通过在项目的 AndroidManifest.xml 中指定附加元数据来自定义通知:
...<application>.... <meta-data android:name="com.pushwoosh.foreground_service_notification_text" android:value="My notification text" /> <meta-data android:name="com.pushwoosh.foreground_service_notification_channel_name" android:value="My notification channel name" />...</application>...后台位置访问
Anchor link to要在没有 ForegroundService 启动的情况下在后台更新地理位置,您需要满足以下条件:
- 获得 Google Play 对您的应用在后台访问位置的批准(详情请参阅 https://support.google.com/googleplay/android-developer/answer/9799150?hl=en)
- 将权限添加到您的 AndroidManifest.xml 中:
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />- 在前台位置权限获得后,请求用户在后台更新位置的权限(请在 Android 开发者文档中查找指南:https://developer.android.com/training/location/permissions#background-dialog-target-android-11)
在 6.3.2 版本的 pushwoosh-location 模块中,我们包含了一个额外的方法,如果满足以下条件,该方法将打开位置权限设置屏幕:
- 应用已获得前台位置权限;
- 应用的 AndroidManifest.xml 中存在
ACCESS_BACKGROUND_LOCATION用户权限。
按如下方式调用该方法:
PushwooshLocation.requestBackgroundLocationPermission();PushwooshLocation.requestBackgroundLocationPermission()Android 12+ 权限类型
Anchor link to从 Android 12 开始,当被要求授予前台位置访问权限时,用户可以选择应用获取用户位置的频率和精度。
有两种类型的权限:
- 大致位置,等同于 ACCESS_COARSE_LOCATION
- 精确位置,等同于 ACCESS_FINE_LOCATION 和 ACCESS_COARSE_LOCATION
与我们分享您的反馈
Anchor link to您的反馈有助于我们创造更好的体验,因此如果您在 SDK 集成过程中遇到任何问题,我们很乐意听取您的意见。如果您遇到任何困难,请随时通过此表单与我们分享您的想法。