Android WorkManager-CoroutineWorker后台运行发送Notification通知到前台通知栏,kotlin(2)

275 篇文章 0 订阅

Android WorkManager-CoroutineWorker后台运行发送Notification通知到前台通知栏,kotlin(2)

 

package com.example.myapplication

import android.R
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationCompat.PRIORITY_MIN
import androidx.work.*


val TAG = "zhangphil"

fun notification(context: Context, s: String): Notification {
    var channelId = createNotificationChannel(context, "com.fly", "Foreground_Service") ?: ""
    return NotificationCompat.Builder(context, channelId)
        .setOngoing(true)
        .setSmallIcon(R.drawable.ic_notification_overlay)
        .setContentTitle("我的标题")
        .setContentText(s)
        .setPriority(PRIORITY_MIN)
        .setCategory(Notification.CATEGORY_SERVICE)
        .build()
}


private fun createNotificationChannel(
    context: Context,
    channelId: String,
    channelName: String
): String? {
    val channel = NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_NONE)
    channel.lightColor = Color.BLUE
    channel.lockscreenVisibility = Notification.VISIBILITY_PRIVATE
    val service = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    service?.createNotificationChannel(channel)

    return channelId
}


class ForegroundWorker(private val context: Context, parameters: WorkerParameters) :
    CoroutineWorker(context, parameters) {

    override suspend fun getForegroundInfo(): ForegroundInfo {
        return ForegroundInfo(1, notification(context, "zhangphil"))
    }

    override suspend fun doWork(): Result {
        setForegroundAsync(getForegroundInfo())

        repeat(1000) {
            Thread.sleep(1000)
            Log.d(TAG, "do work " + it)
        }

        return Result.success()
    }
}


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val request = OneTimeWorkRequestBuilder<ForegroundWorker>()
            .setExpedited(OutOfQuotaPolicy.DROP_WORK_REQUEST)
            .build()

        WorkManager.getInstance(applicationContext)
            .enqueue(request)
    }

    override fun onStart() {
        super.onStart()
        println("前台")
    }

    override fun onStop() {
        super.onStop()
        println("后台")
    }
}

 

配置权限:

<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

 

记得在手机的应用列表设置运行这个app允许通知。

 

android现代化后台任务WorkManager,kotlin(1)_zhangphil的博客-CSDN博客Android可保活的、不依附于应用进程存活的后台任务框架:WorkManagerWorkManager旨在实现不依附与App进程的后台线程化任务。和JobSchedule 相似,是JobSchedule 的改进。WorkManager可实现:一次性执行任务,链式执行任务,周期性执行任务,一定条件下(网络,电量)执行任务,设备重启后执行上次未完成任务等。Android可保活的、不依附于应用进程存活的后台任务框架:WorkManager_workmanager 保活_zhangphil的博客-CSDN博客。https://blog.csdn.net/zhangphil/article/details/129379527

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值