Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6081895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:13:39+00:00 2026-05-23T11:13:39+00:00

I found this question: XMPP events on Android , and the answer explains a

  • 0

I found this question: XMPP events on Android, and the answer explains a solution to the problem I am having. But I cannot seem to figure out how to get the packetListener to work. I have tried packetCollectors and packetListeners but noting seems to work.

I am unsure of where I should place the packet listeners. Should it go into the onCreate() method of the service, or in the onStartCommand(), or should it be put into a separate method that runs every few seconds?

My confusion is mostly with the concept of listeners. Is the listener always running, and will trigger as soon as a packet is received? And how do I make sure that the listener can ‘see’ the packet events coming in?

This is my current attempt at getting this to work:

public class XMPPService extends Service{

    private static String TAG = "XMPPService";
    XMPPConnection connection;
    MultiUserChat muc;      

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate(){
        super.onCreate();
    }

    public int onStartCommand(Intent intent, int flags, int startId){

        ConnectionConfiguration config = new ConnectionConfiguration("jabber.org", 5222);
        connection = new XMPPConnection(config);

        try 
        {
            connection.connect();
            Log.i(TAG,"connected");
        }
        catch (XMPPException e) 
        {
            Log.e(TAG, "Connection Issue: ", e);
        }

        try 
        {
            connection.login("user", "password");

            muc = new MultiUserChat(connection, "room@conference.jabber.org");
            muc.join("nick","password");    

            Presence presence = new Presence(Presence.Type.available);
            connection.sendPacket(presence);
            setConnection(connection);
        } 
        catch (XMPPException e) 
        {
            Log.e(TAG, "Connection Issue: ", e);
        }

        makeNotification("started");

        return 1;
    }

    private void makeNotification(String msg){
        Notification notification = new Notification(R.drawable.icon, msg, System.currentTimeMillis());
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, MUCTestActivity.class), 2);
        notification.setLatestEventInfo(this, "Title", msg, contentIntent);

        NotificationManager nmgr = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
        nmgr.notify(123443, notification);
    }


    public void setConnection(XMPPConnection connection) {
        this.connection = connection;
        if (connection != null) {
            // Add a packet listener to get messages sent to us
            PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
            connection.addPacketListener(new PacketListener() {
                public void processPacket(Packet packet) {
                    Message message = (Message) packet;
                    if (message.getBody() != null) {
                        String fromName = StringUtils.parseBareAddress(message.getFrom());
                        Log.i(TAG, "Got text [" + message.getBody() + "] from [" + fromName + "]");
                    }
                    else
                    {
                        Log.i(TAG,"null");
                    }
                }
            }, filter);
        }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T11:13:40+00:00Added an answer on May 23, 2026 at 11:13 am

    The PacketListener part looks right. It will be attached to smacks connection (the reader thread) and invoked by the connection if a packet, which matches the filter, arrives. You got the concept right.
    Returning START_STICKY (please use here the constant and not the value) should also help to keep the service and therefore the connection alive.

    Do you see the JID of the connection online? I am not sure if one can always send messages to a JID without being in the roster of the JID. Try enabling debugging in smack: Connection.DEBUG_ENABLED=true. If you have asmack, all debug messages will go into DBMS log. Also the emulator and eclipse with breakpoints are powerful tools to help you analyze the problem.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I searched and found this question but did not like the answer. Is there
So I found this question on here, but I'm having an issue with the
Found this question but there is no proper answer, so asking again. How do
I found this question that had been answered, but it didn't solve my problem:
I found this question , which has an answer, but facebook changed the token
I've found this question , which doesn't really solve my problem but at least
I found this question that is discussing what I would like to do, but
I found this question but it was only similar and, more importantly, dated by
I found this question, but the question is about how to get an image
I've already found this question on SO (which is exactly the same problem I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.