无法获取位置更新

时间:2015-02-08 08:08:27

标签: android locationlistener

我在基于位置的Android应用程序中进行了更改,以符合最新的API。 所以现在我的活动实现了:

public class MapActivity extends FragmentActivity implements   LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
.....
}

onCreate中,我配置LocationRequestGoogleApiClient

mLocationRequest = LocationRequest.create()
            .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY)
            .setInterval(2 * 1000);
mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();

这是我的onConnected方法:

@Override
public void onConnected(Bundle bundle) {
     Log.i(TAG, "Location services connected.");
     Location location =  LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);        
     if (location == null) {               LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,  mLocationRequest, this);
     } else {
        .....
        .....
    }
}

我面临的问题是即使用户处于移动状态,也不会调用onLocationChanged

我在这里缺少什么?

在调试过程中,我发现onConnected()

Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);  

该位置不为空,因此不会执行if内的代码。这可能是onLocationChanged没有被调用的原因吗?

0 个答案:

没有答案