获取广播接收器中的位置

时间:2014-01-30 10:27:01

标签: android geolocation broadcastreceiver google-play-services

我正在使用GooglePlayServices API获取当前位置。

在获取当前位置的任何活动中,以下代码都可以正常运行

@Override
    protected void onStart() {
        super.onStart();
        // Connect the client.
        mLocationClient.connect();
    }
  @Override
    protected void onStop() {
        // Disconnecting the client invalidates it.
        mLocationClient.disconnect();
        super.onStop();
    }

以下代码为我提供了用户的最佳已知位置。

 mCurrentLocation = mLocationClient.getLastLocation();

但如何理想地连接到BroadcastReceiver中的位置客户端并与onReceive()断开连接是其生命周期中唯一的方法?

1 个答案:

答案 0 :(得分:1)

我认为这需要一项服务。 BroadcastReceivers不应该执行长操作,并且LocationClient看起来是异步的。如果您不想使用服务,那么AsyncTask可能会起作用,但这将是我的第二选择。