位置广播接收器

发布于 2024-10-21 02:37:23 字数 116 浏览 1 评论 0原文

我知道 BroadcastReceiver 会监视文本、电话事件等...但是您可以将 LocationServices 作为基于位置的服务和事件运行吗?

例如,您位于某个 GPS 点附近,手机会通知您。

I know BroadcastReceiver watches for text, phone events, etc... but can you run LocationServices as a service and event based on Location?

For example, you are near a certain GPS point and the phone notifies you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

话少情深 2024-10-28 02:37:23

我想你正在寻找的是这样的东西。 LocationManager.requestLocationUpdates() 的一个版本采用 PendingIntent 作为参数,并且此 Intent 可用于触发 BroadcastReceiver。此代码将注册一个自定义广播以随位置更新一起触发。

Intent intent = new Intent("UNIQUE_BROADCAST_ACTION_STRING_HERE");
LocationManager manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
long minTime;     //This should probably be fairly long
float minDistance; //This should probably be fairly big
String provider;   //GPS_PROVIDER or NETWORK_PROVIDER

PendingIntent launchIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
manager.requestLocationUpdates(provider, minTime, minDistance, launchIntent);

然后,您只需使用相同的 Intent 操作注册您的 BroadcastReceiver ,它将接收位置更新以进行处理。当然,请保留 PendingIntent,因为您需要在某些时候调用 manager.removeUpdates(launchIntent)

最后说明:

因为听起来您正在尝试在应用程序不在前台时实现定期位置更新,所以请记住这一点。除非您希望您的应用程序被打上电池杀手的标签,否则请在实现此功能时务必非常小心。

您将需要使用较大的 minTime 或 minDistance 参数来大大降低位置更新的频率,以允许定位服务休息。此功能还应该附加到用户控制的首选项,以便他们可以启用/禁用您的应用程序在后台跟踪位置。

希望有帮助!

I think what you are looking for is something like this. There is a version of LocationManager.requestLocationUpdates() that takes a PendingIntent as a parameter, and this Intent could be used to fire a BroadcastReceiver. This code will register a custom broadcast to fire with the location updates.

Intent intent = new Intent("UNIQUE_BROADCAST_ACTION_STRING_HERE");
LocationManager manager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
long minTime;     //This should probably be fairly long
float minDistance; //This should probably be fairly big
String provider;   //GPS_PROVIDER or NETWORK_PROVIDER

PendingIntent launchIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
manager.requestLocationUpdates(provider, minTime, minDistance, launchIntent);

Then you just have to register your BroadcastReceiver with the same Intent action and it will receive the location updates for processing. And, of course, keep that PendingIntent around because you'll need to call manager.removeUpdates(launchIntent) at some point!

Final Note:

Because it sounds like you are trying to implement regular location updates while your app isn't in the foreground, keep this in mind. Unless you want your app branded as a battery killer, be extremely careful with how you implement this functionality.

You will want to greatly reduce the frequency up location updates with either a large minTime or minDistance parameter to allow the location service to rest. This feature should also be attached to a user controlled preference so they can enable/disable your app from tracking location in the background.

Hope that Helps!

平定天下 2024-10-28 02:37:23

这个问题已经有两年多了,但现在有一种非常非常简单且电池高效的方法来做到这一点。这个概念称为地理围栏。您可以在下面更详细地了解它:

http://developer.android.com/training/位置/地理围栏.html

This question is more then 2 years old but there is now very very easy and battery efficient way to do this. The concept is called Geofences. You can learn it in more detail below:

http://developer.android.com/training/location/geofencing.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文