Java 使用GPS的位置并不总是正确的,并且在没有连接的情况下发生碰撞
当我试图找到我需要显示天气的当前位置时,我遇到了一个问题。这是我用来查找位置的方法:Java 使用GPS的位置并不总是正确的,并且在没有连接的情况下发生碰撞,java,android,gps,android-location,Java,Android,Gps,Android Location,当我试图找到我需要显示天气的当前位置时,我遇到了一个问题。这是我用来查找位置的方法: protected class GetPosition extends AsyncTask<String, Void, String> { protected String doInBackground(String... params) { List<Address> addresses = null; try {
protected class GetPosition extends AsyncTask<String, Void, String> {
protected String doInBackground(String... params) {
List<Address> addresses = null;
try {
LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
Geocoder gcd = new Geocoder(getApplicationContext(), Locale.getDefault());
addresses = gcd.getFromLocation(latitude, longitude, 1);
if (addresses.size() > 0) return addresses.get(0).getLocality();
} catch (Exception e) {
return "Nessuna posizione disponibile";
}
return addresses.get(0).getLocality();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
citylb.setText(s);
getWeather((String) citylb.getText());
}
}
我有两个问题:
1) 如果我没有任何连接,应用程序将在行崩溃,如果(netInfo!=null | | netInfo.isConnectedOrConnecting())
,netInfo似乎null
2) 位置并不总是正确的。它通常返回一个“旧”位置,而不是当前位置。如果两个小时前,我在罗马,现在在米兰,那就是罗马。我不明白我错在哪里
关于第一个问题,我也试着写:if(netInfo!=null&&netInfo.isConnectedOrConnecting())
而不是&
。不会崩溃,但它只能在wifi中找到位置,而不能在数据连接中找到位置。如何解决这些问题?谢谢当您使用该方法时,您将获得给定提供者返回的最后一个位置。如果该提供程序已有一段时间没有被任何其他应用程序使用,则返回的位置将是旧的。在这种情况下,您应该使用requestLocationUpdates()方法家族中的一个,该家族适合您的需要
关于崩溃,您应该在清单上包含以下权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
使用该方法时,您将获得给定提供程序返回的最后一个位置。如果该提供程序已有一段时间没有被任何其他应用程序使用,则返回的位置将是旧的。在这种情况下,您应该使用requestLocationUpdates()方法家族中的一个,该家族适合您的需要
关于崩溃,您应该在清单上包含以下权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
public Boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni != null && ni.isConnected())
return true;
return false;
}
(二)
同样也已经回答了——这是预期的行为
但是,我已经实现了一些代码,这些代码触发GPS,并在找到有效的新位置后立即发出回调(GPS firstfix)。您可以在我对这个问题的回答中找到代码:
获得有效位置后,您可以立即停止GPS。1)
已经回答了,但这里是我的isOnline()实现:
public Boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni != null && ni.isConnected())
return true;
return false;
}
(二)
也已经回答了-这是预期的行为
但是,我已经实现了一些代码,这些代码触发GPS,并在找到有效的新位置后立即发出回调(GPS firstfix)。您可以在我对这个问题的回答中找到代码:
获得有效位置后,您只需立即停止GPS即可
getLastKnownLocation()
获取位置,但未使用
locationManager.RequestLocationUpdate(locationManager.NETWORK_提供程序,2000,0,null)...
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 0, this);
...
public void onLocationChanged(Location arg0) {
if (arg0 != null){
double longitude = arg0.getLongitude();
double latitude = arg0.getLatitude();
...
}
}
LocationManager.NETWORK\u PROVIDER
不是接收位置的好方法,因为当设备靠近wifi路由器时,它使用另一个位置服务接收的位置。若由于某些原因,另一个服务并没有在某个路由器附近接收到该位置,它将不会为您提供正确的位置public class MyActivity extends Activity implements LocationListener
{
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
... // do something
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, // every 2 seconds
0, this);
}
...
public void onLocationChanged(Location arg0) {
if (arg0 != null){
double longitude = arg0.getLongitude();
double latitude = arg0.getLatitude();
...
}
}
}
getLastKnownLocation()
获取位置,但未使用
locationManager.RequestLocationUpdate(locationManager.NETWORK_提供程序,2000,0,null)...
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, 0, this);
...
public void onLocationChanged(Location arg0) {
if (arg0 != null){
double longitude = arg0.getLongitude();
double latitude = arg0.getLatitude();
...
}
}
LocationManager.NETWORK\u PROVIDER
不是接收位置的好方法,因为当设备靠近wifi路由器时,它使用另一个位置服务接收的位置。若由于某些原因,另一个服务并没有在某个路由器附近接收到该位置,它将不会为您提供正确的位置public class MyActivity extends Activity implements LocationListener
{
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
... // do something
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 2000, // every 2 seconds
0, this);
}
...
public void onLocationChanged(Location arg0) {
if (arg0 != null){
double longitude = arg0.getLongitude();
double latitude = arg0.getLatitude();
...
}
}
}
最主要的是没问题。已经有这个许可了。。所以我不能取消