사용자가 버튼을 클릭 할 때만 위치를 확인하는 앱을 만듭니다.android getLastKnownLocation() 항상 null을 반환합니다.
public void main(String[] args, int iteration, Context context){
Criteria criteria = new Criteria();
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
LocationListener loc_listener = new LocationListener() {
public void onLocationChanged(Location l) {
}
public void onProviderEnabled(String p) {}
public void onProviderDisabled(String p) {}
public void onStatusChanged(String p, int status, Bundle extras) {}
};
String bestProvider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(bestProvider, 0, 0, loc_listener);
Location location = locationManager.getLastKnownLocation(bestProvider);
location = locationManager.getLastKnownLocation(bestProvider);
double lat;
double lon;
try {
lat = location.getLatitude();
lon = location.getLongitude();
} catch (NullPointerException e) {
lat = -1.0;
lon = -1.0;
}
Log.i("results:", String.valueOf(lat) + " " + String.valueOf(lon) + " 10NN");}}
이 장치의 위치 서비스가 활성화되어 : 나는 requestLocationUpdates를 호출 후에도, 여기
내 코드, 그리고 내가 getLastKnownLocation를 호출 할 때 그것은 null를 돌려줍니다. 대단히 감사합니다!
을 PLS보십시오. 따라서 null이 될 가능성이 있습니다. 보십시오보십시오 http://stackoverflow.com/questions/7423624/how-reliable-is-locationmanagers-getlastknownlocation-and-how-often-is-it-updat – alekz
나는 locationUpdates를 요청하려고 시도했지만 실제로 업데이트하지 않습니다. 내가 아는 마지막 위치. –
해결책을 찾았습니까 – Questioner