Android通知-if/else部分都在运行
我正在我的应用程序(xmpp聊天)中使用数据包侦听器实现一个通知 在第一次通知出现之前,代码工作正常,但只要用户单击通知,在第一次单击之后(if和else)的代码都将用完。我无法获得解决方案,奇怪的是IF/ELSE代码是如何运行的——在第一次通知单击之后 代码 如果有人能帮助我,请帮助我!Android通知-if/else部分都在运行,android,notifications,multiple-instances,Android,Notifications,Multiple Instances,我正在我的应用程序(xmpp聊天)中使用数据包侦听器实现一个通知 在第一次通知出现之前,代码工作正常,但只要用户单击通知,在第一次单击之后(if和else)的代码都将用完。我无法获得解决方案,奇怪的是IF/ELSE代码是如何运行的——在第一次通知单击之后 代码 如果有人能帮助我,请帮助我! 谢谢在不同的条件下,您的函数可能会在同一时间被调用不止一次(可能是两次)…因此您会有被调用if和else的感觉。尝试使用标志限制代码块的执行次数,或设置计数器检查代码块是否执行多次。将您计划使用的计数器或标志
谢谢在不同的条件下,您的函数可能会在同一时间被调用不止一次(可能是两次)…因此您会有被调用if和else的感觉。尝试使用标志限制代码块的执行次数,或设置计数器检查代码块是否执行多次。将您计划使用的计数器或标志变量设为静态变量,这样您就可以检测到来自不同实例的调用,而这些实例可能是您在不知不觉中创建的。您的函数可能会在不同的条件下同时被调用多次(可能是两次)…因此您将感受到if和else被调用的感觉。尝试使用标志限制代码块的执行次数,或设置计数器检查代码块是否执行多次。将您计划使用的计数器或标志变量设为静态变量,这样您就可以检测到来自不同实例的调用,而这些实例可能是您无意中创建的。对不起,noob/lazy问题-两个if/else中的哪一个同时运行?您对此有日志吗?我认为在单击通知时会创建多个实例,如果/else具有NotificationOfOrchat代码的实例同时运行,但在调试它时,它不会同时输入这两个实例。因此,似乎正在创建多个实例,这使得它看起来就像两个实例同时运行一样。很抱歉,noob/lazy问题-两个if/else中的哪一个同时运行?您对此有日志吗?我认为在单击通知时会创建多个实例,如果/else具有NotificationOfOrchat代码的实例同时运行,但在调试它时,它不会同时输入这两个实例。因此,似乎正在创建多个实例,这使得它看起来就像两个实例同时运行一样。是的,我已经检查了我的函数运行了两次,它正在两次侦听相同的数据包:(现在我遇到了一个问题,为什么这些数据包要接收多次(第一次单击后)这是主要的prblm。多个实例正在形成。请帮助我。这是多个实例形成的问题,当我单击NotifCaion时。我没有找到soln yetI。我已经在你发布的新问题中为同一个问题提出了解决方案。是的,我检查了我的函数运行了两次,它正在两次侦听相同的数据包:(现在我遇到了一个问题,为什么这些数据包会多次接收(在第一次单击之后)这是主要的prblm。多个实例正在形成。请帮助我。这是多个实例形成的问题,当我单击NotifCaion时。我没有找到soln yetI。我已经在您发布的新问题中为同一个问题提出了解决方案。
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
final PacketCollector collector = connection.createPacketCollector(filter);
connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packetChat) {
Message message = (Message) packetChat;
packetChat = collector.nextResult();
if (message.getBody() != null) {
fromName = StringUtils.parseBareAddress(message
.getFrom());
Log.i("XMPPClient", "Got text [" + message.getBody()
+ "] from [" + fromName + "]");
// messages.add(fromName + ":");
mMessageItem = new MessageItemDataClass();
mMessageItem.isSendMessage = false;
mMessageItem.messageText = message.getBody();
messages.add(mMessageItem);
//to add the packet message in the layout only when the user is on the same friend's screen
//for anonymous chat
if(packetChat.getFrom().equalsIgnoreCase(refineFromjId(frienduserID)+"/Smack")){
messages.add(mMessageItem);
}
else{
if(checkPresence=true){
notificationforChat(fromName.substring(0,fromName.indexOf("@"))+":1212 "+message.getBody(),packetChat.getFrom(),0);
}
}
//messages.add(mMessageItem);
// Add the incoming message to the list view
mHandler.post(new Runnable() {
public void run() {
//simply turn around to the last of the screen
mList.setSelection(mList.getCount());
lastIndex = mList.getLastVisiblePosition()+1;
if (mList.getFirstVisiblePosition() > lastIndex || mList.getLastVisiblePosition() <= lastIndex) {
//mList.smoothScrollToPosition(lastIndex);
customAdapter.notifyDataSetChanged();
mList.setSelection(mList.getCount());
dbhHelper.close();
}else{
mList.setSelection(mList.getCount());
customAdapter.notifyDataSetChanged();
dbhHelper.close();
}
}
});
}
}
}, filter);
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.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);
}