-3

I am making a app in which i have to show a splash screen for 1/2 sec.While the splash is loading i have to get the user location.location accuracy is not concern.So for this i have written a code but always i get null from location.

Code

private Context context;

    private LocationManager locationManager;

    Location location;

    double latitude;
    double longitude;

    public GetLocation(Context context) {
        this.context = context;
        getLocation();
    }


    private Location getLocation() {


        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        location = locationManager.getLastKnownLocation(getProviderName());
        if (location == null) {

            locationManager.requestLocationUpdates(getProviderName(), 0, 0, this);
        }

        return location;


    }


    String getProviderName() {
         locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);

        Criteria criteria = new Criteria();
        criteria.setPowerRequirement(Criteria.POWER_LOW); // Chose your desired power consumption level.
        criteria.setAccuracy(Criteria.ACCURACY_FINE); // Choose your accuracy requirement.
        criteria.setSpeedRequired(true); // Chose if speed for first location fix is required.
        criteria.setAltitudeRequired(false); // Choose if you use altitude.
        criteria.setBearingRequired(false); // Choose if you use bearing.
        criteria.setCostAllowed(false); // Choose if this provider can waste money :-)

        // Provide your criteria and flag enabledOnly that tells
        // LocationManager only to return active providers.
        return locationManager.getBestProvider(criteria, true);
    }

    @Override
    public void onLocationChanged(Location location) {

        Toast.makeText(context,""+location,Toast.LENGTH_LONG).show();

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

        Toast.makeText(context,provider,Toast.LENGTH_LONG).show();

    }

    @Override
    public void onProviderEnabled(String provider) {

        Toast.makeText(context,provider,Toast.LENGTH_LONG).show();

    }

    @Override
    public void onProviderDisabled(String provider) {

        Toast.makeText(context,provider,Toast.LENGTH_LONG).show();

    }

    public void stopUsingGPS() {
        if (locationManager != null) {
            locationManager.removeUpdates(this);
        }
    }

    /**
     * Function to get latitude
     */
    public double getLatitude() {
        if (location != null) {
            latitude = location.getLatitude();
        }

        // return latitude
        return latitude;
    }

    /**
     * Function to get longitude
     */
    public double getLongitude() {
        if (location != null) {
            longitude = location.getLongitude();
        }

        // return longitude
        return longitude;
    }

Permissions

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

Usage

private void getLocation() {

        getLocation = new GetLocation(contextActivity);

        latitude = getLocation.getLatitude();
        longitude = getLocation.getLongitude();

        if (latitude != 0 && longitude != 0) {

            getLocation.stopUsingGPS();
        }
    }

Please do help me out. Thanks.

Anuj Mody
  • 21
  • 1
  • 6

2 Answers2

1

getLastKnownLocation returns null if a location hasn't been yet acquired. If it is case for your device, the following snippet

if (location == null) {
    locationManager.requestLocationUpdates(getProviderName(), 0, 0, this);
}

prevents you from registering the LocationListener, callback for your location manager.

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

I guess you are not updating location in onLocationChanged. Are you getting location non null object in your toast text?

My answer would be updating location member variable with the location received with onLocationChange event.

Kailas
  • 807
  • 1
  • 6
  • 20
  • No i also not getting the toast – Anuj Mody May 26 '15 at 08:31
  • Hmm... Try with fused location API to get location. Fused location API are fast compared to location manager and provides more accurate locations as well. Find more info here : https://developer.android.com/training/location/receive-location-updates.html – Kailas May 26 '15 at 16:20