我的应用程序需要一个位置,当 GPS 提供商打开时,一切正常 = 我正在获取我的位置。不幸的是,当 GPS 关闭时,即使它打开,我的应用也无法连接到网络提供商,我不知道为什么。
之后,屏幕上有消息Disabled provider Network
(此时网络可用),我的应用程序崩溃了。
代码:
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.NO_REQUIREMENT);
criteria.setPowerRequirement(Criteria.NO_REQUIREMENT);;
provider = locationManager.getBestProvider(criteria, false);
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
latituteField = "Location not available";
longitudeField = "Location not available";
}
}
/* Request updates at startup */
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider,
400, 1, this);
needGPS = true;
}
/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
if (needGPS == false || starttime == 0)
locationManager.removeUpdates(this);
}
protected void onStop() {
super.onPause();
if (needGPS == false || starttime == 0)
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
double lat = (double) (location.getLatitude());
double lng = (double) (location.getLongitude());
latituteField = (String.valueOf(lat));
longitudeField = (String.valueOf(lng));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
为什么每次都禁用网络提供商,即使我的 Galaxy SII 上的网络已打开?