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.
volatile
. and for good - to attach full synchronization. - KoVadim