android - Geting only actual location from network provider, not lastknowlocation -


i want location network. dont want lasknowlocation, if network provider enabled, i´m not online. if provider enabled want actual location(lat , long) if it´s not actulal want null (or else).

i´m using

package com.example.tankograf_test;  import android.app.alertdialog; import android.app.service; import android.content.context; import android.content.dialoginterface; import android.content.intent; import android.location.location; import android.location.locationlistener; import android.location.locationmanager; import android.os.bundle; import android.os.ibinder; import android.provider.settings; import android.util.log;  public class gpshelper 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; // 10000 meters  // minimum time between updates in milliseconds private static final long min_time_bw_updates = 1000 * 60 * 1; // 20 minute  // declaring location manager protected locationmanager locationmanager;  public gpshelper(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 = 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 = 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(gpshelper.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");      // setting dialog message     alertdialog.setmessage("gps neni povoleno, chce jit nastaveni?");      // 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; }  } 

and geting location in activity

gps = new gpshelper(this);                  if(gps.cangetlocation()){                      double lat = gps.getlatitude(); // returns latitude                     double lon =  gps.getlongitude(); // returns longitude                     log.d("pozice", string.valueof(lat) + ", "+string.valueof(lat));                       location net = gps.getlocation();                     if (net==null){                         log.d("getlocation", "pozice je prazdna");                     } else {                         log.d("getlocation", "pozice neni prazdna");                     }                      double latn = net.getlatitude();                     double lonn = net.getlongitude();                     if (latn!=0){                         log.d("getlocation", "location not empty");                         log.d("lat", string.valueof(latn));                         log.d("lon", string.valueof(lonn));                         pozice[0] = net.getlatitude();                         pozice[1] = net.getlongitude();                           zobrazpozici(pozice[0], pozice[1],7 ,true);                         vypocitejvzdalenost();                     } else {                         log.d("getlocation", "position empty = something");                     } 

i tried solved on own, don´t know how :( it´s not vitally important, fine if done :)

(i litte confused getting location form network when wokrs , when don´t ope question make sense)


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -