android - Fused location provider with JobScheduler -


i've fused location provider code in mainactivity class providing latitude , longitude values, passed jobservice class using persistablebundle. works fine when user using app (ie. app in foreground). app swiped out or destroyed, last updated value mainactivity repeatedly uploaded through job scheduler time (ie. jobscheduler gets same value time, fused location provider don't work). should make work if app not in foreground? (ps. works when app minimized. ie. can seen in recent app list problem occurs swiped out list)

mainactivity.class

public class mainactivity extends appcompatactivity implements googleapiclient.connectioncallbacks,         googleapiclient.onconnectionfailedlistener {      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          // jobscheduler starts         btnstartjob = (button)findviewbyid(r.id.startjob);          jobscheduler = (jobscheduler)getsystemservice(job_scheduler_service);         btnstartjob.setonclicklistener(new view.onclicklistener(){              @override             public void onclick(view v) {                  componentname jobservice =                         new componentname(getpackagename(), myjobservice.class.getname());                 persistablebundle bundle = new persistablebundle();                 bundle.putstring("lat", latitude+"");                 bundle.putstring("lon", longitude+"");                  jobinfo jobinfo =                         new jobinfo.builder(myjobid, jobservice).setperiodic(10000).                         setrequirednetworktype(jobinfo.network_type_any).                         setrequirescharging(false).                         setrequiresdeviceidle(false).                         setpersisted(true).                         setextras(bundle).                         build();                  int jobid = jobscheduler.schedule(jobinfo);                 if(jobscheduler.schedule(jobinfo)>0){                 }else{                 }             }         });     }      protected synchronized void buildgoogleapiclient() {         mgoogleapiclient = new googleapiclient.builder(this)             .addconnectioncallbacks(this)             .addonconnectionfailedlistener(this)             .addapi(locationservices.api)             .build();     }       private boolean checkplayservices() {         int resultcode = googleplayservicesutil.isgoogleplayservicesavailable(this);         if (resultcode != connectionresult.success) {             if (googleplayservicesutil.isuserrecoverableerror(resultcode)) {                 googleplayservicesutil.geterrordialog(resultcode, this,                         play_services_resolution_request).show();             } else {                 toast.maketext(getapplicationcontext(),                         "this device not supported.", toast.length_long)                         .show();                 finish();             }             return false;         }         return true;     }      @override     public void onconnected(bundle bundle) {         createlocationrequest(bundle);     }      protected void createlocationrequest(bundle bundle) {         mlocationrequest = new locationrequest();         mlocationrequest.setinterval(10000);         mlocationrequest.setfastestinterval(5000);         mlocationrequest.setpriority(locationrequest.priority_high_accuracy);          locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, new locationcallback() {             @override             public void onlocationresult(final locationresult locationresult) {                 latitude = locationresult.getlastlocation().getlatitude() + "";                 longitude = locationresult.getlastlocation().getlongitude() + "";                 log.e("onlocationresult lat", latitude);                 log.e("onlocationresult lon", longitude);             }              @override             public void onlocationavailability(locationavailability locationavailability) {             }         }, null);     }      @override     public void onconnectionsuspended(int i) {         mgoogleapiclient.connect();     }      @override     public void onconnectionfailed(connectionresult result) {         log.i(tag, "connection failed: connectionresult.geterrorcode() = "                 + result.geterrorcode());     }      @override     protected void onpause() {         super.onpause();     }      @override     protected void onresume() {         super.onresume();         checkplayservices();     }      @override     protected void onstart() {         super.onstart();         if (mgoogleapiclient != null) {             mgoogleapiclient.connect();             log.i(tag, "mgoogleapiclient.connect()");         }     }      @override     public void ondestroy() {         super.ondestroy();     } } 

myjobservice class

public class myjobservice extends jobservice {      @override     public boolean onstartjob(jobparameters jobparameters) {         latitude = jobparameters.getextras().getstring("lat");         longitude = jobparameters.getextras().getstring("lon");         log.e("service1",latitude + "");         log.e("service2",longitude + "");         return true;     } } 

update 1:

tried implement fused location in jobservice doesnot work

public class myjobservice extends jobservice implements googleapiclient.connectioncallbacks,         googleapiclient.onconnectionfailedlistener{      string latitude = null;     string longitude = null;       public myjobservice() {     }      @override     public boolean onstartjob(jobparameters jobparameters) {         log.d("onstart", "onstartjob() :: ");         return false;     }      @override     public boolean onstopjob(jobparameters jobparameters) {         toast.maketext(this,                 "myjobservice.onstopjob()",                 toast.length_short).show();         return false;     }      //fused location provider starts      private googleapiclient mgoogleapiclient;     private final static int play_services_resolution_request = 1000;     private locationrequest mlocationrequest;     private static final string tag = "zzzz";      protected synchronized void buildgoogleapiclient() {         mgoogleapiclient = new googleapiclient.builder(this)                 .addconnectioncallbacks(this)                 .addonconnectionfailedlistener(this)                 .addapi(locationservices.api)                 .build();     }       private boolean checkplayservices() {         int resultcode = googleplayservicesutil.isgoogleplayservicesavailable(this);         if (resultcode != connectionresult.success) {             if (googleplayservicesutil.isuserrecoverableerror(resultcode)) { //                googleplayservicesutil.geterrordialog(resultcode, this, //                        play_services_resolution_request).show();                 log.e("googleplayservices", resultcode + "");             } else {                 toast.maketext(getapplicationcontext(),                         "this device not supported.", toast.length_long)                         .show();                 stopself();             }             return false;         }         return true;     }      @override     public void onconnected(bundle bundle) {         createlocationrequest(bundle);     }      protected void createlocationrequest(bundle bundle) {         mlocationrequest = new locationrequest();         mlocationrequest.setinterval(5000);         mlocationrequest.setfastestinterval(5000);         mlocationrequest.setpriority(locationrequest.priority_high_accuracy);          if (activitycompat.checkselfpermission(this, android.manifest.permission.access_fine_location) != packagemanager.permission_granted && activitycompat.checkselfpermission(this, android.manifest.permission.access_coarse_location) != packagemanager.permission_granted) {              return;         }          locationservices.fusedlocationapi.requestlocationupdates(mgoogleapiclient, mlocationrequest, new locationcallback() {             @override             public void onlocationresult(final locationresult locationresult) {                 latitude = locationresult.getlastlocation().getlatitude() + "";                 longitude = locationresult.getlastlocation().getlongitude() + "";                 log.e("onlocationresult lat", latitude);                 log.e("onlocationresult lon", longitude);             }              @override             public void onlocationavailability(locationavailability locationavailability) {             }         }, null);     }      @override     public void onconnectionsuspended(int i) {         mgoogleapiclient.connect();     }      @override     public void onconnectionfailed(connectionresult result) {         log.i(tag, "connection failed: connectionresult.geterrorcode() = "                 + result.geterrorcode());     } // fused location provider ends } 

you need put fused location service logic in job service, because when close app location listener destroyed, need new location in job service when app not available in foreground or background, when job service called decided time need implement logic of getting location, logic of update location,

you can location using last known location.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -