android - Wait until GPS location is retrived for ListActivity -


i working on android project in using gps data showing nearby restaurants. on server side, have implemented haversine formula nearby restaurants depending upon longitude , latitude retrieved. but, having problem how should tell listactivity class wait until non-zero gps location retrieved.

i checked similar questions, didn't provide listactivity. nice. lot.

listrestaurants code :

public class restaurantlist extends listactivity  {      private responseentity<restrestaurant[]> responseentity;      private orderadapter m_adapter;      private arraylist<restrestaurant> m_orders = null;      gpstracker gps;      double longitude, latitude;      @override     public void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.restos);         final resttemplate resttemplate = staticresttemplate.getrest();         final string restaurantlist = staticresttemplate.baseurl+"getnearbyrestaurants";           gps = new gpstracker(restaurantlist.this);         long timeout = 5000l; // 5 seconds         long starttime = new date().gettime();         while (!gps.cangetlocation()) {             gps.showsettingsalert();             long difference = new date().gettime() - starttime;             if (timeout > difference) break;          }         latitude = gps.getlatitude();         longitude = gps.getlongitude();         toast.maketext(getapplicationcontext(), "your location - \nlat: " + latitude + "\nlong: " + longitude, toast.length_long).show();         /* if (gps.cangetlocation()) {             latitude = gps.getlatitude();             longitude = gps.getlongitude();             toast.maketext(getapplicationcontext(), "your location - \nlat: " + latitude + "\nlong: " + longitude, toast.length_long).show();          } else {             gps.showsettingsalert();         }*/            thread thread = new thread(new runnable() {             @override             public void run() {                 httpheaders requestheaders = new httpheaders();                 requestheaders.add("cookie", "jsessionid=" + staticresttemplate.jsessionid);                 requestheaders.setaccept(collections.singletonlist(new mediatype("application", "json")));                 httpentity<?> requestentity = new httpentity<object>(requestheaders);                 resttemplate.getmessageconverters().add(new mappingjackson2httpmessageconverter());                 responseentity = resttemplate.exchange(restaurantlist+"/"+longitude+"/"+latitude,                         httpmethod.get, requestentity, restrestaurant[].class);             }         });         thread.start();         progressdialog progress = new progressdialog(this);         progress.settitle("loading");         progress.setmessage("wait while loading...");         try {             thread.join();         } catch (interruptedexception e) {             e.printstacktrace();         }         progress.dismiss();         restrestaurant[] restrestaurantlist = responseentity.getbody();          m_orders = new arraylist<restrestaurant>();         this.m_adapter = new orderadapter(this, android.r.layout.simple_list_item_1, m_orders);          (restrestaurant restrestaurant1 : restrestaurantlist) {             m_adapter.add(restrestaurant1);          }          setlistadapter(this.m_adapter); 

now, can see in thread, passing longitude , latitude. don't want pass non-zero data there.

here gpstracker code using :

public class gpstracker extends service implements locationlistener {      private final context mcontext;      // flag gps status     boolean isgpsenabled = false;      // flag network status     boolean isnetworkenabled = false;      // flag gps status     boolean cangetlocation = false;      location location; // location     double latitude; // latitude     double longitude; // longitude      // minimum distance change updates in meters     private static final long min_distance_change_for_updates = 10; // 10 meters      // minimum time between updates in milliseconds     private static final long min_time_bw_updates = 1000 * 60 * 1; // 1 minute      // declaring location manager     protected locationmanager locationmanager;      public gpstracker(context context) {         this.mcontext = context;         getlocation();     }      public location getlocation() {         try {             locationmanager = (locationmanager) mcontext                     .getsystemservice(location_service);              // getting gps status             isgpsenabled = locationmanager                     .isproviderenabled(locationmanager.gps_provider);              // getting network status             isnetworkenabled = locationmanager                     .isproviderenabled(locationmanager.network_provider);              if (!isgpsenabled && !isnetworkenabled) {                 // no network provider enabled             } else {                 this.cangetlocation = true;                 // first location network provider                 if (isnetworkenabled) {                     locationmanager.requestlocationupdates(                             locationmanager.network_provider,                             min_time_bw_updates,                             min_distance_change_for_updates, this);                     log.d("network", "network");                     if (locationmanager != null) {                         location = locationmanager                                 .getlastknownlocation(locationmanager.network_provider);                         if (location != null) {                             latitude = location.getlatitude();                             longitude = location.getlongitude();                         }                     }                 }                 // if gps enabled lat/long using gps services                 if (isgpsenabled) {                     if (location == null) {                         locationmanager.requestlocationupdates(                                 locationmanager.gps_provider,                                 min_time_bw_updates,                                 min_distance_change_for_updates, this);                         log.d("gps enabled", "gps enabled");                         if (locationmanager != null) {                             location = locationmanager                                     .getlastknownlocation(locationmanager.gps_provider);                             if (location != null) {                                 latitude = location.getlatitude();                                 longitude = location.getlongitude();                             }                         }                     }                 }             }          } catch (exception e) {             e.printstacktrace();         }          return location;     }      /**      * stop using gps listener      * calling function stop using gps in app      * */     public void stopusinggps(){         if(locationmanager != null){             locationmanager.removeupdates(gpstracker.this);         }     }      /**      * function latitude      * */     public double getlatitude(){         if(location != null){             latitude = location.getlatitude();         }          // return latitude         return latitude;     }      /**      * function longitude      * */     public double getlongitude(){         if(location != null){             longitude = location.getlongitude();         }          // return longitude         return longitude;     }      /**      * function check gps/wifi enabled      * @return boolean      * */     public boolean cangetlocation() {         return this.cangetlocation;     }      /**      * function show settings alert dialog      * on pressing settings button lauch settings options      * */     public void showsettingsalert(){         alertdialog.builder alertdialog = new alertdialog.builder(mcontext);          // setting dialog title         alertdialog.settitle("gps settings");          // setting dialog message         alertdialog.setmessage("gps not enabled. want go settings menu?");          // on pressing settings button         alertdialog.setpositivebutton("settings", new dialoginterface.onclicklistener() {             public void onclick(dialoginterface dialog,int which) {                 intent intent = new intent(settings.action_location_source_settings);                 mcontext.startactivity(intent);             }         });          // on pressing cancel button         alertdialog.setnegativebutton("cancel", new dialoginterface.onclicklistener() {             public void onclick(dialoginterface dialog, int which) {                 dialog.cancel();             }         });          // showing alert message         alertdialog.show();     }      @override     public void onlocationchanged(location location) {     }      @override     public void onproviderdisabled(string provider) {     }      @override     public void onproviderenabled(string provider) {     }      @override     public void onstatuschanged(string provider, int status, bundle extras) {     }      @override     public ibinder onbind(intent arg0) {         return null;     }  } 

what ask user turn on gps , call thread code , set adapter. new android, please take consideration. thank you.

you can use while wait until timeout or getting location

long timeout = 5000l; // 5 seconds  long starttime = new date().gettime(); while (!gps.cangetlocation()) {     gps.showsettingsalert();     long difference = new date().gettime() - starttime;     if (timeout > difference) break; }  latitude = gps.getlatitude(); longitude = gps.getlongitude();  toast.maketext(getapplicationcontext(), "your location - \nlat: " + latitude + "\nlong: " + longitude, toast.length_long).show(); 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -