Schedule your task for internet and relax.
Hey android developers, I bet every one faces a problem handling network connection while making server transaction calls. Firstly you check if the internet is connected or not. Secondly you check if the internet is working or not and Thirdly handle your task that requires internet.
The commonly used approach for it is, you save your Request somewhere and the next time user opens the app, you check if there is any non-executed request there and internet is there, you execute it.
But heyyyyy, why bro why… why that much of pain when android keeps improving day by day with new features. The feature we are going to discuss about today is Android Job Scheduler.
Android Job Scheduler
Must have seen some apps, which allows you to proceed only when you are connected to wifi, or when the charger is plugged in. These actions can be performed easily using JobScheduler. In this lesson we will be doing the internet handling. You can more details on the official android documentation.
When I mean handling the internet connection, I refer to the ability of Jobscheduler to keep the data persistence. So even if your app is in backgound or removed from background too or even if the phone is rebooted, your task is executed for sure.
The code:
Create a JobService which contains a onStart() method which is executed. So your service looks like this
public class MyJobService extends JobService {
public MyJobService() {
}
@Override
public boolean onStartJob(JobParameters params) {
Toast.makeText(this,"Executed",Toast.LENGTH_LONG).show();
/*
* True - if your service needs to process
* the work (on a separate thread).
* False - if there's no more work to be done for this job.
*/
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
return false;
}
}So whenever the internet comes, this toast will be fired.
How to start the JobService:
jobScheduler = (JobScheduler)getSystemService(JOB_SCHEDULER_SERVICE);
ComponentName jobService = new ComponentName(getPackageName(), MyJobService.class.getName());
JobInfo jobInfo = new JobInfo.Builder(MYJOBID,jobService).setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY). build();
jobScheduler.schedule(jobInfo);here jobscheduler is an instance of JobScheduler and MYJOBID issome integer.
JobScheduler jobScheduler;
int MYJOBID = 1;You can send the data with the into the JobScheduler using a PersistableBundle .
PersistableBundle bundle = new PersistableBundle();
bundle.putString("name","Bruce Wayne");
bundle.putInt("age",43);
bundle.putBoolean(isSuperhero,true);and then we can add this bundle in JobInfo builder
JobInfo jobInfo = new JobInfo.Builder(MYJOBID,jobService).setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).setExtras(bundle). build();And lastly, register the JobService in the manifest.
<service
android:name=".MyJobService"
android:exported="true"
android:permission="android.permission.BIND_JOB_SERVICE" />Done. Now do whatever you want and the toast will be fired whenever internet connection is available.
Write to srivastava.prakhar91@gmail.com for any clarifiations.
