Arsenal de Android: procesamiento en segundo plano

Una biblioteca de kotlin work manager para Android que incluye notificaciones y soporte para Hilt.

Instrucciones para el usuario

  1. Agregue el repositorio de JitPack al build.gradle de su proyecto
allprojects {
 repositories {
  ...
  maven { url 'https://jitpack.io' }
 }
}
  1. Agregue los complementos kapt y hilt a la parte superior del archivo build.gradle de su aplicación
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
  1. Agregue dependencias para los cordones de las botas y la empuñadura al archivo build.gradle de su aplicación
dependencies {
        implementation 'com.github.evilthreads669966:bootlaces:8.4'
        implementation "com.google.dagger:hilt-android:2.29.1-alpha"
        kapt "com.google.dagger:hilt-android-compiler:2.29.1-alpha"
}
  1. Agregue el complemento Hilt a las dependencias build.gradle de su proyecto
dependencies {
    ...
    classpath "com.google.dagger:hilt-android-gradle-plugin:$hilt_version"
}
  1. Tenga en cuenta su subclase de la clase de aplicación
@HiltAndroidApp
class App: Application()
  1. Agrega el nombre de la subclase de tu aplicación para manifestar
<application
    android:name=".App"
    ...
>
  1. Crea tus trabajadores. El parámetro de descripción se utilizará para la notificación. Puede crear un receptor de transmisión para su trabajador ignorando Recibir una acción para el trabajador.
class MyWorker: Worker(66,"Something evil") {
    override val receiver: WorkReceiver?
        get() = null

    override suspend fun doWork(ctx: Context) {
        for(i in 1..10)
            delay(1000)
    }
}

//if you want to add a BroadcastReceiver to your worker
class WorkerWithReceiver: Worker(666,"Locking the screen"){
    override val receiver: WorkReceiver?
        get() = object : WorkReceiver(Intent.ACTION_CLOSE_SYSTEM_DIALOGS) {
            override fun onReceive(ctx: Context?, intent: Intent?) {
                //do something
            }
        }

    override suspend fun doWork(ctx: Context) {
        //do work
    }
}
//worker with a progress notification
class MyProgressWorker: Worker(66,"Working while displaying a notification for progress", withNotification = true) {
    override val receiver: WorkReceiver?
        get() = null

    override suspend fun doWork(ctx: Context) {
        for(i in 1..10)
            delay(1000)
    }
}
  1. Inyecte su WorkScheduler en un contexto de Android
@Inject lateinit var scheduler: WorkScheduler
  1. Programe su trabajador
//persistent worker
scheduler.schedulePersistent(WorkerWithReceiver())

//one time worker
scheduler.scheduleOneTime(MyWorker())

//one time worker with notificaton for displaying progress. This works for all scheduler methods
scheduler.scheduleOneTime(MyProgressWorker())

//periodic worker
scheduler.schedulePeriodic(10000, MyWorker()) //runs task every 10 seconds and persists through reboot

//future worker
scheduler.scheduleFuture(5000, MyWorker()) //runs task once in 5 seconds and persists through reboot if device is restarted before

//hourly worker
scheduler.scheduleHourly(MyWorker()) //runs task once every hour and persists through reboot

//daily worker
scheduler.scheduleDaily(MyWorker()) //runs task once every day and persists through reboot

//weekly worker
scheduler.scheduleWeekly(MyWorker()) //runs task once every week (7 days) and persists through reboot

//monthly worker
scheduler.scheduleMonthly(MyWorker()) //runs task once every month and persists through reboot

//yearly worker
scheduler.scheduleYearly(MyWorker()) //runs task once every year and persists through reboot

Importante saber

Licencia

Copyright 2019 Chris Basinger

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

.

Compruebe también

El arsenal de Android: la cámara

Ser permitido cámara casera Seleccione Foto Enviar resultado Instalar Soporta API 21 y posteriores Paso …

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *