某些手机的Android GPS位置请求失败
我目前正在开发一个使用android.location.API的android应用程序。某些手机的Android GPS位置请求失败
通常情况下,GPS位置请求成功有些手机(银河注2),但失败(getLastKnownLocation()返回null)在其他国家(银河S3和S4)。
如果我第一次激活三星内置功能“GPS卫星视图”,它将扫描卫星可用,这是奇怪的是,GPS工作S3 S3 & S4。
我的猜测是,这两个S3 & S4使用联发芯片,其提供差的GPS信号。那么有没有人知道如何执行相同的功能,如在Android扫描卫星?我的应用程序是在中国使用
注意,所以请不要提出任何谷歌相关服务。
谢谢。
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) {
}
}
其实这是一个设备问题。
一些设备不支持getLastKnownLocation
(例如Motorola XT720)。您必须执行LocationListener
并自行将位置更新保存在onLocationChanged
中,并保存到数据库或sharedpreferences
。当getLastKnownLocation
为空时,使用该保存的位置。
你可以用最佳供应办法去得到快速的位置尽可能地。
读this博客希望它可以帮助你。
嗨,我不确定这一点。我可以上传我的代码,并且可以为我检查吗? – GilbertLee 2014-10-28 05:28:25
雅尔肯定发布您的代码 – 2014-10-28 05:29:09
请检查代码 – GilbertLee 2014-10-28 05:34:13
首先您必须确保GPS已启用。第二件事是getLastKnownLocation()不能保证它有最后的位置。因此,您需要检查GPS是否无法提供位置,而不是您可以使用NETWORK提供商获取位置。 – 2014-10-28 04:56:38
@BirajZalavadia嗨,我确定GPS已启用。我的网络定位适用于所有设备,但我只是想确保GPS工作。我只是想知道为什么getLastKnownLocation()适用于相同条件下的某些设备。我尝试了GPS的requestLocationUpdates(),但它也没有工作。你有这个解决方案吗?谢谢 – GilbertLee 2014-10-28 05:14:42