Android 通知 - if/else 部分都在运行

标签 android notifications multiple-instances

我正在使用数据包监听器在我的应用程序(xmpp 聊天)中实现通知。

在第一个通知到来之前,代码工作正常,但是一旦用户点击通知,那么在第一次点击之后,( if 和 else )的代码都用完了。我无法获得解决方案,它也很奇怪 IF/ELSE 代码如何运行 - 在第一次通知点击之后。

代码

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

如果有人可以,请帮助我! 谢谢

最佳答案

您的函数可能会在不同的条件下同时被调用不止一次(可能是两次)...所以您会有一种 if 和 else 被调用的感觉。尝试使用一个标志来限制代码块的执行次数或设置一个计数器来检查代码块是否被执行了多次。将您计划使用的计数器或标志变量设为静态,以便您可以检测到来自您可能在不知不觉中创建的不同实例的调用。

关于Android 通知 - if/else 部分都在运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13410426/

相关文章:

android - 房间删除​​查询不会删除数据库中的行

android - 使用方法扩展和继承时名称冲突

android:为 R.drawable.variableValue 传递的变量

java - Android - GCM 推送通知未出现在通知列表中

android - 为什么我的通知小图标总是灰色的?

java - Java中如何将变量从一个JFrame传递到多个JFrame?

java - 使用 TabHost 和 LocalActivityManager 时不调用 Android OnResume

javascript - NW.js 通知 onclick 不在 Windows 上执行

c++ - 重写一个类以使其更通用以实现可移植性

WCF 服务 - 在启动时创建多个实例?