简体   繁体   English

当startforeground多次调用时,通知remoteview重复

[英]Notification remoteview duplicates when startforeground called more than once

In a service I have a main remoteview 在服务中,我有一个主要的远程视图

notRemoteView = new RemoteViews(context.getPackageName(), R.layout.notification_layout);

and to that I add another remoteview which contains an ImageButton 然后我添加另一个包含一个ImageButton的remoteview

RemoteViews btnView1 = new RemoteViews(context.getPackageName(), R.layout.btn1);
notRemoteView.addView(R.id.image_button_container, btnView1);

after that I call: 之后,我打电话给:

startForeground(requestCode, notification);

It all works fine and does the appropriate action when clicked but then the ImageButton gets duplicated. 一切正常,单击时可以执行适当的操作,但是ImageButton会重复。 The btnView1 remoteview seems to get re-added to the main remoteview. btnView1 remoteview似乎已重新添加到主remoteview中。 With each button click another button is added to the notification. 使用每个按钮单击,另一个按钮将添加到通知中。 This happens even when I null both of the remotveiws before building the notification. 即使在构建通知之前我将两个远程对象都设为空时,也会发生这种情况。

I used to have the ImageButton as part of the main remoteview originally and it worked perfectly fine. 我以前曾经将ImageButton用作主remoteview的一部分,并且效果很好。 Note: I use startForeground(requestCode, notification); 注意:我使用startForeground(requestCode, notification); with the same requestCode, is this the right way to update service/background notification 使用相同的requestCode,这是更新服务/后台通知的正确方法

I figured it out, I needed to remove all remoteviews each time I wanted to update notification: 我知道了,每次我想更新通知时都需要删除所有remoteviews:

notRemoteView = new RemoteViews(context.getPackageName(), R.layout.notification_layout);
notRemoteView.removeAllViews(R.id.image_button_container);

Thanks to this answer: Android app widget: content added twice 感谢此答案: Android应用程序小部件:内容添加了两次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM