geolocation - How to make a function executino wait in android -
i new android programming. please forgive me if stupid question.
in app,i trying develop feature if location services turned off in phone, app prompt user turn on taking them location settings page on click of button. problem after turning on location settings phone taking time give location coordinates due fragment suppose show coordinates remaining empty same amount of time , creating confusion.
what want know is, if there way can schedule execution of function fetching location coordinates after beingness sure there coordinates fetch.
i using "getlastknowlocation()". code fetching location below:-
public string getlocation() { // location manager locationmanager locationmanager = (locationmanager)getsystemservice(location_service); criteria criteria = new criteria(); string bestprovider = locationmanager.getbestprovider(criteria, false); android.location.location location = locationmanager.getlastknownlocation(bestprovider); double lat,lon; dateformat dateformat = new simpledateformat("yyyy/mm/dd hh:mm:ss"); date date = new date(); seek { lat = location.getlatitude (); lon = location.getlongitude (); } grab (nullpointerexception e){ } }
create boolean variable locationloaded. in next lines of code:
try { lat = location.getlatitude (); lon = location.getlongitude (); //set value of variable true 1 time location locationloaded=true; } grab (nullpointerexception e){ } then create new recursive method, maintain checking location, until not loaded, this:
private void keepcheckinglocation(){ if(locationloaded){ //location loaded return; } else{ //location not loaded yet seek { //wait 2 seconds thread.sleep(2000); } grab (interruptedexception e) { // todo auto-generated grab block e.printstacktrace(); } //call method 1 time again check location keepcheckinglocation(); } } once have called method, won't allow next line of code execute until locationloaded not true , locationloaded true, 1 time location loaded. can utilize method whenever want wait location loaded.
note: set locationloaded=false; if have check locations again. create code more efficient.
android geolocation
No comments:
Post a Comment