1

Im looking for a very sample example show longitude and latitude trackers with asynctask.

I find this code https://stackoverflow.com/a/6788457 but he doesn't work :/ i catch this strangely exception "provider doesn't exisit: null" in :

public boolean startService() {
    try {
        // this.locatorService= new
        // Intent(FastMainActivity.this,LocatorService.class);
        // startService(this.locatorService);

        FetchCordinates fetchCordinates = new FetchCordinates();
        fetchCordinates.execute();
        return true;
    } catch (Exception error) {
        Log.i("exception", error.getMessage());
        return false;
    }

}

After search, I see this post (https://stackoverflow.com/a/13851305/2137454) who explain I must may be use this line :

List<String> providers = locationManager.getAllProviders();

But I don't understand where and how use this tips in my gpstracker class... Someone can help me ?

Here the complete gpstracker class :

public class GPSTracker extends Service implements LocationListener {

    private final Context mContext;
    boolean isGPSEnabled = false;
    boolean isNetworkEnabled = false;
    boolean canGetLocation = false;

    Location location = null; 
    double latitude = 0; 
    double longitude = 0; 
    double altitude = 0;

    private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 10;
    private static final long MIN_TIME_BW_UPDATES =  1000* 60 * 1;
    protected LocationManager locationManager;

    public GPSTracker(Context context) { 
        this.mContext = context.getApplicationContext(); 
        getLocation();
    }

    public Location getLocation() {
        try {
            locationManager = (LocationManager) mContext.getSystemService(LOCATION_SERVICE);

            isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);

            isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
               Log.i("Je tombe...","...ici !");
            } else {
                this.canGetLocation = true;
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                latitude = location.getLatitude();
                                longitude = location.getLongitude();
                                altitude = location.getAltitude();
                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

    public void stopUsingGPS() {
        if (locationManager != null) {
            Log.i("STOPUSINGGPS", "EFFECTIF");
            locationManager.removeUpdates(GPSTracker.this);
        }
    }

    public double getLatitude() {
        if (location != null) {
            Log.i("LATITUDE", "EFFECTIF");
            latitude = location.getLatitude();
        }
        return latitude;
    }

    public double getLongitude() {
        if (location != null) {
            Log.i("LONGITUDE", "EFFECTIF");
            longitude = location.getLongitude();
        }
        return longitude;
    }

    public double getAltitude() {
        if (location != null) {
            Log.i("ALTITUDE", "EFFECTIF");
            altitude = location.getAltitude();
        }
        return altitude;
    }

    public boolean canGetLocation() {
        return this.canGetLocation;
    }

    @Override
    public void onLocationChanged(Location location) {
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

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

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

}

edit : my gps is turned ON

Community
  • 1
  • 1
spacecodeur
  • 2,206
  • 7
  • 35
  • 71

0 Answers0