android - Get gps coordinates with asyncTask -


i need current coordinates. try next, not result.

public class fragmentnear extends sherlockfragment implements     android.location.locationlistener { @override public view oncreateview(layoutinflater inflater, viewgroup container,         bundle savedinstancestate) {     locationmanager = (locationmanager) getactivity().getsystemservice(             context.location_service);     locationmanager.requestlocationupdates(             locationmanager.gps_provider, 0, 0, this);     v = inflater.inflate(r.layout.fragment_near, null);     downloadwebpagetask dw = new downloadwebpagetask();     dw.execute("");     return v; }  @override public void onlocationchanged(location loc) {     if (showmfd == 0) {      log.d("mylogs", "-------- onlochanged " + mylat + " " + mylong);     mylat = loc.getlatitude();     mylong = loc.getlongitude();     log.d("mylogs", "-------- onlochangedend " + mylat + " " + mylong);      }     } public void showmfd() {     showmfd++;             //my tasks             }  private class downloadwebpagetask extends asynctask<string, void, string> {     @override     protected string doinbackground(string... urls) {         log.d("mylogs", "background");         while (mylat==0)         {          }         showmfd();         return "";     } @override protected void onpreexecute() { super.onpreexecute();  pb = new progressdialog(getactivity()); pb.setmessage("Получение данных gps..."); pb.setprogressstyle(progressdialog.style_spinner); pb.show(); }  @override  protected void onpostexecute(string result) {         pb.dismiss();  } } 

my logic in pictureimage one

an example of tracker class use wifi or classic gps (3g, h+, gprs...)

public class gpstracker extends service implements android.location.locationlistener {     /**      * attributes      */     private static final string tag                             = gpstracker.class.getsimplename();     private context             _context;     private static gpstracker   _instance;     // minimum distance change updates in meters     private static final long   min_distance_change_for_updates = 0;                                // meters     // minimum time between updates in milliseconds     private static final long   min_time_bw_updates             = 1000 * 30 * 1;                    // minute     // declaring location manager     protected locationmanager   _locationmanager;     /**      * public attributes      */     boolean                     _isgpsenabled                   = false;     boolean                     _isnetworkenabled               = false;     boolean                     _cangetlocation                 = false;     public location             _location;     double                      _latitude;     double                      _longitude;      public gpstracker() {          _context = null;     }      public static gpstracker getinstance() {          if (_instance == null) {             _instance = new gpstracker();         }         return _instance;     }      public void set_context(context context) {          this._context = context;     }      public location getlocation(context context) {          _context = context;         try {             _locationmanager = (locationmanager) _context.getsystemservice(location_service);             _isgpsenabled = _locationmanager.isproviderenabled(locationmanager.gps_provider);             _isnetworkenabled = _locationmanager.isproviderenabled(locationmanager.network_provider);             if (!_isgpsenabled && !_isnetworkenabled) {                 // no network provider enabled             }             else {                 this._cangetlocation = true;                 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) {             // nothing         }         return _location;     }      public void stopusinggps() {          if (_locationmanager != null) {             _locationmanager.removeupdates(gpstracker.this);         }     }      public double getlatitude() {          if (_location != null) {             _latitude = _location.getlatitude();         }         return _latitude;     }      public double getlongitude() {          if (_location != null) {             _longitude = _location.getlongitude();         }         return _longitude;     }      public boolean cangetlocation() {          return this._cangetlocation;     }      /**      * function show settings alert dialog on pressing settings button lauch settings options      */     public void showsettingsalert(string title, string msg) {          alertdialog.builder alertdialog = new alertdialog.builder(_context);         alertdialog.settitle(title);         alertdialog.setmessage(msg);         // settings button         alertdialog.setpositivebutton("paramètres", new dialoginterface.onclicklistener() {             public void onclick(dialoginterface dialog, int which) {                  intent intent = new intent(settings.action_location_source_settings);                 _context.startactivity(intent);             }         });         // cancel button         alertdialog.setnegativebutton("annuler", new dialoginterface.onclicklistener() {             public void onclick(dialoginterface dialog, int which) {                  dialog.cancel();             }         });         // show         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;     } } 

it fast network/gps/phone (thanks android) need create gpstracker.java object :

//create instance singleton gpstracker tracker = gpstracker.getinstance(); // can use in "if" test if location updated ok gpstracker.cangetlocation() //update location as want method tracker.getlocation(this); //retreive location when need location location = etablissementapplication._gpstracker._location 

in ondestroy() or when don't want use gps anymore don't forget :

if (tracker != null) {     tracker.stopusinggps(); } 

Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -