Not able to get LastActivity of a jabberId

2019-04-13 16:05发布

问题:

I am trying to get last offline time of a particular jabberId by this piece of code.

LastActivityManager lActivityManager = LastActivityManager.getInstanceFor(MessageService.getConnection());
        lActivityManager.isLastActivitySupported(to + "@localhost");

        Log.v(TAG, "Last Activity: " + lActivityManager.getLastActivity(to + "@localhost"));
        LastActivity activity = lActivityManager.getLastActivity(to + "@localhost");

But I am keep getting service-unavailable exception. I have checked the server configuration but this feature is implemented correctly from server side.

回答1:

Have you added other jabber id as your roster. If not try adding them then check. If you are using ejabbered then you can manually add roster from their admin panel for testing. I used this code snippet to add roster--

Roster.setDefaultSubscriptionMode(Roster.SubscriptionMode.accept_all);

                    String number = datacollection.get(i).getNo().replace("+", "");
                    String jid = number + "@localhost";

                    Collection<RosterEntry> entries = roster.getEntries();
                    for (RosterEntry entry : entries)
                    {
                        System.out.println(entry);
                        if (entry.getUser().equals(jid))
                        {
                            rosterAlreadyAdded = true;
                            if (entry.getType() != RosterPacket.ItemType.both)
                            {
                                // Create a presence subscription packet and send.
                                Presence presencePacket = new Presence(Presence.Type.subscribe);
                                presencePacket.setTo(jid);
                                connection.sendStanza(presencePacket);
                            }

                        }

                    }
                    if (!rosterAlreadyAdded)
                        roster.createEntry(jid, jid, null);