0

I have the following class to get the user location.

public class GetUserLocation extends Activity {

Timer timer1;

LocationManager locationManager = null;
LocationResult locationResult;
boolean gpsEnabled = false;
boolean networkEnabled = false;
public static Location userLocation = null;

public String checkProviders(Context context) {
    if (locationManager == null) {
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }
    try {
        gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
    } catch (Exception ex) {
        ex.printStackTrace();
        gpsEnabled = false;
    }

    try {
        networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {
        ex.printStackTrace();
        networkEnabled = false;
    }

    if(!gpsEnabled && !networkEnabled)
        return "No Location Service Found.\nPlease turn on your location service by\nenabling GPS or Data Service or WI-FI";

    if(gpsEnabled && !networkEnabled)
        return "No Internet Service Found.\nPlease turn on your inernet service by\nenabling Data Service or WI-FI";

    return null;

}

public boolean getLocation(Context context, LocationResult result) {
    locationResult = result;

    if (locationManager == null) {
        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    }

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

    }

    try {
        networkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

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

    if (gpsEnabled) {
        locationManager.removeUpdates(locationListenerGps);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListenerGps);
    }

    if (networkEnabled) {
        locationManager.removeUpdates(locationListenerNetwork);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,locationListenerNetwork);
    }

    timer1 = new Timer();
    //timer1.schedule(new GetLastLocation(), 30000);

    return true;
}

LocationListener locationListenerGps = new LocationListener() {

    @Override
    public void onLocationChanged(Location location) {
        timer1.cancel();

        locationResult.gotLocation(location);
        locationManager.removeUpdates(this);
        locationManager.removeUpdates(locationListenerNetwork);
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

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

LocationListener locationListenerNetwork = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        timer1.cancel();
        locationResult.gotLocation(location);
        locationManager.removeUpdates(this);
        locationManager.removeUpdates(locationListenerGps);
    }

    @Override
    public void onProviderDisabled(String provider) {
    }

    @Override
    public void onProviderEnabled(String provider) {
    }

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

//  class GetLastLocation extends TimerTask {
//      @Override
//      public void run() {
//          
//          locationManager.removeUpdates(locationListenerGps);
//          locationManager.removeUpdates(locationListenerNetwork);
//          
//          Location networkLocation = null, gpsLocation = null;
//          if (gpsEnabled) {
//              //t = Calendar.getInstance().getTimeInMillis();
//              gpsLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
//          }
//          
//          if (networkEnabled) {
//              networkLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
//          }
//          
//          // if there are both values use the latest one
//          if (gpsLocation != null && networkLocation != null) {
//              
//              if (gpsLocation.getTime() > networkLocation.getTime())
//                  locationResult.gotLocation(gpsLocation);
//              else
//                  locationResult.gotLocation(networkLocation);
//              
//              return;
//          }
//          
//          if (gpsLocation != null) {
//              locationResult.gotLocation(gpsLocation);
//              return;
//          }
//          
//          if (networkLocation != null) {
//              locationResult.gotLocation(networkLocation);
//              return;
//          }
//          
//          locationResult.gotLocation(null);
//      }
//  }

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

public void removeUpdates() {
    if(timer1!=null) {
        timer1.cancel();
    }
    locationManager.removeUpdates(locationListenerGps);
    locationManager.removeUpdates(locationListenerNetwork);
    userLocation = null;
    locationResult.gotLocation(null);
}

public Timer getTimer() {
    return this.timer1;
}

}

This code is working good for other devices, but not in galaxy s3. But in the test S3 device the google map is getting the user location fix.

Can anyone say why is this happening? Any Hints will be appreciated.

Alex Curran
  • 8,818
  • 6
  • 49
  • 55
aman.nepid
  • 2,864
  • 8
  • 40
  • 48
  • Can you add the "other" devices it works (Android OS, model)? Maybe there is something different in 4+ devices. Does it work on a Android 4 simulator? – madlymad Mar 12 '13 at 11:31
  • @madlymad yeh it works on Samsung Galaxy SII with 4.0.4, 4.2 and galaxy ACE with 2.3. But its doint nothing and I am getting user location null on SIII with 4.1.2 – aman.nepid Mar 12 '13 at 15:05
  • 3
    I am facing the same issue. Works on all Samsung phone's except S3. – Siddharth Mar 12 '13 at 19:12
  • Same issue here. I have tried downloaded a lot of tutorial sample codes including the official sample in android dev site but location is always null. I'm testing using S3(version 4.1.2) device. Have you find any solution yet? – vida Apr 03 '13 at 10:52
  • @Siddharth and @ vida : didn't get any exact solution. but however I have used GetLastLocation TimerTask which is commented in above code snippet and forced the Time to schedule for 30secs. And it was working fine. I hope this will help you. – aman.nepid Apr 25 '13 at 03:46
  • 1
    Timer does not last long. Its better to use AlarmManager – Siddharth Apr 25 '13 at 06:12
  • 1
    I have logged a forum question with Samsung. Hopefully you we will get some help there http://developer.samsung.com/forum/board/thread/view.do?boardName=GeneralB&messageId=233508&startId=zzzzz~ – Siddharth May 06 '13 at 09:16
  • Are you still looking for this answer ? Or did you figure it out ? – Siddharth May 09 '13 at 17:23

1 Answers1

0

I solved a problem like this, installing the APP GPS Status, that reset the GPS. Solution found at: http://productforums.google.com/forum/#!topic/maps/q24Cq4sQAtc%5B1-25-false%5D
OR just, try clearing the App Data from "Maps". You'll then need to re-grant access to location services when prompted.

Hugs

Gabriel Pereira
  • 580
  • 5
  • 9