1
votes

I am using Smack XMPP client for android for developing an IM Messenger. I want to do like this scenario When user login I want to attach PackerListener with XMPPConnection reference object in Background Service. xmppCon.addPacketListener (……) . This Packet Listener use to Listen incoming request (can be chat message,subscription request,group chat request etc).Now after getting this packet request I Identify the request type like it is chat message,subscription request so on etc.So if it is a chat message and my chat screen open I want to send / update that screen if it is subscription request I want to update my pending UI Activity screen so on depending on request I want to update specific UI from a service. Problem is that how I can update Activity(active activity) from service? Anyone can guide me how I can do this or can give me better suggestion for this ? I will be very thankful …

2
Perhaps you can get an idea from this about the way of using a background service with a listener: stackoverflow.com/a/14478281/5361779B378

2 Answers

0
votes

You can make a service class and after that in its on Start method you can add this code :-

RosterListener r1 = new RosterListener() {

                @Override
                public void presenceChanged(Presence presence) {
                    // TODO Auto-generated method stub

                    //sending the broadcast to update the expandable list view
                    //to check if any person's presence has changed.
                       sendBroadcast(new Intent(UserMenuActivity.ACTION_UPDATE));
notification("changed");

                }

                @Override
                public void entriesUpdated(Collection<String> arg0) {
                    // TODO Auto-generated method stub
                    //notification("entriesUpdated");
                }

                @Override
                public void entriesDeleted(Collection<String> arg0) {
                    // TODO Auto-generated method stub
                    //notification("entriesDeleted");
                }


                @Override
                public void entriesAdded(Collection<String> arg0) {
                    // TODO Auto-generated method stub
                    Iterator<String> it = arg0.iterator();
                    if (it.hasNext()) {
                        user = it.next();
                    }
                    /*RosterEntry entry = roster.getEntry(user);
                    if(entry.getType().toString().equalsIgnoreCase("to")){
                        int index_of_Alpha = user.indexOf("@");
                        String subID = user.substring(0, index_of_Alpha);

                        notification("Hi,"+subID+" wants to add you");
                    }       */      
                }
            };

            if (roster != null) {
                roster.setSubscriptionMode(Roster.SubscriptionMode.manual);
                System.out.println("subscription going on");
                roster.addRosterListener(r1);
            }

        } else {
            showToast("Connection lost-", 0);
        }

This is how you can do the same what you are asking. Please feel free to ask me any queries regarding the same.

-1
votes

The service that drives your XMPP connection could broadcast an Intent if there is a state change. Your UI Activity (or even an Widget) can then register for those Intents and update their display accordingly.