編輯:關於Android編程
一、查詢所有分組
通過Roster來獲取所有分組,Roster可以通過connection.getRoster()來得到。
[java]
<SPAN style="WHITE-SPACE: pre"> </SPAN>/**
* 獲取所有組
*
* @param roster
* @return 所有組集合
*/
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;
}
/**
* 獲取所有組
*
* @param roster
* @return 所有組集合
*/
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;
}
二、添加分組
也一樣通過roster來添加分組,groupName 為分組名。
[java]
SPAN style="WHITE-SPACE: pre"> </SPAN>/**
* 添加一個分組
*
* @param roster
* @param groupName
* @return
*/
public static boolean addGroup(Roster roster, String groupName) {
try {
roster.createGroup(groupName);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 添加一個分組
*
* @param roster
* @param groupName
* @return
*/
public static boolean addGroup(Roster roster, String groupName) {
try {
roster.createGroup(groupName);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
三、查詢某個組裡面的所有好友
很簡單不解釋...
[java]
<SPAN style="WHITE-SPACE: pre"> </SPAN>/**
* 獲取某個組裡面的所有好友
*
* @param roster
* @param groupName
* 組名
* @return
*/
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;
}
/**
* 獲取某個組裡面的所有好友
*
* @param roster
* @param groupName
* 組名
* @return
*/
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;
}
四、查詢所有好友信息
很簡單
[java]
<SPAN style="WHITE-SPACE: pre"> </SPAN>/**
* 獲取所有好友信息
*
* @param roster
* @return
*/
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;
}
/**
* 獲取所有好友信息
*
* @param roster
* @return
*/
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信息
[java]
<SPAN style="WHITE-SPACE: pre"> </SPAN>/**
* 獲取用戶VCard信息
*
* @param connection
* @param user
* @return
* @throws XMPPException
*/
public static VCard getUserVCard(XMPPConnection connection, String user)
throws XMPPException {
VCard vcard = new VCard();
vcard.load(connection, user);
return vcard;
}
/**
* 獲取用戶VCard信息
*
* @param connection
* @param user
* @return
* @throws XMPPException
*/
public static VCard getUserVCard(XMPPConnection connection, String user)
throws XMPPException {
VCard vcard = new VCard();
vcard.load(connection, user);
return vcard;
}
六、獲取用戶頭像信息
通過Vcard來獲取用戶頭像信息,可以把 InputStream 轉換為自己想要的類型,InputStream 轉Drawable
[java]
<SPAN style="WHITE-SPACE: pre"> </SPAN>/**
* 獲取用戶頭像信息
*
* @param connection
* @param user
* @return
*/
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);
}
/**
* 獲取用戶頭像信息
*
* @param connection
* @param user
* @return
*/
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);
}
七、添加好友(有、無分組)
[java]
<SPAN style="WHITE-SPACE: pre"> </SPAN>/**
* 添加好友 無分組
*
* @param roster
* @param userName
* @param name
* @return
*/
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;
}
}
/**
* 添加好友 有分組
*
* @param roster
* @param userName
* @param name
* @param groupName
* @return
*/
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;
}
}
/**
* 添加好友 無分組
*
* @param roster
* @param userName
* @param name
* @return
*/
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;
}
}
/**
* 添加好友 有分組
*
* @param roster
* @param userName
* @param name
* @param groupName
* @return
*/
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;
}
}
八、刪除好友
[java]
<SPAN style="WHITE-SPACE: pre"> </SPAN>/**
* 刪除好友
*
* @param roster
* @param userName
* @return
*/
public static boolean removeUser(Roster roster, String userName) {
try {
if (userName.contains("@")) {
userName = userName.split("@")[0];
}
RosterEntry entry = roster.getEntry(userName);
System.out.println("刪除好友:" + userName);
System.out.println("User." + roster.getEntry(userName) == null);
roster.removeEntry(entry);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 刪除好友
*
* @param roster
* @param userName
* @return
*/
public static boolean removeUser(Roster roster, String userName) {
try {
if (userName.contains("@")) {
userName = userName.split("@")[0];
}
RosterEntry entry = roster.getEntry(userName);
System.out.println("刪除好友:" + userName);
System.out.println("User." + roster.getEntry(userName) == null);
roster.removeEntry(entry);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
九、查詢用戶
serverDoMain 為服務器名
[java]
SPAN style="WHITE-SPACE: pre"> </SPAN>/**
* 查詢用戶
*
* @param connection
* @param serverDomain
* @param userName
* @return
* @throws XMPPException
*/
public static List<User> searchUsers(XMPPConnection connection,
String serverDomain, String userName) throws XMPPException {
List<User> results = new ArrayList<User>();
System.out.println("查詢開始..............." + connection.getHost()
+ connection.getServiceName());
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());
System.out.println(row.getValues("userAccount").next());
System.out.println(row.getValues("userPhote").next());
results.add(user);
// 若存在,則有返回,UserName一定非空,其他兩個若是有設,一定非空
}
return results;
}
/**
* 查詢用戶
*
* @param connection
* @param serverDomain
* @param userName
* @return
* @throws XMPPException
*/
public static List<User> searchUsers(XMPPConnection connection,
String serverDomain, String userName) throws XMPPException {
List<User> results = new ArrayList<User>();
System.out.println("查詢開始..............." + connection.getHost()
+ connection.getServiceName());
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());
System.out.println(row.getValues("userAccount").next());
System.out.println(row.getValues("userPhote").next());
results.add(user);
// 若存在,則有返回,UserName一定非空,其他兩個若是有設,一定非空
}
return results;
}
十、修改用戶頭像
[java]
<SPAN style="WHITE-SPACE: pre"> </SPAN>/**
* 修改用戶頭像
*
* @param connection
* @param f
* @throws XMPPException
* @throws IOException
*/
public static void changeImage(XMPPConnection connection, File f)
throws XMPPException, IOException {
VCard vcard = new VCard();
vcard.load(connection);
byte[] bytes;
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);
}
/**
* 修改用戶頭像
*
* @param connection
* @param f
* @throws XMPPException
* @throws IOException
*/
public static void changeImage(XMPPConnection connection, File f)
throws XMPPException, IOException {
VCard vcard = new VCard();
vcard.load(connection);
byte[] bytes;
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);
}
最近做的項目中,遇到個很棘手的問題: 客戶給我的數據是有限制的,因此,在返回某條具體頁面內容的時候,他只能給我一個html片段,裡面包含 文字,圖片以及附件的下載地址。如
相信去年聖誕節打開過手機淘寶的童鞋都會對當時的特效記憶猶新吧:全屏飄雪,旁邊還有個小雪人來控制八音盒背景音樂的播放,讓人有種身臨其境的感覺,甚至忍不住想狠狠購物了呢(誤)
本文來自http://blog.csdn.net/hellogv/ ,引用必須注明出處! 上次講了Android手機與BLE終端之間的通信,而最常見的BLE終端應該是蘋果
Linkify 詳解 Linkify是一個輔助類, 在TextView(包含派生類)中通過RegEx模式匹配創建超鏈接; Lin