android - FusedLocationApi is not working while scheduling it with JobScheduler -


calling fusedlocationapi within jobscheduler doesn't work. have tried in 2 ways onconnected() never called in both of them. how can make work? thankyou

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);          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{                 }             }         });     } }    

try 1:

here have implemented connectioncallbacks, onconnectionfailedlistener in jobservice onconnected() method never called...

public class myjobservice extends jobservice implements googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener, locationlistener {     protected jobparameters mjobparameters;     private googleapiclient mgoogleapiclient;     locationrequest mlocationrequest;      public myjobservice() {     }      @override     public boolean onstartjob(jobparameters jobparameters) {         log.e("token", "start job called");         setuplocationclientifneeded();         mlocationrequest = locationrequest.create();         // use high accuracy         mlocationrequest.setpriority(locationrequest.priority_high_accuracy);         mlocationrequest.setinterval(5000);         return true;     }      @override     public boolean onstopjob(jobparameters jobparameters) {         toast.maketext(this,                 "myjobservice.onstopjob()",                 toast.length_short).show();         return false;     }      @override     public void onconnected(@nullable bundle bundle) {         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(this.mgoogleapiclient,                 mlocationrequest, this); // changed line.         log.e("onconnected", "onconnected");     }      @override     public void onconnectionsuspended(int i) {      }      @override     public void onconnectionfailed(@nonnull connectionresult connectionresult) {      }      private void setuplocationclientifneeded()     {         if(mgoogleapiclient == null)             buildgoogleapiclient();     }      protected synchronized void buildgoogleapiclient() {         this.mgoogleapiclient = new googleapiclient.builder(this)                 .addconnectioncallbacks(this)                 .addonconnectionfailedlistener(this)                 .addapi(locationservices.api)                 .build();         this.mgoogleapiclient.connect();     }      @override     public void onlocationchanged(location location) {         log.e("token",location.getlatitude()+""+location.getlongitude());     } } 

try 2:

called innerclass implements connectioncallbacks, onconnectionfailedlistener in onstartjob of jobservice still onconnected method never called

public class myjobservice extends jobservice {     private location mlastlocation;     string latitude = null;     string longitude = null;     protected jobparameters mjobparameters;      private class getlocation extends asynctask<integer, void, integer> implements googleapiclient.connectioncallbacks, googleapiclient.onconnectionfailedlistener {         protected integer doinbackground(integer... jobid) {             if (mgoogleapiclient == null) {                 mgoogleapiclient = new googleapiclient.builder(getbasecontext())                         .addconnectioncallbacks(this)                         .addonconnectionfailedlistener(this)                         .addapi(locationservices.api)                         .build();             }             return jobid[0];         }          protected void onpostexecute(integer jobid) {             //h2 = new handler();             //runnable run = new runnable() {             //   @override             //    public void run() {             //        long millis = system.currenttimemillis() - 0;             //        int seconds = (int) (millis / 1000);             //        int minutes = seconds / 60;             //        seconds     = seconds % 60;             //        log.i("jobschedulertest","job finished!");             //       h2.postdelayed(this, 1500);                      jobfinished(mjobparameters, true);                 }             };           }          @override         public void onconnected(@nullable bundle bundle) {              log.e("onconnected " , "onconnected" );             createlocationrequest();         }          @override         public void onconnectionsuspended(int i) {          }          @override         public void onconnectionfailed(@nonnull connectionresult connectionresult) {          }      protected void createlocationrequest() {         mlocationrequest = new locationrequest();         mlocationrequest.setinterval(5000);         mlocationrequest.setfastestinterval(5000);         mlocationrequest.setpriority(locationrequest.priority_high_accuracy);          if (activitycompat.checkselfpermission(myjobservice.this, android.manifest.permission.access_fine_location) != packagemanager.permission_granted && activitycompat.checkselfpermission(myjobservice.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);     }     }       public myjobservice() {     }      @override     public boolean onstartjob(jobparameters jobparameters) {         log.d("onstart", "onstartjob() :: ");          mjobparameters=jobparameters;         integer i=new integer(jobparameters.getjobid());         new getlocation().execute(i);          return false;     }      @override     public boolean onstopjob(jobparameters jobparameters) {         toast.maketext(this,                 "myjobservice.onstopjob()",                 toast.length_short).show();         return false;     } } 


Comments

Popular posts from this blog

Sort a complex associative array in PHP -

vb.net - How to ignore if a cell is empty nothing -

recursion - Can every recursive algorithm be improved with dynamic programming? -