可能重复:
XMPP aSmack - How can I get the current user state (offline/online/away/etc.)?
我正在基于asmack lib的Android上开发聊天应用程序。我在ListView上显示所有用户,但我使用图像显示在线/离线用户。但它只返回离线图像,即使用户在线,这里是我的代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.buddies);
Controller.getInstance().roster = Controller.getInstance().connection.getRoster();
// ArrayList<Buddy> buddies = new ArrayList<Buddy>();
Collection<RosterEntry> entries = Controller.getInstance().roster.getEntries();
Controller.getInstance().buddyList = new Buddy[entries.size()];
int i = 0;
for (RosterEntry r : entries) {
Buddy bud = new Buddy();
VCard card = new VCard();
try {
ProviderManager.getInstance().addIQProvider("vCard",
"vcard-temp", new VCardProvider());
card.load(Controller.getInstance().connection, r.getUser());
} catch (XMPPException e) {
Log.e("ChatOnAndroid", e.getMessage() + " " + r.getUser() + " "
+ e.getLocalizedMessage());
}
bud.jid = r.getUser();
bud.name = r.getName();
bud.status = Controller.getInstance().roster.getPresence(r.getUser());
Controller.getInstance().buddies.add(bud);
Controller.getInstance().buddyList[i++] = bud;
}
BuddyAdapter adapter = new BuddyAdapter(this, R.layout.buddy, Controller.getInstance().buddies);
setListAdapter(adapter);
/*
* list = (ListView) findViewById(R.id.buddiesList);
* list.setAdapter(adapter);
*/
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
startActivity(new Intent(this, Conferences.class));
}
public class BuddyAdapter extends ArrayAdapter<Buddy> {
private ArrayList<Buddy> items;
public BuddyAdapter(Context context, int textViewResourceId,
ArrayList<Buddy> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.buddy, null);
}
Buddy buddy = items.get(position);
if (buddy != null) {
TextView tt = (TextView) v.findViewById(R.id.buddyName);
ImageView iv = (ImageView) v.findViewById(R.id.buddyThumb);
//buddy.status = Controller.getInstance().roster.getPresence(buddy.jid);
if (buddy.status != null) {
buddy.img = R.drawable.status_online;
iv.setImageResource(buddy.img);
} else if (buddy.status == null) {
buddy.img = R.drawable.status_offline;
iv.setImageResource(buddy.img);
}
//iv.setImageResource(buddy.img);
if (tt != null) {
tt.setText(buddy.name);
}
}
return v;
}
}
答案 0 :(得分:0)
您可以通过RosterListener获取在线和离线朋友,就像我在下面所做的那样,然后用新数据更新listview。
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
}
});
答案 1 :(得分:0)
有一种方法可以找到用户的离线/在线状态。可以通过寻找用户的存在来完成。以下是代码段:
//这是你在线或离线的方式
Presence availability = roster.getPresence(user);
以下是获取其存在模式的代码,即如果用户可用,那么他是离开,不要打扰模式或在线进行聊天。
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;
}
您可以将此状态保存在数组列表中: -
mFriendsDataClass.friendState = retrieveState_mode(availability.getMode(),availability.isAvailable());
如果您对聊天类型应用程序中的xmpp / smack有任何疑问,请与我们联系