我的应用需要一个位置,当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上的网络已开启?
答案 0 :(得分:0)
这可能是许可问题。 打开AndroidMaifest.xml并确保你有
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />