prevent通知PendingIntent启动活动已经开始了吗?开始了、通知、prevent、PendingIntent

2023-09-05 02:43:05 作者:△我是疯子,但疯的真实

该服务创建一个持久通知并通过 PendingIntent 开始对点击的主要活动。这里是code。

The service creates a persistent Notification and starts the main activity on click via PendingIntent. Here is the code.

Intent notificationIntent = new Intent(getApplicationContext(), ViewPagerActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(StreamingService.this, 0, notificationIntent, 0);

但是,如果的主要活动启动当我preSS通知,则将启动再次,然后再和......

However, if the main activity is started when I press notification, it will be started again, and again and...

在最后,我也有同样的活性起球了,一个在另一个上面。我可以用pressing后退按钮,这将杀死的主要活动一次,然后让我回到相同的活动,直到我结束最后一个看到这一点。

In the end, I have the same activity pilled up, one on the top of another. I can see this by pressing the back button, which will kill the Main activity once and then get me back to the same activity until I close the last one.

我怎么能prevent这种情况发生?可 PendingIntent 检测瞄准活动运行,因此它不会再次创建相同的活动,而是开始运行的一个?

How can I prevent this to happen? Can PendingIntent detect that aiming activity is running so it does not create the same activity again, but rather start the running one?

PS。我道歉,如果不能够说明这一点。如果是这样的话,让我知道,我会重组的问题。

PS. I apologize if not being able to explain this well. If this is the case, let me know and I will rephrase the problem.

推荐答案

根据要实现的具体行为,你可以通过这些标志之一,因为getActivity()的最后一个参数:

Depending on the exact behaviour you want to implement, you could pass one of these flags as the last param of getActivity():

FLAG_ACTIVITY_REORDER_TO_FRONT FLAG_ACTIVITY_SINGLE_TOP FLAG_ACTIVITY_REORDER_TO_FRONT FLAG_ACTIVITY_SINGLE_TOP