Android WorkManager PeriodicWorkRequest不';应用程序关闭时无法工作
我需要在应用程序关闭时创建通知,我尝试执行以下方法: 通知员:Android WorkManager PeriodicWorkRequest不';应用程序关闭时无法工作,android,android-notifications,android-workmanager,Android,Android Notifications,Android Workmanager,我需要在应用程序关闭时创建通知,我尝试执行以下方法: 通知员: class NotificationWorker(context: Context, params: WorkerParameters) : CoroutineWorker(context, params) { private lateinit var notificationManager: NotificationManager override suspend fun doWork(): Res
class NotificationWorker(context: Context, params: WorkerParameters) :
CoroutineWorker(context, params) {
private lateinit var notificationManager: NotificationManager
override suspend fun doWork(): Result {
initNotifications()
notificationManager.notify(0, createNotification())
return Result.success()
}
private fun initNotifications() {
notificationManager = getSystemService(
applicationContext,
NotificationManager::class.java
) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_LOW
val channel =
NotificationChannel(NOTIFICATION_CHANNEL, NOTIFICATION_CHANNEL, importance)
notificationManager.createNotificationChannel(channel)
}
}
private fun createNotification(): Notification {
val builder = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O)
Notification.Builder(applicationContext) else
Notification.Builder(applicationContext, NOTIFICATION_CHANNEL)
builder.apply {
setContentTitle("Current time")
setContentText("${Calendar.getInstance()}")
setSmallIcon(R.drawable.ic_launcher_foreground)
}
return builder.build()
}
companion object {
private const val NOTIFICATION_CHANNEL = "syncended.news"
private fun getWorkerRequest(): PeriodicWorkRequest =
PeriodicWorkRequestBuilder<NotificationWorker>(15, TimeUnit.MINUTES).build()
fun enqueueSelf(context: Context) {
WorkManager.getInstance(context).enqueueUniquePeriodicWork(
NotificationWorker::class.java.name,
ExistingPeriodicWorkPolicy.KEEP,
getWorkerRequest()
)
}
}
}
我在清单中创建了接收者和提供者:
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
android:exported="false"
tools:node="remove" />
<receiver
android:name=".notifications.NotificationsStartReceiver"
android:process=":newsBgNotify" />
一些中国OEM手机(如Oppo)在用户从“最近使用的应用”选项卡中刷下应用后,会关闭应用(停止后台服务)。这是主要原因。要防止这些设备这样做,请转到设置->应用程序设置->“YourApp”和“启用后台服务”一些中国OEM手机(如Oppo)在用户从“最近使用的应用程序”选项卡中刷下应用程序后关闭应用程序(停止后台服务)。这是主要原因。要阻止这些设备这样做,请转到设置->应用程序设置->“YourApp”和“启用后台服务”
<provider
android:name="androidx.work.impl.WorkManagerInitializer"
android:authorities="${applicationId}.workmanager-init"
android:exported="false"
tools:node="remove" />
<receiver
android:name=".notifications.NotificationsStartReceiver"
android:process=":newsBgNotify" />
2020-05-04 15:06:26.678 2046-2159/system_process W/InputDispatcher: channel '68c8279 ru.syncended.news/ru.syncended.news.main.MainActivity (server)' ~ Consumer closed input channel or an error occurred. events=0x9
2020-05-04 15:06:26.679 2046-2159/system_process E/InputDispatcher: channel '68c8279 ru.syncended.news/ru.syncended.news.main.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!