LocationListener runs on emulator, not on phone

I had a problem calling the LocationListener function to call callback onLocationChanged () on my phone. When I run my code in the emulator, it works fine, the callback is called every time I fix geo.

When I run the application on my phone, nothing happens. The callback is never called. I have a location enabled by both GPS and Wireless in my settings. The application has all permissions, permissions for access permissions.

Also, when I call getLastKnownLocation () on the LocationManager, my application crashes. (However, only on my stupid phone). Even if I try to catch an exception that will lead to its failure, it is still just a failure, so I canโ€™t even get information about what causes it to fail. This is very frustrating.

LocationManager.getBestProvider () returns GPS, and when I open Google maps, it finds my location in no time. What the hell is going on here? Is there any way to understand why it is crashing on my phone?

private void setupLocListener(){ Criteria c = new Criteria(); c.setAccuracy(Criteria.ACCURACY_FINE); c.setAltitudeRequired(false); c.setBearingRequired(false); c.setSpeedRequired(false); c.setCostAllowed(false); lm.requestLocationUpdates(lm.getBestProvider(c,true), 0, 0, new LocationListener() { @Override public void onLocationChanged(Location arg0) { map.setLocation(arg0); } public void onProviderDisabled(String arg0) { } public void onProviderEnabled(String arg0) { } public void onStatusChanged(String arg0, int arg1, Bundle arg2) { } }); } 
+4
source share
2 answers

onLocationChanged () does not start until you actually start getting GPS coordinates.

By this, I mean that the chip should warm up for about a minute or so from my experience before you begin to receive data from it.

Usually I launch some other application and wait for it to prove that the GPS chip has warmed up before I start testing any of my GPS applications.

I know that you mentioned that it works correctly on Google Maps, but did you try to clear the memory and restart the application right away?

Also getLastKnownLocation () is always null until you start getting the coordinates.

+1
source

The Location frame moves the coordinates of your callback when they become available. Depending on the weather, etc. You cannot get a โ€œfixโ€ initially. You should see the โ€œGPSโ€ indicator in the status bar when your listener is successfully registered. getLastKnownPosition() works just fine (it can return null ); and Google Maps uses this while it waits for an initial fix from the location provider. You can also see what other providers are available, for example. cell data and try to get data from those (that is, LKP), either instead, or until your "preferred" provider starts pushing the data. Also, do not assume that any particular service exists, for example. LocationManager ( Context.getSystemService() may return null ) or any suitable provider exists, ( getBestProvider() may return null ). Your code will not work on the correct device with the correct settings. If the documentation says null , you should check it, or users will delete it, because it is FC everywhere.

0
source

Source: https://habr.com/ru/post/1338354/


All Articles