2

I am trying to send my location values periodically from android phone to a server application and i set the time period to 1 minute. When i started the service the jobservice works fine but after a few minutes when i am unlocking my phone, Jobservice send two data within 1 second. Is there anything wrong in my code

MainActivity.java

    JobScheduler jobScheduler=
 (JobScheduler)getSystemService(Context.JOB_SCHEDULER_SERVICE);

ComponentName jobService = new ComponentName(getPackageName(), 
ServiceClass.class.getName());
    JobInfo.Builder jobInfo = new JobInfo.Builder(MY_JOB, jobService);
    PersistableBundle bundle = new PersistableBundle();
    jobInfo.setPeriodic(period*60*1000);
 int jobId = jobScheduler.schedule(jobInfo.build());
    if(jobId>0){
        Toast.makeText(MainActivity.this,"Successfully scheduled job 
    ",Toast.LENGTH_SHORT).show();
        btstartservice.setEnabled(false);
        btstopservice.setEnabled(true);
    }else{
        Toast.makeText(MainActivity.this,"Faild to start service 
  ",Toast.LENGTH_SHORT).show();
    }

ServiceClass.java

   public class ServiceClass extends JobService implements LocationListener{
      public boolean onStartJob(JobParameters params) {
         getLocation();
         return false;
      }

      @Override
      public boolean onStopJob(JobParameters params) {
          Toast.makeText(getApplicationContext(),"Service 
          Stopped",Toast.LENGTH_SHORT).show();
          return false;
     }
}
Nazmul Haque
  • 319
  • 2
  • 12

1 Answers1

1

If you look at the JobInfo code, MIN_PERIOD_MILLIS is set to 15 minutes. Anything that is set below this period will actually use 15 minutes.

See this comment in the code too:

Query the minimum interval allowed for periodic scheduled jobs. Attempting to declare a smaller period that this when scheduling a job will result in a job that is still periodic, but will run with this effective period.

A recurring task with your interval will need some other service, possibly the Alarm Manager will work for you.

Steve Miskovetz
  • 2,360
  • 13
  • 29
  • I have tried Alarm Manager first but it did not perform well. I also read about the difference between Alarm Manager and JobScheduler and I think [JobScheduler](https://www.bignerdranch.com/blog/choosing-the-right-background-scheduler-in-android/) is efficient for periodic task. – Nazmul Haque Nov 08 '17 at 03:46
  • I agree, if your periodic task does not need an interval of <15 minutes, JobScheduler has many advantages. I'd be curious on the issues you see with Alarm Manager, but a different thread would probably be more appropriate. – Steve Miskovetz Nov 08 '17 at 19:15
  • See this thread too for Job intervals that <15 minutes: https://stackoverflow.com/questions/39641278/job-scheduler-in-android-n-with-less-then-15-minutes-interval – Steve Miskovetz Nov 10 '17 at 00:02
  • I have tried setting the periodic interval to 20 minutes, but the same thing happening when i unlock my phone. – Nazmul Haque Nov 10 '17 at 04:03