Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

以 XMPP Smack、Openfire 開發 IM:分組、好友與頭像操作

編輯:關於 Android 程式設計

在 Android 上打造即時通訊(IM)時,Smack 搭配 Openfire 是很常見的組合。其中「好友名冊(Roster)」是整個通訊錄的核心,分組、好友、頭像等操作大多繞著它打轉。以下整理幾個常用的操作範例。

一、查詢所有分組

透過 Roster(可由 connection.getRoster() 取得)就能拿到所有分組:

public static List<RosterGroup> getGroups(Roster roster) {
    List<RosterGroup> grouplist = new ArrayList<RosterGroup>();
    Collection<RosterGroup> rosterGroup = roster.getGroups();
    Iterator<RosterGroup> i = rosterGroup.iterator();
    while (i.hasNext()) {
        grouplist.add(i.next());
    }
    return grouplist;
}

二、新增分組

public static boolean addGroup(Roster roster, String groupName) {
    try {
        roster.createGroup(groupName);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

三、查詢某個分組內的所有好友

public static List<RosterEntry> getEntriesByGroup(Roster roster, String groupName) {
    List<RosterEntry> Entrieslist = new ArrayList<RosterEntry>();
    RosterGroup rosterGroup = roster.getGroup(groupName);
    Collection<RosterEntry> rosterEntry = rosterGroup.getEntries();
    Iterator<RosterEntry> i = rosterEntry.iterator();
    while (i.hasNext()) {
        Entrieslist.add(i.next());
    }
    return Entrieslist;
}

四、查詢所有好友

public static List<RosterEntry> getAllEntries(Roster roster) {
    List<RosterEntry> Entrieslist = new ArrayList<RosterEntry>();
    Collection<RosterEntry> rosterEntry = roster.getEntries();
    Iterator<RosterEntry> i = rosterEntry.iterator();
    while (i.hasNext()) {
        Entrieslist.add(i.next());
    }
    return Entrieslist;
}

五、取得使用者 VCard 資訊

public static VCard getUserVCard(XMPPConnection connection, String user)
        throws XMPPException {
    VCard vcard = new VCard();
    vcard.load(connection, user);
    return vcard;
}

六、取得使用者頭像

頭像同樣存在 VCard 中,取出後可把 InputStream 轉成 Drawable:

public static Drawable getUserImage(XMPPConnection connection, String user) {
    ByteArrayInputStream bais = null;
    try {
        VCard vcard = new VCard();
        // 這行程式碼用來解決 "No VCard for" 的問題
        ProviderManager.getInstance().addIQProvider("vCard", "vcard-temp",
                new org.jivesoftware.smackx.provider.VCardProvider());

        vcard.load(connection, user + "@" + connection.getServiceName());

        if (vcard == null || vcard.getAvatar() == null)
            return null;
        bais = new ByteArrayInputStream(vcard.getAvatar());
    } catch (Exception e) {
        e.printStackTrace();
    }
    if (bais == null)
        return null;
    return FormatTools.getInstance().InputStream2Drawable(bais);
}

七、新增好友(含或不含分組)

// 無分組
public static boolean addUser(Roster roster, String userName, String name) {
    try {
        roster.createEntry(userName, name, null);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

// 有分組
public static boolean addUser(Roster roster, String userName, String name,
        String groupName) {
    try {
        roster.createEntry(userName, name, new String[] { groupName });
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

八、刪除好友

public static boolean removeUser(Roster roster, String userName) {
    try {
        if (userName.contains("@")) {
            userName = userName.split("@")[0];
        }
        RosterEntry entry = roster.getEntry(userName);
        roster.removeEntry(entry);
        return true;
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
}

九、查詢使用者

serverDomain 為伺服器名稱,透過 UserSearchManager 送出搜尋表單並解析結果:

public static List<User> searchUsers(XMPPConnection connection,
        String serverDomain, String userName) throws XMPPException {
    List<User> results = new ArrayList<User>();

    UserSearchManager usm = new UserSearchManager(connection);

    Form searchForm = usm.getSearchForm(serverDomain);
    Form answerForm = searchForm.createAnswerForm();
    answerForm.setAnswer("userAccount", true);
    answerForm.setAnswer("userPhote", userName);
    ReportedData data = usm.getSearchResults(answerForm, serverDomain);

    Iterator<Row> it = data.getRows();
    Row row = null;
    User user = null;
    while (it.hasNext()) {
        user = new User();
        row = it.next();
        user.setUserAccount(row.getValues("userAccount").next().toString());
        user.setUserPhote(row.getValues("userPhote").next().toString());
        results.add(user);
    }
    return results;
}

十、修改使用者頭像

把圖片讀成位元組、編碼為 Base64 後寫入 VCard,再儲存回伺服器:

public static void changeImage(XMPPConnection connection, File f)
        throws XMPPException, IOException {

    VCard vcard = new VCard();
    vcard.load(connection);

    byte[] bytes = getFileBytes(f);
    String encodedImage = StringUtils.encodeBase64(bytes);
    vcard.setAvatar(bytes, encodedImage);
    vcard.setEncodedImage(encodedImage);
    vcard.setField("PHOTO", "<TYPE>image/jpg</TYPE><BINVAL>" + encodedImage
            + "</BINVAL>", true);

    ByteArrayInputStream bais = new ByteArrayInputStream(vcard.getAvatar());
    FormatTools.getInstance().InputStream2Bitmap(bais);

    vcard.save(connection);
}

掌握以上這些以 Roster 與 VCard 為核心的操作,就能應付大部分 IM 通訊錄的開發需求。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved