简体   繁体   English

访问服务中的Google Play服务位置API

[英]Accessing Google Play Services Location API in a Service

I want to connect to the Google Play Services Location API in a service so I can request location updates and geofencing. 我想连接到服务中的Google Play服务位置API,以便可以请求位置更新和地理围栏。 The example on the Android developer site makes the connection in an Activity using the activity lifecycle methods to connect and disconnect, it also handles failure by displaying dialogs etc. In my app I want to use a service rather than an Activity as I want to keep receiving location updates when the activity isn't visible. Android开发人员网站上的示例使用活动生命周期方法连接和断开活动,从而在“活动”中建立连接,并且还通过显示对话框等来处理失败。在我的应用中,我想使用服务而不是“活动”,因为我想保留在看不到活动时接收位置更新。 I am planning on connecting to Google Play Services when the service is started and disconnecting when I no longer require location updates or when the service is stopped. 我计划在服务启动时连接到Google Play服务,并在不再需要位置更新或服务停止时断开连接。 Is it possible to handle the connection failure scenarios from the service as it doesn't have access to an Activity to display the dialogs etc? 是否有可能处理服务中的连接失败情况,因为该服务无权访问活动以显示对话框等?

Thanks 谢谢

You can use a LocalBroadcastReceiver in the activity, and let the service send the messages when a failure scenario is reached. 您可以在活动中使用LocalBroadcastReceiver,并在出现故障情况时让服务发送消息。 The activity can show error accordingly, based on the provided intent and other parameters that you can send within it. 根据提供的意图和您可以在其中发送的其他参数,活动可以相应地显示错误。 Note that you have to validate, inside the receiver, when the activity is on background, and show the error or try when the user brings back your activity to foreground. 请注意,您必须在接收器内部验证活动处于后台的时间,并显示错误或在用户将活动恢复到前台时进行尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM