应用被杀死时的Android地理围栏 [英] Android geofencing when app is killed

查看:225
本文介绍了应用被杀死时的Android地理围栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主要要求是让服务具有自己的流程并触发自己的地理围栏事件.我希望在用户进入地理围栏时从通知中心收到通知,即使该应用已被杀死.

My main requirement would be to have a service having its own process and trigger its own geofencing event. I'd like the user to be notified from the notification center when he enters a geofence, even if the app is killed.

我阅读了有关服务的信息,这对我来说似乎很清楚,Android文档非常密集,因此我设法了解了如何使用自己的流程来启动服务,以及如何使用Messenger来与应用进行通信.

I read about services and this seems pretty clear to me, the Android documentation is dense so I managed to understand how to start a service with its own process, and also how it is able to communicate with the app using Messenger.

然后有一个来自Google的代码示例,展示了如何在Google Play服务中使用地理围栏: Google示例地理围栏

Then there is this code sample from Google showing how to use geofencing with google play services: Google samples geofencing

到目前为止,我发现我们必须使用IntentService来触发地理围栏事件,并且从我阅读的文档中可以看出,IntentService在其工作完成时会自行终止.而且我还进行了一些测试,当应用程序被杀死时,似乎Intentservice被杀死了,这在文档方面并不令我感到惊讶.

What I found so far is that we have to use an IntentService to trigger geofencing events, and from the docs I've read it states that an IntentService terminates itself when its work is done. And I tested a bit also, it looks like the Intentservice gets killed when the app gets killed, which doesn't surprise me regarding the documentation.

所以我的问题是,我们如何执行具有自己流程的地理围栏服务?可以避免在此问题上使用IntentService吗?

So my question is, how could we perform a geofencing service having its own process? Is that possible to avoid using IntentService for this matter?

我想这样做的原因是,用户可以根据日常使用情况来设置地理围栏提醒".例如,假设您每天到达工作场所,当我们进入有关应用程序是否运行的地理围栏时,我们应该能够发出警报(或通知).我们不希望用户担心在后台运行应用程序.

The reason I wanna do this, is that the user can set geofence "reminders", it is based on a daily usage. For instance imagine each day you arrive at your workplace, we should be able to fire an alarm (or a notification) when we enter this geofence regarding the app is running or not. We don't want the user to worry about having the app running in background.

推荐答案

我实际上是通过使用自己的进程创建服务来实现这一点的.此服务与IPC通信绑定,然后我的活动会在需要时注册到它.

I actually realized this by creating a service with its own process. This service is bound with IPC communication, my activity then registers to it when needed.

在此服务中,我确实应用了在Google示例中找到的地理围栏示例.它使用IntentService触发地理围栏事件.我不在自己的活动中使用此逻辑,而是在自己的服务中使用它.现在,即使我的应用程序被终止,我也可以收到通知.

Within this service, I did apply the geofencing example that I found in google samples. It uses an IntentService to trigger geofencing events. Instead of using this logic in my activity, I use it in my own service. Now I can receive notifications even when my app is killed.

这篇关于应用被杀死时的Android地理围栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆