gps - Implementing background services on Android -


i'm building application android track user gps positioning during ride (by sending events positioning information server), i've got 2 issues far. since user suspend application in order use other applications during ride, application stop send events. turn around issue, i've implemented background service keeps running when user switches application, , consequently keeps sending events server. seems somehow service suspended whenever phone turns off screen (go sleeping mode?). second issue getting gps positioning data phone. i've put necessary permissions application data gps receiver, , actually, sometimes, positiong retrieved correctly (at same moments when there other applications using gps well). when there no other applications using gps, positioning retrieved tuple (0.0, 0.0) longitude , latitude. it's unclear me why these 2 issues taking place. why background service getting suspended whenever screens turned off, , why gps returns correct positioning when there other applications requesting gps use?

i faced off same problem(first issue). resolve via using looper inside of service. second issue must see codeto you. i'll show code search location maybe help. used googleapiclient https://developer.android.com/training/location/retrieve-current.html

public class locationservice extends service implements com.google.android.gms.location.locationlistener  {     private thread mtriggerservice;      private handler mlooperhandler;     ...     private void addlocationlistener(){      // start thread listen location     mtriggerservice = new thread(new runnable(){         public void run(){             try{                 looper.prepare();//initialise current thread looper.                 mlooperhandler = new handler();                 initlocationmanager();                 looper.loop();             }catch(exception ex){                 ex.printstacktrace();             }         }     }, "locationthread");     mtriggerservice.start(); } /**  * strart listening location  */ public void initlocationmanager(context context) {     ...     mlocationrequest = new locationrequest();     mlocationrequest.setinterval(minterval);     //this controls fastest rate @ application receive location      //updates, might faster setinterval(long) in situations      //(for example, if other applications triggering location updates).(from doc)     mlocationrequest.setfastestinterval(mfastestinterval);     mlocationrequest.setpriority(locationrequest.priority_high_accuracy);     mgoogleapiclient = new googleapiclient.builder(context)             .addconnectioncallbacks(new googleapiclient.connectioncallbacks() {                 @override                 public void onconnected(bundle bundle) {                     log.d(tag, "onconnected");                     locationservices.fusedlocationapi.requestlocationupdates(                             mgoogleapiclient, mlocationrequest, this);                 }                  @override                 public void onconnectionsuspended(int i) {                     log.d(tag, "onconnectionsuspended");                 }              })             .addonconnectionfailedlistener(monconnectionfailedlistener)             .addapi(locationservices.api)             .build();     mgoogleapiclient.connect(); }  //stop listening location private void stoplocationlistener() {     if(mlocationhelper != null){         mlooperhandler.getlooper().quit();         if(mgoogleapiclient != null && mlocationlistener != null           && mgoogleapiclient.isconnected()) {             locationservices.fusedlocationapi.removelocationupdates(                 mgoogleapiclient, this);         }     } } } 

Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -