如何接收位置意图

时间:2011-11-10 15:34:59

标签: android android-intent broadcastreceiver

我已经构建了一个广播接收器,我希望在其他应用程序获得位置时接收意图。对我来说清楚这是我可以在我的应用程序未运行时收到位置更新。当我收到位置时,我启动一个intentService来更新我的数据库。

但是我不确定在清单中使用哪个intent过滤器?

是否有可以使用哪些意图过滤器的列表?

1 个答案:

答案 0 :(得分:0)

您可以做的一件事是使用LocationManager requestLocationUpdates() method with a pending intent argument。如果您使用的是API版本8或更高版本,则您要使用的位置提供程序是PASSIVE_PROVIDER,这样只有在其他活动更新时才会获得更新。

在这种情况下,您不必使用BroadcastReceiver,因为只要位置发生变化,就会调用PendingIntent。

总结一下,这就是你如何实现这个

    LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    Intent intent = new Intent(this, PassiveLocationActivity.class);

    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            intent, 0);

    locationManager.requestLocationUpdates(
            LocationManager.PASSIVE_PROVIDER, 1000, 10.0f, pendingIntent);

并确保将权限添加到您的清单

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

警告:执行此类操作可能会对电池造成极大的负担。确保为minTimeminDistance设置合理的值,并在不再需要更新时调用locationManager.removeUpdates(pendingIntent);

相关问题