【发布时间】:2016-03-03 20:13:27
【问题描述】:
我在android中的通知有问题,每当我每次点击通知时,我都必须再次调用相同的活动,但据我认为新活动被调用但前一个也在后端运行,因此我的代码一次又一次地运行(因为多个实例)
请帮助我每次点击通知时如何解决或关闭多个实例。
代码
public void notificationforChat(CharSequence message,String toJid, int notificationID) {
int notificationCount = 1;
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
int icon = R.drawable.ic_launcher;
CharSequence tickerText = message;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
//notification.number = notificationCount++;
Context context = getApplicationContext();
CharSequence contentTitle = "Chat";
CharSequence contentText = message;
Intent notificationIntentforChat = new Intent(this, UserChatActivity.class);
notificationIntentforChat.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
notificationIntentforChat.putExtra("userNameVal", toJid);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntentforChat, PendingIntent.FLAG_UPDATE_CURRENT);
notification.setLatestEventInfo(context, contentTitle, contentText,
contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notification.defaults = Notification.DEFAULT_ALL;
mNotificationManager.notify(notificationID, notification);
}
谢谢
【问题讨论】:
-
您是指仅在您的应用中的单个活动?
-
是的。假设您在您的 gtalk 聊天屏幕上,正在与其他人聊天,现在如果另一个人 ping 您,那么您将通过通知收到通知,现在当您单击该通知时,其他人的聊天屏幕将打开。现在这里我前一个人的聊天画面也在后台运行
标签: android notifications multiple-instances