android - How to get a users current state(location) -


i'm trying create android app calculates sales tax(this dependent on state or province you're in) can't figure out how state/province android user in.

this best approach:

first of check providers enabled. may disabled on device, may disabled in application manifest. if provider available start location listeners , timeout timer. it's 20 seconds in example, may not enough gps can enlarge it. if update location listener use provided value. stop listeners , timer. if don't updates , timer elapses have use last known values. grab last known values available providers , choose recent of them. here's how use class:

locationresult locationresult = new locationresult(){     @override     public void gotlocation(location location){         //got location!     } }; mylocation mylocation = new mylocation(); mylocation.getlocation(this, locationresult);   import java.util.timer; import java.util.timertask; import android.content.context; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle;  public class mylocation {     timer timer1;     locationmanager lm;     locationresult locationresult;     boolean gps_enabled=false;     boolean network_enabled=false;      public boolean getlocation(context context, locationresult result)     {         //i use locationresult callback class pass location value mylocation user code.         locationresult=result;         if(lm==null)             lm = (locationmanager) context.getsystemservice(context.location_service);          //exceptions thrown if provider not permitted.         try{gps_enabled=lm.isproviderenabled(locationmanager.gps_provider);}catch(exception ex){}         try{network_enabled=lm.isproviderenabled(locationmanager.network_provider);}catch(exception ex){}          //don't start listeners if no provider enabled         if(!gps_enabled && !network_enabled)             return false;          if(gps_enabled)             lm.requestlocationupdates(locationmanager.gps_provider, 0, 0, locationlistenergps);         if(network_enabled)             lm.requestlocationupdates(locationmanager.network_provider, 0, 0, locationlistenernetwork);         timer1=new timer();         timer1.schedule(new getlastlocation(), 20000);         return true;     }      locationlistener locationlistenergps = new locationlistener() {         public void onlocationchanged(location location) {             timer1.cancel();             locationresult.gotlocation(location);             lm.removeupdates(this);             lm.removeupdates(locationlistenernetwork);         }         public void onproviderdisabled(string provider) {}         public void onproviderenabled(string provider) {}         public void onstatuschanged(string provider, int status, bundle extras) {}     };      locationlistener locationlistenernetwork = new locationlistener() {         public void onlocationchanged(location location) {             timer1.cancel();             locationresult.gotlocation(location);             lm.removeupdates(this);             lm.removeupdates(locationlistenergps);         }         public void onproviderdisabled(string provider) {}         public void onproviderenabled(string provider) {}         public void onstatuschanged(string provider, int status, bundle extras) {}     };      class getlastlocation extends timertask {         @override         public void run() {              lm.removeupdates(locationlistenergps);              lm.removeupdates(locationlistenernetwork);               location net_loc=null, gps_loc=null;              if(gps_enabled)                  gps_loc=lm.getlastknownlocation(locationmanager.gps_provider);              if(network_enabled)                  net_loc=lm.getlastknownlocation(locationmanager.network_provider);               //if there both values use latest 1              if(gps_loc!=null && net_loc!=null){                  if(gps_loc.gettime()>net_loc.gettime())                      locationresult.gotlocation(gps_loc);                  else                      locationresult.gotlocation(net_loc);                  return;              }               if(gps_loc!=null){                  locationresult.gotlocation(gps_loc);                  return;              }              if(net_loc!=null){                  locationresult.gotlocation(net_loc);                  return;              }              locationresult.gotlocation(null);         }     }      public static abstract class locationresult{         public abstract void gotlocation(location location);     } } 

for example if update network provider don't stop listeners continue waiting. gps gives more accurate data it's worth waiting it. if timer elapses , you've got update network not gps can use value provided network.


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -