gpt4 book ai didi

安卓多实例

转载 作者:太空狗 更新时间:2023-10-29 12:49:50 26 4
gpt4 key购买 nike

我在 android 中遇到通知问题,每当我每次单击通知时,我都必须再次调用相同的 Activity ,但据我所知,新 Activity 已被调用,但前一个 Activity 也在后端运行,因此我的代码一次又一次地运行(因为多个实例)

请帮助我如何在每次单击通知时解决或关闭多个实例。

代码

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);
}

谢谢

最佳答案

将该 Activity 的以下代码放入 maifest 中:

android:launchMode="singleTop"

然后为了在再次调用 Activity 时处理新的更改,覆盖以下方法。

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//Do your new intent handling here.
}

您放入 list 中的代码确保只创建该 Activity 的一个实例。您可以在该 Activity 的 onNewIntent 覆盖方法中处理新 Intent 所需的更改。

关于安卓多实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13411173/

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