我在android中的通知中遇到问题,每当我单击通知时,我都必须再次调用相同的活动,但据我认为新的活动被调用,但前一个活动也在后端运行,因为我的代码一次又一次地运行(因为有多个实例)
请帮助我如何在每次单击通知时解决或关闭多个实例。
code
公共无效notificationforChat(CharSequence消息,字符串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);
}
thanks
将该活动的以下代码放入 maifest 中:
android:launchMode="singleTop"
然后,为了在再次调用活动时处理新的更改,请重写以下方法。
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//Do your new intent handling here.
}
您放入清单中的代码可确保仅创建该活动的一个实例。您可以在该活动的 onNewIntent 重写方法中处理新意图所需的更改。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)