1

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 …

Sheraz Ahmad Khilji
  • 8,300
  • 9
  • 52
  • 84
aftab
  • 1,141
  • 8
  • 21
  • 40
  • Perhaps you can get an idea from this about the way of using a background service with a listener: https://stackoverflow.com/a/14478281/5361779 – B378 Apr 06 '18 at 03:27

2 Answers2

0

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.

Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143
  • HI if i wrote chat manager listener for listening the incoming messages like this in service. for the first message only listener is fried after that it is not triggering. please give me some suggestion to listen the xmpp events every time. @Long miles to Go.. – Roster Aug 20 '13 at 05:15
-1

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.

Flow
  • 23,572
  • 15
  • 99
  • 156