I am trying to get the users current location using the LocationManager
. I have done a lot of research and can\'t seem to find anyone with the same problem. T
As Phan Van Linh
answered , it's probably because you are inside a building.
There are explanation here on how to mock GPS for the emulator.
Also, there is this app that mocks GPS data.
This happened to me when the location was turned off. Not the location permission for the app, but the location for the android device itself. Requesting that the user activate the GPS fixed it for me:
protected void onCreate(Bundle savedInstanceState) {
//...
final LocationManager manager = (LocationManager) getSystemService( Context.LOCATION_SERVICE );
if ( !manager.isProviderEnabled( LocationManager.GPS_PROVIDER ) ) {
buildAlertMessageNoGps();
}
}
private void buildAlertMessageNoGps() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Your GPS seems to be disabled, do you want to enable it?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(@SuppressWarnings("unused") final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, @SuppressWarnings("unused") final int id) {
dialog.cancel();
}
});
final AlertDialog alert = builder.create();
alert.show();
}
Solution from here.
You may also need to turn the GPS off and then on again to trigger a dialog to improve location accuracy:
It looks like you setup should work, since it doesn't, I would make your example as simple as possible in order to troubleshoot. I would make your request look like
requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
This way you get ALL the updates possible. And comment out the part about getting the last known location. It's not needed yet.
Then in onLocationChanged()
, just have
Log.v(TAG, "IN ON LOCATION CHANGE, lat=" + latitude + ", lon=" + longitude);
Comment out the rest so that you keep your listener active. This should give you a stream of updates on a real device. On the emulator, you'll need to use DDMS, and you'll get one GPS update each time you press send.
I faced same issue. I was getting getLastKnownLocation as null. Issue is resolved when I restarted both the device and Location service in device.
If you are trying with NETWORK_PROVIDER, it is really difficult to test:
I guess the reason is that it just the location of network provider only not your device itself, and the network provider never move, so the onLocationChanged() never call. It might happen only when switching between mobile data and wifi and vice versa. Please correct me if I am wrong.
So I suggest to use GPS_PROVIDER to experiment before considering performance and battery usage. It works nicely on both emulators and real devices. For the best practice: https://developer.android.com/guide/topics/location/strategies.html
It took me several hours to test with NETWORK_PROVIDER without any results, so hopefully my suggestion helpful.
I have 2 devices: Samsung Galaxy S4 (Android 5.0.1) and Vertex Win (Android 8.1).
On Samsung everything works right and fast, GPS coordinates are found, and onLocationChanged
is called.
On Vertex in prioity PRIORITY_BALANCED_POWER_ACCURACY
it probably interacts with network provider first, so it finds coordinates via Wi-Fi. But it can't detect right, whether location service is turned on (after some time the device thinks it is turned on, while it is off, and calls addOnSuccessListener
method of LocationServices.getSettingsClient
. So I switched to PRIORITY_HIGH_ACCURACY
and got another surprise. Network provider turned off, but onLocationChanged
wasn't called, so I couldn't get coordinates. I downloaded GPS tester
(as @Li3ro advised) from Play Market and saw that sattelites are visible, but without fixed signal. I couldn't receive a signal inside a building.
Even standalone maps couldn't receive coordinates, but outdoor could. So, after I walked away, I could find my coordindinates in an application.
See also https://stackoverflow.com/a/27828929/2914140, where it is said about priorities and error recognition (disabling GPS after some time if no coordinsates found).