Reputation: 3337
Is networking using HttpUrlConnection etc (for uploading data to server) possible with JobScheduler? Or I've to go with GCMNetworkManager? How can I perform network operation scheduling?
MainActivity.class
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());
JobInfo jobInfo =
new JobInfo.Builder(MYJOBID, jobService).setPeriodic(10000).build();
int jobId = jobScheduler.schedule(jobInfo);
if(jobScheduler.schedule(jobInfo)>0){
Toast.makeText(MainActivity.this,
"Successfully scheduled job: " + jobId,
Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(MainActivity.this,
"RESULT_FAILURE: " + jobId,
Toast.LENGTH_SHORT).show();
}
}});
MyJobService.class
public class MyJobService extends JobService {
public MyJobService() {
}
@Override
public boolean onStartJob(JobParameters jobParameters) {
Toast.makeText(this,"MyJobService.onStartJob()",Toast.LENGTH_SHORT).show();
//networking is not working here
return false;
}
@Override
public boolean onStopJob(JobParameters jobParameters) {
Toast.makeText(this,
"MyJobService.onStopJob()",
Toast.LENGTH_SHORT).show();
return false;
}
}
Upvotes: 0
Views: 1644
Reputation: 43314
Is networking using HttpUrlConnection etc (for uploading data to server) possible with JobScheduler?
Of course. You should use the .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
on the JobInfo builder though, to ensure your job does not run if there is no network available.
You have to do your network operation in JobService on a separate thread though, because the onStartJob is executed on the main thread.
Upvotes: 2