在下面的代碼中,總是在設備上返回null,即使我得到的是Provide = internet
。它工作正常,但知道它返回一個空值。我正在使用Galaxy Tab版本2.2。getLastKnownLocation()在設備上返回始終爲空
public void find_Location(Context con) {
Log.d("Find Location", "in find_location");
this.con=con;
String location_context = Context.LOCATION_SERVICE;
LocationManager locationManager =
(LocationManager)con.getSystemService(location_context);
List<String> providers = locationManager.getProviders(true);
for (String provider : providers) {
locationManager.requestLocationUpdates(provider, 1000, 0,new LocationListener() {
public void onLocationChanged(Location location) {}
public void onProviderDisabled(String provider){}
public void onProviderEnabled(String provider){}
public void onStatusChanged(String provider, int status,Bundle extras){}
});
Location location = locationManager.getLastKnownLocation(provider);
if (location != null) {
lat = location.getLatitude();
lng = location.getLongitude();
Geocoder geocoder = new Geocoder(AdvanceSearch.this,
Locale.getDefault());List<Address> addresses;
try {
addresses = geocoder.getFromLocation(lat,lng,100);
countryname=addresses.get(0).getCountryName();
eexit e = new eexit();
statename= addresses.get(0).getAdminArea();
cityname=addresses.get(0).getLocality();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
在此先感謝您的幫助。
你需要時間去修復。檢查何時調用onLocationChanged。 – hsz
http://stackoverflow.com/questions/1916568/getlastknownlocation-getting-null-on-sdk?rq=1 檢查這1出。這與你的問題完全相同。 –
我等待arrount 4分鐘,但onLocationChanged從來沒有打電話 – sara