我目前正在使用android.location.API开发一个Android应用程序。
通常GPS定位请求在某些手机(Galaxy Note2)中成功但在其他手机中失败(getLastKnownLocation()返回null)(Galaxy S3和S4)。
GPS很适合S3& S4如果我第一次激活三星内置功能" GPS卫星视图"它将扫描可用的卫星。
我的猜测是S3和& S4正在使用提供差的GPS信号的联发科技芯片。那么有谁知道如何执行像在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) {
}
}
答案 0 :(得分:0)
实际上这是设备问题。
某些设备不支持getLastKnownLocation
(例如Motorola XT720)。您必须自己实施LocationListener
并将onLocationChanged
中的位置更新保存到数据库或sharedpreferences
。当getLastKnownLocation
为null时,请使用保存的位置。
您可以采用最佳提供商方式尽可能快地获取位置。
阅读this博客希望它对您有所帮助。