how to get user online or offline in asmack, android [duplicate]

筅森魡賤 提交于 2019-12-04 02:48:50

You can get online and offline friends via RosterListener like i did below and then update listview with new data.

roster.addRosterListener(new RosterListener() {

            @Override
            public void presenceChanged(final Presence presence) {

                System.out.println("Presence changed: " + presence.getFrom()
                        + " " + presence);

                runOnUiThread(new Runnable() {
                    public void run() {

                        // / To Update listview  should clear arraylists first
                        // then inavalidate Listview to redraw then add new data
                        // to Arraylists then notify adapter.
                        JID.clear();
                        Names.clear();
                        Status.clear();
                        Image.clear();
                        list.invalidateViews();
                        for (RosterEntry entry : entries) {
                            card = new VCard();
                            presencek = roster.getPresence(entry.getUser());

                            try {
                                card.load(Main.conn, entry.getUser());
                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            JID.add(entry.getUser());
                            Names.add(card.getField("FN"));
                            Status.add(presencek.getType().name());
                            Log.d("Prescence", "" + presencek.getType().name());// //num
                                                                                // one
                                                                                // log

                            // if (bud.name == null)
                            // bud.name = bud.jid;
                            // buddies.add(bud);
                            byte[] img = card.getAvatar();
                            if (img != null) {
                                int len = img.length;
                                Image.add(BitmapFactory.decodeByteArray(img, 0,
                                        len));
                            } else {
                                Drawable d = getApplicationContext()
                                        .getResources().getDrawable(
                                                R.drawable.save);
                                Bitmap bitmap = ((BitmapDrawable) d)
                                        .getBitmap();
                                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                                bitmap.compress(Bitmap.CompressFormat.PNG, 100,
                                        stream);
                                img = stream.toByteArray();
                                int len = img.length;
                                Image.add(BitmapFactory.decodeByteArray(img, 0,
                                        len));
                                // Image.add(null);
                            }
                            // buddyList[i++] = bud;
                        }

                        adapter.notifyDataSetChanged();


                }       

            @Override
            public void entriesUpdated(Collection<String> addresses) {
                // TODO Auto-generated method stub

            }

            @Override
            public void entriesDeleted(Collection<String> addresses) {
                // TODO Auto-generated method stub

            }

            @Override
            public void entriesAdded(Collection<String> addresses) {
                // TODO Auto-generated method stub

            }
        });

There is a means to find the offline/online status of the user. It can be done by seeking the PRESENCE OF THE USER. Here is the code snippet :

//here is how you can get is ONLINE or OFFLINE

        Presence availability = roster.getPresence(user);

And here is the code to get its presence mode i.e if user is available then is he AWAY,DONOT DISTURB MODE or ONLINE For CHAT.

public int retrieveState_mode(Mode userMode, boolean isOnline) {
        int userState = 0;
        /** 0 for offline, 1 for online, 2 for away,3 for busy*/
        if(userMode == Mode.dnd) {
            userState = 3;
        } else if (userMode == Mode.away || userMode == Mode.xa) {

            userState = 2;
        } else if (isOnline) {
            userState = 1;
        }
        return userState;
    }

you can save this state in an array list as :-

mFriendsDataClass.friendState = retrieveState_mode(availability.getMode(),availability.isAvailable());

Please let me know if you have any queries regarding xmpp/smack in chat type application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!