Good evening! Shoveled all the LocacionListener documentation, as well as reviewed and tried different implementations, but each time with tests on different devices, there are those on which everything is sad.
Tell me what the problem can be? I no longer understand. For example, the problems were on the Samsung Galaxy S1, Galaxy fit, htc one v, HTC Incredible S, Pipo Smart S1, etc. I don’t understand what's wrong. I myself test everything on the Galaxy S3 . At the moment I use the implementation like this (and there were others).

 public class LocationListener implements android.location.LocationListener { private LocationManager mLocationManager = null; private String mCurrentProvider; private boolean mLocationEnabled = false; private Location mLocation = null; private LocationListener locationListener; //рСализация интСрфСйса Runnable которая Π²Ρ‹Π·ΠΎΠ²Π΅Ρ‚ Π½ΡƒΠΆΠ½Ρ‹Π΅ ΠΌΠ΅Ρ‚ΠΎΠ΄Ρ‹ Π² UI ΠΏΠΎΡ‚ΠΎΠΊΠ΅ private LocationRunnable runnable = null; public LocationListener(Context context, LocationRunnable runnable) { this.runnable = runnable; mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE); } public boolean enableMyLocation() { mLocationEnabled = true; List<String> mProviders = mLocationManager.getProviders(true); if (mProviders.size() > 0) { locationListener = this; mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 1, locationListener); if(mProviders.contains( LocationManager.GPS_PROVIDER) && mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) mLocationManager.requestLocationUpdates( LocationManager.GPS_PROVIDER, 1000, 1, locationListener); } //запускаСм Ρ„ΠΎΠ½ΠΎΠ²Ρ‹ΠΉ процСсс, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ отсчитаСт, Ρ‡Ρ‚ΠΎ ΠΏΡ€ΠΎΡˆΠ»ΠΎ 30 сСкунд new TimeOut().execute(); return false; } private class TimeOut extends AsyncTask<Void,Void,Void> { @Override protected Void doInBackground(Void... params) { //Ρ‚ΡƒΡ‚ отсчитываСм 30 сСкунд ΠΈ Ссли ΠΌΠ΅Ρ‚ΠΎΠ΄ onLocationChanged Π΅Ρ‰Π΅ Π½Π΅ вызывался, Ρ‚ΠΎ запустим runnable long sec = System.currentTimeMillis(); //Ρ‚ΡƒΡ‚ стал ΠΏΡ€ΠΎΠ²Π΅Ρ€ΡΡ‚ΡŒ locationChanged которая с ΠΌΠΎΠΈΡ„ΠΈΠΊΡ‚ΠΎΡ€ΠΎΠΌ volatile while (!locationChanged == null && System.currentTimeMillis() - sec < 30 * 1000) { } return null; } @Override protected void onPostExecute(Void p) { if (!locationChanged) { runnable.setLocation(mLocation); runnable.run(); } //откаТСмся ΠΎΡ‚ ΠΎΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΠΉ mLocationManager.removeUpdates(locationListener); } } // Ρ‚ΡƒΡ‚ сдСлал ΠΊΠ°ΠΊ сказали, ΠΈ отказался ΠΎΡ‚ использования Π² Ρ„ΠΎΠ½ΠΎΠ²ΠΎΠΌ ΠΏΠΎΡ‚ΠΎΠΊΠ΅ locztion private volatile boolean locationChanged = false; public void onLocationChanged(Location location) { //Ссли ΠΌΡ‹ ΠΎΠΏΡ€Π΅Π΄Π΅Π»ΠΈΠ»ΠΈ мСстополоТСниС Ρ‚ΠΎ Π²Ρ‹Π·ΠΎΠ²Π΅ΠΌ измСнСния Π² UI mLocation = location; locationChanged = true; runnable.setLocation(location); runnable.run(); //откаТСмся ΠΎΡ‚ ΠΎΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΠΉ mLocationManager.removeUpdates(locationListener); } } 

UPD: declined GPS detection, stopped accessing mLocation in the background thread, instead checking the boolean variable with the volatile modifier. The effect of the actions taken is zero.

  • Maybe gps just does not have time to connect? Especially if you are testing in the room. plus everything, this while code (mLocation == null && System.currentTimeMillis () - sec <30 * 1000) {} is bad. It may not work as you think. You need to declare at least mLocation with the volatile . and for good - to attach full synchronization. - KoVadim
  • The fact is that mLocation is read in one stream, and written in another. The compiler can check the value once and stop checking. - KoVadim
  • @KoVadim, will that be enough? synchronized (mLocation) {long sec = System.currentTimeMillis (); // time out 30 sec while (mLocation == null && System.currentTimeMillis () - sec <10 * 1000) {}} - andreich
  • in onLocationChanged is also needed. But I still think that you just have gps connected for a very long time, and this is the main reason. - KoVadim
  • @KoVadim, and the problem is not the point. I wait 30 seconds, and then if the onLocationChanged method was not called, in which runnable.run () is executed, execute it, and if it was called, then everything is ok. Problems due to synchronization and access to variables from other threads are unlikely here. After all, onLocationChanged is not called in these 30 seconds, even if you do not use gps - andreich

0