【Android源码 栏目提醒】:网学会员为需要Android源码 的朋友们搜集整理了android平台即时通讯简单实现 源代码 - 企业软件开发相关资料,希望对各位网友有所帮助!
android平台即时通讯简单实现 package org.apache.android.xmpp import android.app.Activity import android.os.Bundle import android.os.Handler import android.util.Log import android.view.View import android.widget.ArrayAdapter import android.widget.Button import android.widget.EditText import android.widget.ListView import org.jivesoftware.smack.PacketListener import org.jivesoftware.smack.XMPPConnection import org.jivesoftware.smack.filter.MessageTypeFilter import org.jivesoftware.smack.filter.PacketFilter import org.jivesoftware.smack.packet.Message import org.jivesoftware.smack.packet.Packet import org.jivesoftware.smack.util.StringUtils import java.util.ArrayList public class XMPPClient extends Activity private ArrayList messages new ArrayList private Handler mHandler new Handler private SettingsDialog mDialog private EditText mRecipient private EditText mSendText private ListView mList private XMPPConnection connection / Called with the activity is first created. / Override public void onCreateBundle icicle super.onCreateicicle Log.iXMPPClient onCreate called setContentViewR.layout.main mRecipient EditText this.findViewByIdR.id.recipient Log.iXMPPClient mRecipient mRecipient mSendText EditText this.findViewByIdR.id.sendText Log.iXMPPClient mSendText mSendText mList ListView this.findViewByIdR.id.listMessages Log.iXMPPClient mList mList setListAdapter // Dialog for getting the xmpp settings mDialog new SettingsDialogthis // Set a listener to show the settings dialog Button setup Button this.findViewByIdR.id.setup setup.setOnClickListenernew View.OnClickListener public void onClickView view mHandler.postnew Runnable public void run mDialog.show // Set a listener to send a chat text message Button send Button this.findViewByIdR.id.send send.setOnClickListenernew View.OnClickListener public void onClickView view String to mRecipient.getText.toString String text mSendText.getText.toString Log.iXMPPClient Sending text text to to Message msg new Messageto Message.Type.chat msg.setBodytext connection.sendPacketmsg messages.addconnection.getUser : messages.addtext setListAdapter / Called by Settings dialog when a connection is establised with the XMPP server param connection / public void setConnection XMPPConnection connection this.connection connection if connection null // Add a packet listener to get messages sent to us PacketFilter filter new MessageTypeFilterMessage.Type.chat connection.addPacketListenernew PacketListener public void processPacketPacket packet Message message Message packet if message.getBody null String fromName StringUtils.parseBareAddressmessage.getFrom Log.iXMPPClient Got text message.getBody from fromName messages.addfromName : messages.addmessage.getBody // Add the incoming message to the list view mHandler.postnew Runnable public void run setListAdapter filter private void setListAdapter ArrayAdapter adapter new ArrayAdapterthis R.layout.multi_line_list_item messages mList.setAdapteradapter package org.apache.android.xmpp import android.app.Dialog import android.util.Log import android.view.View import android.widget.Button import android.widget.EditText import org.jivesoftware.smack.ConnectionConfiguration import org.jivesoftware.smack.XMPPConnection import org.jivesoftware.smack.XMPPException import org.jivesoftware.smack.packet.Presence / Gather the xmpp settings and create an XMPPConnection / public class SettingsDialog extends Dialog implements android.view.View.OnClickListener private XMPPClient xmppClient public SettingsDialogXMPPClient xmppClient superxmppClient this.xmppClient xmppClient protected void onStart super.onStart setContentViewR.layout.settings getWindow.setFlags4 4 setTitleXMPP Settings Button ok Button findViewByIdR.id.ok ok.setOnClickListenerthis public void onClickView v String host getTextR.id.host String port getTextR.id.port String service getTextR.id.service String username getTextR.id.userid String password getTextR.id.password // Create a connection ConnectionConfiguration connConfig new ConnectionConfigurationhost Integer.parseIntport service XMPPConnection connection new XMPPConnectionconnConfig try connection.connect Log.iXMPPClient SettingsDialog Connected to connection.getHost catch XMPPException ex Log.eXMPPClient SettingsDialog Failed to connect to connection.getHost Log.eXMPPClient ex.toString xmppClient.setConnectionnull try connection.loginusername password Log.iXMPPClient Logged in as connection.getUser // Set the status to available Presence presence new PresencePresence.Type.available connection.sendPacketpresence xmppClient.setConnectionconnection catch XMPPException ex Log.eXMPPClient SettingsDialog Failed to log in as username Log.eXMPPClient ex.toString xmppClient.setConnectionnull dismiss private String getTextint id EditText widget EditText this.findViewByIdid return widget.getText.toString