Pro Android with Kotlin
Download 5.42 Mb. Pdf ko'rish
|
@de android telegram Pro Android with Kotlin Developing Modern Mobile
134
CHAPTER 8: APIs To use the Firebase JobDispatcher, it first must be installed. To do so, add the following to your module’s build.gradle file, in the dependencies section: implementation 'com.firebase:firebase-jobdispatcher:0.8.5' As a first step, implement a job class as follows: import com.firebase.jobdispatcher.* class MyJobService : JobService() { var jobThread:Thread? = null override fun onStopJob(job: JobParameters?): Boolean { Log.e("LOG", "onStopJob()") jobThread?.interrupt() jobThread = null return false // this job should not be retried } override fun onStartJob(job: JobParameters): Boolean { Log.e("LOG", "onStartJob()") jobThread?.interrupt() jobThread = Thread { Log.i("LOG", "started job thread") // do job work... jobFinished(job, false) // instead use true to signal a retry jobThread = null Log.i("LOG", "finished job thread") } jobThread?.start() return true // work is going on in the background } } Then register the job in the manifest file AndroidManifest.xml as follows: android:name=".MyJobService"> /> 135 CHAPTER 8: APIs To include a check for availability, you have to perform the following steps: 1. Google Play Services needs to be added to the SDK installation. Inside Android Studio, go to Tools ➤ Android ➤ SDK Manager. In the menu choose Appearance & Behavior ➤ System Settings ➤ Android SDK. On the SDK Tools tab, select Google Play Services and then click the OK button. 2. Right-click the project, choose Open Module Settings, and in the menu select your app module. Go to the Dependencies tab, and add the library com.google.android.gms:play-services by clicking the + button. To actually schedule a job from in your app, you can acquire the service, create a job, and then register this job by using this: val gps = GoogleApiAvailability.getInstance(). isGooglePlayServicesAvailable(this) if(gps == ConnectionResult.SUCCESS) { // Create a new dispatcher using the Google Play // driver. val dispatcher = FirebaseJobDispatcher( GooglePlayDriver(this)) val myJob = dispatcher.newJobBuilder() .setService(MyJobService::class.java) // the JobService that will be called .setTag("my-unique-tag") // uniquely identifies the job .build() dispatcher.mustSchedule(myJob) } else { Log.e("LOG", "GooglePlayServices not available: " + GoogleApiAvailability.getInstance(). getErrorString(gps)) } This example scheduled a job with basic job scheduling characteristics. For more complex needs, the job builder allows for more options, as shown in Table 8-3 . Just chain them before the .build() method. |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling