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) {
}
}