3

onLocationChanged event is not fired with requestSingleUpdate.

It seems that it depends on the looper attached ex:

locManager.requestSingleUpdate("network",locListener,Looper.getMainLooper());
locManager.requestSingleUpdate("network",locListener,null);
locManager.requestSingleUpdate("network",locListener,Looper.myLooper());

are not giving the same results with the emulator and the device !

Questions

  • If requestSingleUpdate is called many times (with different providers), only the last query will be taken ?
  • requestSingleUpdate must be called on ui thread ? If not, there's a trick to call it outside ?

Complete Class :

public class LocationNew {

    private Context context;


    public LocationNew(Context context) {
        this.context = context;
    }

    public void requestNew(Looper paramLooper){

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int permissionLocation = ContextCompat.checkSelfPermission(DooodApp.getContext(), Manifest.permission.ACCESS_FINE_LOCATION);
            if (permissionLocation != PackageManager.PERMISSION_GRANTED) {
                return;
            }
        }

        LocationManager locManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        List<String> listProv = locManager.getProviders(true);
        for (String provTmp: listProv) {
            locManager.requestSingleUpdate(provTmp,locListener,Looper.getMainLooper());
        }
    }

    private LocationListener locListener = new LocationListener() {

        @Override
        public void onLocationChanged(Location location) {
            Toast.makeText(context, "New location from : " + location.getProvider(), Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {
            Toast.makeText(context, "onStatusChanged" , Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onProviderEnabled(String provider) {
            Toast.makeText(context, "onProviderEnabled" , Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onProviderDisabled(String provider) {
            Toast.makeText(context, "onProviderDisabled" , Toast.LENGTH_SHORT).show();
        }
    };
}

Thank's to all.

Mo12
  • 31
  • 1
  • 1
  • 6

0 Answers0