1

I have a job service that i want to excecute for every three hours, I have made the jobservice class but I don't know how to excecute it every three hours.

here is my Jobservice class

public class CleanupJobService extends JobService {
private static final String TAG = CleanupJobService.class.getSimpleName();

@Override
public boolean onStartJob(JobParameters params) {
    Log.d(TAG, "Cleanup job started");
    new CleanupTask().execute(params);

    //Work is not yet complete
    return true;
}

@Override
public boolean onStopJob(JobParameters params) {
    //No need to reschedule any jobs
    return false;
}

/* Handle access to the database on a background thread */
private class CleanupTask extends AsyncTask<JobParameters, Void, JobParameters> {

    @Override
    protected JobParameters doInBackground(JobParameters... params) {
        String where = String.format("%s = ?", DatabaseContract.TaskColumns.IS_COMPLETE);
        String[] args = {"1"};

        int count = getContentResolver().delete(DatabaseContract.CONTENT_URI, where, args);
        Log.d(TAG, "Cleaned up " + count + " completed tasks");

        return params[0];
    }

    @Override
    protected void onPostExecute(JobParameters jobParameters) {
        //Notify that the work is now done
        jobFinished(jobParameters, false);
    }
}

}

and registered it on Manifest

    <service
        android:name=".data.CleanupJobService"
        android:permission="android.permission.BIND_JOB_SERVICE"
        android:exported="true"/>

Any idea to resolve this ? Thanks!

mangkool
  • 316
  • 2
  • 18
  • solved, I am using jobInfo class to solve this, the code should look like this ComponentName jobService = new ComponentName(getContext(), CleanupJobService.class); JobInfo task = new JobInfo.Builder(CLEANUP_JOB_ID, jobService) .setPeriodic(jobInterval) .setPersisted(true) .build(); – mangkool Oct 02 '17 at 00:42

1 Answers1

0

solved, I am using jobInfo class to solve this, the code should look like this

ComponentName jobService = new ComponentName(getContext(), CleanupJobService.class); 
JobInfo task = new JobInfo.Builder(CLEANUP_JOB_ID, jobService) 
           .setPeriodic(jobInterval) 
           .setPersisted(true) 
           .build();

jobInterval is the predefined time that we want the service to excecute, the type is Long and have a millis format.

grrigore
  • 1,050
  • 1
  • 21
  • 39
mangkool
  • 316
  • 2
  • 18