在Android应用中使用WorkManager处理后台工作

一、         实验名称

 在Android应用中使用WorkManager处理后台工作。

二、         参考资料

应用架构:数据层 - 使用 WorkManager 调度任务 - Android 开发者  |  Android Developers (google.cn)、第九、十章课件。

三、         实验目的

练习在Android应用中使用WorkManager处理后台工作。

四、         实验内容

本实验在Blur-O-Matic应用初始项目的基础上完成。该应用可对照片进行模糊处理,并将处理后的照片保存到文件中,如下图所示:

image.png

本实验步骤如下:

        参照第十章课件,使用WorkManager在后台对照片进行模糊处理并能够在应用的界面中查看处理后的图片文件。

        课件上的内容只实现了对固定的图片进行模糊处理,参考上一章的内容,完成从相册中任取一张图片进行模糊处理并能够在应用的界面中查看处理后的图片文件。

按照实验任务书的要求完成以下实验报告:

一、程序代码

 1.在workers包下新建BlurWorker.kt文件

package com.example.bluromatic.workers

private const val TAG = "BlurWorker"
class BlurWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx, params){
   override suspend fun doWork(): Result {
       // ADD THESE LINES(更新 BlurWorker 类中的 doWork() 方法,以获取输入数据对象传入的 URI 和模糊处理级别)
       val resourceUri = inputData.getString(KEY_IMAGE_URI)
       val blurLevel = inputData.getInt(KEY_BLUR_LEVEL,1)

       //makeStatusNotification(
       //            applicationContext.resources.getString(R.string.blurring_image),
       //            applicationContext
       //        )

       return withContext(Dispatchers.IO) {
           return@withContext try {
               //NEW code(检查是否已填充 resourceUri 变量,如果未填充,代码应抛出异常)
               require(!resourceUri.isNullOrBlank()){
                   val errorMessage =
                       applicationContext.resources.getString(R.string.invalid_input_uri)
                   Log.e(TAG,errorMessage)
                   errorMessage
               }
               //由于图片来源是作为 URI 传入的,因此我们需要一个 ContentResolver 对象来读取该 URI 所指向的内容
               val resolver = applicationContext.contentResolver

               delay(DELAY_TIME_MILLIS)
               //val picture = BitmapFactory.decodeResource(
               //                    applicationContext.resources,
               //                    R.drawable.android_cupcake
               //                )
               val picture = BitmapFactory.decodeStream(
                   resolver.openInputStream(Uri.parse(resourceUri))
               )
               //val output = blurBitmap(picture, 1)
               val output = blurBitmap(picture, blurLevel)

               val outputUri = writeBitmapToFile(applicationContext, output)
               makeStatusNotification(
                   "Output is $outputUri",
                   applicationContext
               )
               val outputData = workDataOf(KEY_IMAGE_URI to outputUri.toString())
               //Result.success()
               Result.success(outputData)
           } catch (throwable: Throwable) {
               Log.e(
                   TAG,
                   applicationContext.resources.getString(R.string.error_applying_blur),
                   throwable
               )
               Result.failure()
           }
       }
   }
}

2. 在workers包下新建CleanupWorker.kt文件

package com.example.bluromatic.workers

private const val TAG = "CleanupWorker"
class CleanupWorker(ctx: Context, params: WorkerParameters) : CoroutineWorker(ctx, params) {
   override suspend fun doWork(): Result {
       makeStatusNotification(
           applicationContext.resources.getString(R.string.cleaning_up_files),
           applicationContext
       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值