gpt4 book ai didi

android - ListenableWorker 不移除通知图标

转载 作者:行者123 更新时间:2023-12-04 11:10:00 25 4
gpt4 key购买 nike

我正在使用 ListenableWorker 来执行后台任务。
另外我希望操作系统知道我的服务重要性,所以我调用

 setForegroundAsync(new ForegroundInfo(WorkerConstants.NOTIFICATION_FOREGROUND_ID,
builder.build()));

正如谷歌文档中所建议的那样。

但是当我的服务停止或取消时,我仍然可以看到前台服务通知,我无法删除它。

此外,我在此通知中添加了取消按钮,但它什么也没做,我无法关闭它:
PendingIntent intent = WorkManager.getInstance(getApplicationContext())
.createCancelPendingIntent(getId());
builder.addAction(R.drawable.ic_redesign_exit,"Stop",intent);

有什么建议么?

最佳答案

我用这个向谷歌跟踪器提交了一个问题。并发现那是完全破旧的。

官方回复如下:

Looking at your sample app, you are introducing a race between completion of your ListenableFuture returned by CallbackToFutureAdaptor, and the main looper.

Even without the use of REPLACE one of your Notification updates is posted after your Worker is marked as successful.

However, you can actually avoid the race condition by waiting for the call to setForegroundAsync() to be completed (because that also returns a ListenableFuture). Once you do that - you never have a notification that shows up after your Worker is done.

I will also go ahead and file an improvement on how we can automatically do this even when developers get this wrong.


try {
// This ensures that you waiting for the Notification update to be done.
setForegroundAsync(createForegroundInfo(0)).get();
} catch (Throwable throwable) {
// Handle this exception gracefully
Log.e("FgLW", "Something bad happened", throwable);
}

关于android - ListenableWorker 不移除通知图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60541280/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com