XMPP aSmack - How can I get the current user state (offline/online/away/etc.)?

前端 未结 5 2133
自闭症患者
自闭症患者 2020-12-08 15:54

I am new to xmpp/asmack in android.

Can anyone please help me in getting the presence of the user\'s friends ( roster list)

I am using this :



        
5条回答
  •  醉话见心
    2020-12-08 16:26

    ConnectToServer(){
        final ProgressDialog dialog = ProgressDialog.show(ChatWindowFragmentActivity.this,
                "Connecting...", "Please wait...", false);
        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
    
    
                // Object of XmppClient class
                XmppClient mXmppClient = new XmppClient();
    
                /*
                 * // Create a connection ConnectionConfiguration connConfig =
                 * new ConnectionConfiguration(HOST, PORT);
                 */
                XMPPConnection connection = null;
    
                try {
                    SmackAndroid.init(ChatWindowFragmentActivity.this);
                    connection = mXmppClient.connectionToXmppServer();
                } catch (XMPPException e) {
                    // TODO Auto-generated catch block
          //                    setConnection(null, null);
                }
                try {
                    mXmppClient.loginUser(connection, USERNAME, PASSWORD);
                    Log.i("XMPPChatDemoActivity",
                            "Logged in as" + connection.getUser());
                    // Set the status to available
                    Presence presence = new Presence(Presence.Type.available);
                    connection.sendPacket(presence);
    
                    setConnection(connection);
    
                    Roster roster = connection.getRoster();
    
                     /*
                    Fetch USER availability
                    */
    
    
                    switch (isUserAvailable(connection)){
                        case 0:
                            imgAvailability.setBackgroundColor(Color.GRAY);
                            break;
                        case 1:
                            imgAvailability.setBackgroundColor(Color.GREEN);
                            break;
                        case 2:
                            imgAvailability.setBackgroundColor(Color.YELLOW);
                            break;
                        case 3:
                            imgAvailability.setBackgroundColor(Color.RED);
                            break;
                        default:
                            break;
                    }
    
    
                    Collection entries = roster.getEntries();
                    for (RosterEntry entry : entries) {
    
                        Log.d("XMPPChatDemoActivity",
                                "--------------------------------------");
                        Log.d("XMPPChatDemoActivity", "RosterEntry " + entry);
                        Log.d("XMPPChatDemoActivity",
                                "User: " + entry.getUser());
                        Log.d("XMPPChatDemoActivity",
                                "Name: " + entry.getName());
                        Log.d("XMPPChatDemoActivity",
                                "Status: " + entry.getStatus());
                        Log.d("XMPPChatDemoActivity",
                                "Type: " + entry.getType());
                        Presence entryPresence = roster.getPresence(entry
                                .getUser());
    
                        Log.d("XMPPChatDemoActivity", "Presence Status: "
                                + entryPresence.getStatus());
                        Log.d("XMPPChatDemoActivity", "Presence Type: "
                                + entryPresence.getType());
    
                        Presence.Type type = entryPresence.getType();
                        if (type == Presence.Type.available)
                            Log.d("XMPPChatDemoActivity", "Presence AVAILABLE");
                        Log.d("XMPPChatDemoActivity", "Presence : "
                                + entryPresence);
                    }
    
    
    
                } catch (XMPPException e) {
    
                    e.printStackTrace();
                    Log.e("XMPPChatDemoActivity", "Failed to log in as "
                            + USERNAME);
                    Log.e("XMPPChatDemoActivity", e.toString());
                    new ShowAlert(ChatWindowFragmentActivity.this,e.getMessage(), false).show(
                            getSupportFragmentManager(), TAG);
       //                    setConnection(null, null);
                }
    
                dialog.dismiss();
            }
        });
        t.start();
        dialog.show();
    }
    

    And your method called inside it.

提交回复
热议问题