Jobscheduler requires API 21+.
An alternative that works in all versions is the Gcmnetworkmanager.
It uses Jobscheduler in versions 21+ and in lower versions uses its own implementation.
To use it, your service must inherit from Gcmtaskservice.
Declare it on Androidmanifest.xml as follows:
<service
android:name=".MyTaskService"
android:exported="true"
android:permission="com.google.android.gms.permission.BIND_NETWORK_TASK_SERVICE">
<intent-filter>
<action android:name="com.google.android.gms.gcm.ACTION_TASK_READY" />
</intent-filter>
</service>
Get the Gcmnetworkmanager:
mGcmNetworkManager = GcmNetworkManager.getInstance(this);
Schedule a periodic task like this:
PeriodicTask task = new PeriodicTask.Builder()
.setService(MyTaskService.class)
.setTag(TASK_TAG_PERIODIC)
.setPeriod(30L)
.build();
mGcmNetworkManager.schedule(task);
At the time the scheduled task is executed, the system calls the method onRunTask()
of your service.
More alternatives:
Firebase Jobdispatcher that provides a Jobscheduler compatible API.
Works on all versions from API 9 that have Google Play services installed. Backward compatibility is achieved by using Gcmnetworkmanager.
Workmanager
New api included in the brand new Android Jetpack.
Uses support libraries, allowing backward compatibility.
References:
Dude, Google is full of great tutorials. I did a search now and the first result that came is already an excellent tutorial. I only had to type 3 words in the search: job Android Scheduler. Much simpler than writing a whole question.
– Márcio Oliveira
I couldn’t find information about it working in previous versions
– Igor Oliveira