安卓文档展示了如何以编程方式安排一个WorkManager Worker,就像这样:
val uploadWorkRequest = OneTimeWorkRequestBuilder<UploadWorker>()
.build()
WorkManager.getInstance().enqueue(uploadWorkRequest)
但是当用户点击通知操作时,您应该如何安排计划呢?
例如,以下是在用户单击通知操作时启动 IntentService 的代码:
class ApiCallService : IntentService("ApiCallService") {
// ...
}
val notificationBuilder = NotificationCompat.Builder(context,
NOTIFICATION_CHANNEL_ID)
val saveIntent = Intent(context, ApiCallService::class.java)
val savePendingIntent = PendingIntent.getBroadcast(context,
0, saveIntent, 0)
notificationBuilder.addAction(R.drawable.ic_done_white_24dp,
context.getString(R.string.save),
savePendingIntent)
但是,当用户单击相同的通知操作时,您要如何将WorkManager Worker排队,而不是使用IntentService?
IntentService
中调用WorkManager
工作。因为没有PendingIntent
,无法完成通知操作! - Nick Bapu