I use the following code to get the user's location:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
...
m_api = new GoogleApiClient.Builder(context)
.addConnectionCallbacks(this)
.addApi(LocationServices.API)
.build();
...
}
@Override
protected void onStart()
{
super.onStart();
m_api.connect();
}
@Override
protected void onStop()
{
m_api.disconnect();
super.onStop();
}
@Override
public void onConnected(Bundle bundle)
{
Address address;
Location cur = LocationServices.FusedLocationApi.getLastLocation(m_api);
Geocoder geocoder;
List<Address> addressList;
geocoder = new Geocoder(this, Locale.getDefault());
addressList = geocoder.getFromLocation(cur.getLatitude(),cur.getLongitude(), 1);
if (addressList != null && !addressList.isEmpty()) {
address = addressList.get(0);
...
}
}
Of five devices I tested, three worked and two did not. In both, which did not work, the "getLastLocation" method returned null and aborted execution.
I put in application dependences 'com.google.android.gms: play-services-location: 9.4.0'. I am using the minSdkVersion 16 compatibility API.
Of the two devices that aborted execution one had android 4.4 and another 5.1.
Do you have any coding errors?
Is the problem with the API installed on the device? How to test?