如何在Asmack,Android中使用户在线或离线 [英] how to get user online or offline in asmack, android
问题描述
Possible Duplicate:
XMPP aSmack - How can I get the current user state (offline/online/away/etc.)?
我正在基于asmack lib的Android上开发聊天应用程序.我在ListView上显示所有用户,但我使用图像显示在线/离线用户.但是它仅返回离线图像,即使用户在线,这也是我的代码
I am developing chat app on Android base on asmack lib. I display all the user on the ListView but I use an image to show online/offline user. But It return offline image only, even the user is online, here is my code
@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;
}
}
推荐答案
有一种方法可以查找用户的离线/在线状态.可以通过寻求用户的存在来完成.这是代码片段:
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());
如果您在聊天类型应用程序中对xmpp/smack有任何疑问,请告诉我
Please let me know if you have any queries regarding xmpp/smack in chat type application
这篇关于如何在Asmack,Android中使用户在线或离线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!