0
即時通訊需要解決方案abot如何從服務變量更改UI,即時消息從服務聊天消息,我知道如何將消息轉換爲字符串,但我不知道如何發送他們到UI或其他活動,這是我的服務:從packetlistener服務發送數據到UI
public void setConnection(XMPPConnection connection) {
this.connection = connection;
if (connection != null) {
// Add a packet listener to get messages sent to us
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(new PacketListener() {
@Override
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
Log.i("XMPPChatDemoActivity ", " Text Recieved " + message.getBody() + " from
" + fromName);
messages.add(fromName + ":");
messages.add(message.getBody());
Log.i("XMPPChatDemoActivity",message.getBody());
mHandler.post(new Runnable() {
public void run() {
setListAdapter();
}
});
}
}
}, filter);
}
}
,這裏是其中的主要活動
public void setListAdapter() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
R.layout.listitem, messages);
listview.setAdapter(adapter);
}
我有奧托的情況下,公交車庫數據屬於,所以我試圖用intent和otto在mhandler.post之前,但似乎它完全停止了這個過程......可以你們幫我在這裏?感謝
這裏我做了mHandler.obtainMessage:
messages.add(fromName + ":");
messages.add(message.getBody());
Log.i("XMPPChatDemoActivity",message.getBody());
mHandler.obtainMessage(1,message).sendToTarget();
mHandler.post(new Runnable() {
public void run() {
// setListAdapter();
}
});
,並在活動:
private class getit extends Handler{
public void handleMessage(Message msg)
{
mHandler.post(new Runnable() {
public void run() {
setListAdapter();
}
});
}
}
請問什麼是int?我試了一下,創建活動中的私有類,味精了身體,卻沒有什麼:( – user3660198
也應我把什麼東西在sendtotarget? – user3660198
你只是傳遞一個對象時,INT可用於指定的類型如果對象可以有更多類型的不僅僅是一個類型'Message' – Fenix