0

I'm using this What is the simplest and most robust way to get the user's current location on Android? to get my current location and list nearby restaurants when i click on swith button.It works before i remove the app and install it again. this is my implementation of gotLocation method

switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    MyLocationManager.LocationResult locationResult= new MyLocationManager.LocationResult() {
                        @Override
                        public void gotLocation(final Location location) {
                            //Got the location!


                                    currentLocation = location;
                                    browseNearbyBusinessesList(currentLocation);

                        }
                        };
                    MyLocationManager myLocationManager = new MyLocationManager();
                    myLocationManager.getLocation(getBaseContext(), locationResult);
                }else {
                    searchDefaultList();
                }
            }
        });

I got not null LocationResult Object but the gotLocation method was not executed . Can any one tell me where is exactly the problem please?

this is the code of MyLocationManager Class

public class MyLocationManager {
    public LocationResult locationResult;
    public Context context;
    Timer timer1;
    LocationManager lm;
    boolean gps_enabled = false;
    boolean network_enabled = false;
    LocationListener locationListenerGps = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerNetwork);
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };
    LocationListener locationListenerNetwork = new LocationListener() {
        public void onLocationChanged(Location location) {
            timer1.cancel();
            locationResult.gotLocation(location);
            lm.removeUpdates(this);
            lm.removeUpdates(locationListenerGps);
        }

        public void onProviderDisabled(String provider) {
        }

        public void onProviderEnabled(String provider) {
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
        }
    };
    private boolean isPermessed = false;

    public MyLocationManager() {

    }

    public MyLocationManager(boolean isPermessed) {
        this.isPermessed = isPermessed;
    }

    public boolean getLocation(Context context, LocationResult result) {
        //I use LocationResult callback class to pass location value from MyLocation to user code.
        this.context = context;
        locationResult = result;
        if (lm == null)
            lm = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        //exceptions will be thrown if provider is not permitted.
        try {
            gps_enabled = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        } catch (Exception ex) {
        }
        try {
            network_enabled = lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        } catch (Exception ex) {
        }

        //don't start listeners if no provider is enabled
        if (!gps_enabled && !network_enabled)
            return false;

        if (gps_enabled) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                isPermessed = false;
                return false;

            } else {
                isPermessed = true;
            }
            if (isPermessed)
                lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListenerGps);
        }
        if (network_enabled) {
            if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

                isPermessed = false;
                return false;
            } else {
                isPermessed = true;
            }
            if (isPermessed)
                lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListenerNetwork);
        }

        timer1 = new Timer();
        timer1.schedule(new GetLastLocation(), 4000);
        return true;
    }

    public static abstract class LocationResult {
        public abstract void gotLocation(final Location location);
    }

    class GetLastLocation extends TimerTask {
        @Override
        public void run() {
            lm.removeUpdates(locationListenerGps);
            lm.removeUpdates(locationListenerNetwork);

            Location net_loc = null, gps_loc = null;
            if (gps_enabled) {

                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    isPermessed = false;
                    return;
                } else {
                    isPermessed = true;
                }
                if (isPermessed)
                    gps_loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            }
            if (network_enabled) {
                if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                    isPermessed = false;
                    return;
                } else {
                    isPermessed = true;
                }
                if (isPermessed)
                    net_loc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
            }
            System.out.println(isPermessed);
            //if there are both values use the latest one
            if (gps_loc != null && net_loc != null) {
                if (gps_loc.getTime() > net_loc.getTime())
                    locationResult.gotLocation(gps_loc);
                else
                    locationResult.gotLocation(net_loc);
                return;
            }

            if (gps_loc != null) {
                locationResult.gotLocation(gps_loc);
                return;
            }
            if (net_loc != null) {
                locationResult.gotLocation(net_loc);
                return;
            }
            locationResult.gotLocation(null);
        }
    }

}

1 Answers1

0

The problem was isPermissed returns always false. I created a method getLocationPermissions() to grant permissions.

        private final static String FINE_LOCATION= Manifest.permission.ACCESS_FINE_LOCATION;
        private final static String COARSE_LOCATION= Manifest.permission.ACCESS_COARSE_LOCATION;
        private final static int LOCATION_PERMISSION_REQUEST_CODE=1234;
                   --------------------------------------

        if (gps_enabled) {
                    getLocationPermission();
                    if (((ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) && (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED))) {

                        isPermessed = true;
                        Log.i("isPermessed = ", isPermessed+"");

                    } else {
                        isPermessed = false;
                        Log.i("isPermessed = ", isPermessed+"");
                        return false;


                    }

                     --------------------------------------
 private void getLocationPermission() {
        String[] permissions = {android.Manifest.permission.ACCESS_FINE_LOCATION,
                android.Manifest.permission.ACCESS_COARSE_LOCATION};

        if (ContextCompat.checkSelfPermission(context,
                FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            if (ContextCompat.checkSelfPermission(context,
                    COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            } else {
                ActivityCompat.requestPermissions((Activity) context,
                        permissions,
                        LOCATION_PERMISSION_REQUEST_CODE);
            }
        } else {
            ActivityCompat.requestPermissions((Activity) context,
                    permissions,
                    LOCATION_PERMISSION_REQUEST_CODE);
        }
    }