2

I am currently developing an android app using the android.location.API.

Usually the GPS location request succeeded in some phones(Galaxy Note2) but failed(getLastKnownLocation() returns null) in others(Galaxy S3 and S4).

It was weird that the GPS worked for S3 & S4 if I first activated the Samsung built-in function "GPS Satellite View" which will scan the satellites available.

My guess is that both S3 & S4 are using Mediatek chips which provide poor GPS signals. So does anyone know how to perform the same function like scanning satellites in android?

Note that my app is used in China so please don't suggest any Google related services.

Thank you.

public class MyLocationManager implements android.location.LocationListener{

    private static MyLocationManager instance = null;
    private Location currentLocation;
    private String provider;
    private LocationManager locationManager;
    private Context context;

    public static MyLocationManager getInstance(){
        if (instance == null){
            instance = new MyLocationManager();
        }
        return instance;
    }

    public boolean hasLocation(){
        if(currentLocation == null){
            return false;
        } else{
            return true;
        }
    }

    public float distanceInMetersFromLocation(double latitude, double longitude){
        if (currentLocation == null){
            return -1;
        }
        float[] results = new float[3];
        Location.distanceBetween(currentLocation.getLatitude(), currentLocation.getLongitude(), latitude, longitude, results);

        return results[0];
    }


    public void startListenLocation(Context context) {

        this.context = context;

        locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        boolean wifiEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (gpsEnabled){
            provider = LocationManager.GPS_PROVIDER;
            Log.e("MyLocationManager", "GPS");
        } else if (wifiEnabled){
            provider = LocationManager.NETWORK_PROVIDER;
            Log.e("MyLocationManager", "NetWork");
        } else {
            Log.e("MyLocationManager", "Please Enable Location Service");
            return;
        }

        requestLocationUpdates();

        if(provider.equals(LocationManager.GPS_PROVIDER)){
            currentLocation = locationManager.getLastKnownLocation(provider);
        } else{
            currentLocation = locationManager.getLastKnownLocation(provider);
        }

        if(currentLocation == null && provider.equals(LocationManager.GPS_PROVIDER)){
            Log.e("MyLocationManager", "GPS Failed");
            if(wifiEnabled){
                Log.e("MyLocationManager", "Use Wifi");
                provider = LocationManager.NETWORK_PROVIDER;
                requestLocationUpdates();
                currentLocation = locationManager.getLastKnownLocation(provider);
            }else{
                return;
            }
        }

    }

    public void stopListenLocation(){
        //stop updating after retrieving the location
        locationManager.removeUpdates(this);
    }

    public void requestLocationUpdates(){
        if(provider != null && !provider.isEmpty()){
            locationManager.requestLocationUpdates(provider, 60000, 10, this);
        } else{
            Log.e("MyLocationManager", "Request Location Updates Failed: Provider is null.");
        }
    }


    @Override
    public void onLocationChanged(Location location) {
        currentLocation = location;
        Log.d("SONIC", "location changed to "+location);

    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }

}
GilbertLee
  • 654
  • 1
  • 6
  • 21
  • First you must ensure that GPS is enabled. Second thing is getLastKnownLocation() not guaranty that it has last location. So you need to check if GPS is not able to provide the location than you can use NETWORK provider to get location. – Biraj Zalavadia Oct 28 '14 at 04:56
  • @BirajZalavadia Hi, I am sure the GPS had been enabled. My network locating works for all devices but I just want to make sure the GPS works. I am just wondering why getLastKnownLocation() works for some devices under same conditions. I tried requestLocationUpdates() for GPS but it didn't work neither. Do you have a solution for this? Thank you – GilbertLee Oct 28 '14 at 05:14

1 Answers1

0

Actually this is a device issue.

Some devices doesn't support getLastKnownLocation (e.g. Motorola XT720). You have to implement LocationListener and save location updates in onLocationChanged by yourself to database or sharedpreferences. And when getLastKnownLocation is null use that saved location.

You can go with Best Provider approach to get the fast location as much as possible.

Read this blog hope it helps you.

Biraj Zalavadia
  • 28,348
  • 10
  • 61
  • 77
  • Hi, I am not sure about this. Can I upload my codes and you can have a check for me? – GilbertLee Oct 28 '14 at 05:28
  • Your code is ok. But Issue wit GPS is it will not accurate in building and it may take up to 1 minutes to initialize. It also depends on GPS hardware quality. If you will try under clear sky it will work. – Biraj Zalavadia Oct 28 '14 at 05:40
  • It's back to my question: hardware issues. As I noticed, the Samsung built-in function can initialize in about 10 secs, and then I can location with GPS in my app. So there might be a way to initialize the GPS quickly. – GilbertLee Oct 28 '14 at 05:47
  • As it is all about hardware we can't do anything because until hardware respond there is no way to get it. – Biraj Zalavadia Oct 28 '14 at 05:49
  • You can go with Best Provider approach to get the fast location as much as possible. – Biraj Zalavadia Oct 28 '14 at 05:53
  • aye, but it is said not all devices support getBestProvider()? Thank you for you time but I am sorry it is not the answer for this question. – GilbertLee Oct 28 '14 at 05:55