首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取Geofence.GEOFENCE_TRANSITION_EXIT上的当前位置

获取Geofence.GEOFENCE_TRANSITION_EXIT上的当前位置
EN

Stack Overflow用户
提问于 2017-03-02 07:41:21
回答 2查看 885关注 0票数 1

我必须在出口处更新geoFence的信息。为此,我需要目前的位置在这一点。

,当我的应用程序关闭时,,我没有onLocationChanged的侦听器。为了更新geoFence,我需要我的位置。他们有没有办法在Geofence.GEOFENCE_TRANSITION_EXIT找到当前的位置?

代码语言:javascript
复制
public void broadcastUpdateGeoFences() {
        MainActivity.isGeoFenceAdded = false;//It tells to update geoFence
        Intent intent = new Intent(Constants.RECEIVER_GEOFENCE);
        intent.putExtra("done", 1);
        sendBroadcast(intent);
    }

广播接收机在MainActivity中的应用

代码语言:javascript
复制
   private BroadcastReceiver receiver = new BroadcastReceiver() {
      @Override
      public void onReceive(Context context, Intent intent) {
            Bundle bundle = intent.getExtras();
            if (bundle != null) {
                int resultCode = bundle.getInt("done");
                if (resultCode == 1) {

                    if(!MainActivity.isGeoFenceAdded){
                        updateGeoFencesOnCurrentExit();//I need current location here
                    }
                }
            }
        }
    };

每件事都很正常。但是,当应用程序关闭时,无法找到在出口通过currentLocation的方法。

代码语言:javascript
复制
private void updateGeoFencesOnCurrentExit(Location currentLocation){
            locationHandler.updateGeoFences(currentLocation);
}
EN

回答 2

Stack Overflow用户

发布于 2017-03-08 05:43:42

您可以在广播中实现LocationListener接口,并使用以下覆盖方法onLocationChanged

代码语言:javascript
复制
    @Override
public void onLocationChanged(Location location) {
    location.getLongitude();
    location.getLatitude();
}
票数 0
EN

Stack Overflow用户

发布于 2017-03-08 18:39:30

我不建议在IntentService或BroadcastReceiver中使用IntentService或BroadcastReceiver,因为一旦函数(onHandleIntent或onReceive)退出,就可以销毁它们。一旦您得到geofence事件,我将使用带有PendingIntent的LocationManager请求一个更新。我有示例应用程序演示如何使用github上的PendingIntent (https://github.com/pablobaxter/AndroidLocationExamples)请求位置更新。

编辑:

IntentService并没有错。您只是在后台线程中运行onHandleIntent逻辑,而在主线程上运行onReceive

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42549449

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档