Pro Android with Kotlin


Download 5.42 Mb.
Pdf ko'rish
bet115/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   111   112   113   114   115   116   117   118   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

Firebase JobDispatcher
This is a general multipurpose job dispatcher library you can use for an 
Android device starting from API level 14, targeting more than 99 percent 
of the Android devices in use. It is a little hard to find comprehensive and 
complete documentation for the Firebase JobDispatcher on the Web
but you will find enough examples to get you started. It is not part of the 
Android OS core, though, and it requires Google Play Services and the 
Google Play store to be installed.


130
CHAPTER 8: APIs

JobScheduler
This is an integrated library for scheduling jobs on the Android OS. It runs 
on any device starting at API level 21, which is for about 85 percent of the 
Android devices in use. It is highly recommended to use it, unless you really 
need to address devices before API level 21, that is, Android 4.4 and older.
The more low-level approaches are covered in Chapter 
10
; the rest of this section is about 
the Firebase JobDispatcher, the JobScheduler, and the AlarmManager.
JobScheduler
The JobScheduler is the dedicated method to schedule and run background tasks in 
any Android device starting at API level 21. The documentation of Android 8 strongly 
recommends using JobSchedulers to overcome the background task execution restrictions 
imposed since Android 8.
To start using a JobScheduler, we first implement the job itself. To do so, implement the 
class android.app.job.JobService, as follows:
class MyJob : JobService() {
var jobThread:Thread? = null
override
fun onStartJob(params: JobParameters) : Boolean {
Log.i("LOG", "MyJob: onStartJob() : " +
params.jobId)
jobThread?.interrupt()
jobThread = Thread {
Log.i("LOG", "started job thread")
// do job work...
jobFinished(params, false) jobThread = null
Log.i("LOG", "finished job thread")
}
jobThread.start()
return true
}
override
fun onStopJob(params:JobParameters) : Boolean {
Log.i("LOG", "MyJob: onStopJob()")
jobThread?.interrupt()
Note Use JobSchedulers for background tasks if your target API level is 21 or higher (great than 
85 percent of Android installations as of February 2018).



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   111   112   113   114   115   116   117   118   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling