我的 Gps 设置一开始是“关闭”的,我执行了这段代码,我可以得到 CurrentLocation 的纬度和经度。
但是一旦我以编程方式打开,设置就会改变,一切都很好。
当我再次执行时,它没有给我(提供者、位置参考),我不明白为什么会这样。
public void getCurrentlocation()
{
String providers = Settings.Secure.getString(getContentResolver(), Settings.Secure.LOCATION_PROVIDERS_ALLOWED);
Log.e("provider",providers);
if(!providers.contains("gps")){
final Intent poke = new Intent();
poke.setClassName("com.android.settings", "com.android.settings.widget.SettingsAppWidgetProvider");
poke.addCategory(Intent.CATEGORY_ALTERNATIVE);
poke.setData(Uri.parse("3"));
sendBroadcast(poke);
Log.e("your gps is","enabled");
}
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria criteria = new Criteria();
String provider = locationManager.getBestProvider(criteria,true);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null)
{
System.out.println("Provider " + provider + " has been selected.");
strLatitude=String.valueOf(location.getLatitude());
strLongitude=String.valueOf(location.getLongitude());
Log.e("lattitude",strLatitude);
Log.e("logntitude",strLongitude);
}
else
{
strLatitude=String.valueOf(0);
strLongitude=String.valueOf(0);
Log.e("lattitude",strLatitude);
Log.e("logntitude",strLongitude);
}
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
try {
List<Address> addresses = geocoder.getFromLocation(Double.parseDouble(strLatitude), Double.parseDouble(strLongitude), 5);
getAddress(Double.parseDouble(strLatitude), Double.parseDouble(strLongitude));
if(addresses != null) {
Address returnedAddress = addresses.get(0);
StringBuilder strReturnedAddress = new StringBuilder("Address:\n");
for(int i=0; i<returnedAddress.getMaxAddressLineIndex(); i++) {
strReturnedAddress.append(returnedAddress.getAddressLine(i)).append("\n");
}
Log.e("address",strReturnedAddress.toString());
}
else{
Log.e("not found","No Address returned!");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("cannot get address","Canont get Address!");
}
}