【发布时间】:2014-12-23 09:47:39
【问题描述】:
我目前正在使用 android.location.API 开发一个 android 应用程序。
通常 GPS 定位请求在某些手机(Galaxy Note2)中成功,但在其他手机(Galaxy S3 和 S4)中失败(getLastKnownLocation() 返回 null)。
如果我首先激活三星内置功能“GPS Satellite View”,它会扫描可用的卫星,那么 GPS 在 S3 和 S4 上工作很奇怪。
我的猜测是 S3 和 S4 都使用了 GPS 信号较差的 Mediatek 芯片。那么有人知道如何在android中执行类似扫描卫星的功能吗?
请注意,我的应用在中国使用,因此请不要推荐任何与 Google 相关的服务。
谢谢。
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) {
}
}
【问题讨论】:
-
首先您必须确保 GPS 已启用。第二件事是 getLastKnownLocation() 不能保证它有最后一个位置。因此,您需要检查 GPS 是否无法提供位置,而不是使用 NETWORK 提供程序来获取位置。
-
@BirajZalavadia 嗨,我确定 GPS 已启用。我的网络定位适用于所有设备,但我只想确保 GPS 正常工作。我只是想知道为什么 getLastKnownLocation() 在相同条件下适用于某些设备。我尝试了 GPS 的 requestLocationUpdates() 但它也没有工作。您对此有解决方案吗?谢谢
标签: android gps samsung-mobile