Hello guys,
I am currently working on my second android app which contains a google maps activity. In this activity I would like to show the user their current location. I kind of managed to do it and it works on both of my devices (Huawei P smart & Galaxy S5). I also sent my friend an APK of the app and on his device (Huawei P9 Lite) the current location returns null after calling task.getResult(). On his device the method will always jump to the else statement (meaning the currentLocation == null). I used the following method.
I have tried/done the following things:
I am currently working on my second android app which contains a google maps activity. In this activity I would like to show the user their current location. I kind of managed to do it and it works on both of my devices (Huawei P smart & Galaxy S5). I also sent my friend an APK of the app and on his device (Huawei P9 Lite) the current location returns null after calling task.getResult(). On his device the method will always jump to the else statement (meaning the currentLocation == null). I used the following method.
Java:
private void getDeviceLocation() {
Log.d(TAG, "getDeviceLocation: getting the devices current location");
try {
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
if (mLocationPermissionsGranted) {
final Task location = mFusedLocationProviderClient.getLastLocation();
location.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
Log.d(TAG, "onComplete: found location!");
Location currentLocation = (Location) task.getResult();
if (currentLocation != null) {
moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()),
DEFAULT_ZOOM);
} else {
Log.d(TAG, "onComplete: current location is null");
Toast.makeText(MapsActivity.this, "unable to get current location", Toast.LENGTH_SHORT).show();
mFusedLocationProviderClient.getLastLocation();
moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()),
DEFAULT_ZOOM);
}
}
});
}
} catch (SecurityException e) {
Log.e(TAG, "getDeviceLocation: SecurityException: " + e.getMessage());
}
}
I have tried/done the following things:
- Adding the following permissions/features to my manifest
Java:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_GPS" />
- Changing the minSDK to 17 (was 23 before).
- Several codechanges which probably aren't noteworthy
- Adding the nullcheck and try & catch