【Android源码 栏目提醒】:网学会员为广大网友收集整理了,android即时通信系统源代码 - 知识管理,希望对大家有所帮助!
/文件:Principal.java 描述:项目 IMA 的。
主类,为学科发展 工作从天主教大学毕业米纳斯吉拉斯 Pocos 德卡尔达斯 - 爵开发者: 温贝托 Cerqueira Gobatto 帕德里弗朗西斯克莱特斯考克斯,665 CEP 证书:37701-355 市/州:德卡尔达斯 Pocos - 爵 国家:巴西 日期:2009 年 12 月 邮寄地址:humberto.gobattogmail.com/package br.
android.imaimport java.text.SimpleDateFormatimport java.util.ArrayListimport java.util.Calendarimport org.jivesoftware.smack.ConnectionConfigurationimport org.jivesoftware.smack.PacketListenerimport org.jivesoftware.smack.XMPPConnectionimport org.jivesoftware.smack.XMPPExceptionimport org.jivesoftware.smack.filter.MessageTypeFilterimport org.jivesoftware.smack.filter.PacketFilterimport org.jivesoftware.smack.packet.Messageimport org.jivesoftware.smack.packet.Packetimport org.jivesoftware.smack.packet.Presenceimport org.jivesoftware.smack.util.StringUtilsimport
android.app.Activityimport
android.app.NotificationManagerimport
android.content.Contextimport
android.content.Intentimport
android.os.Bundleimport
android.os.Handlerimport
android.util.Logimport
android.view.MenuItemimport
android.view.Viewimport
android.widget.AdapterViewimport
android.widget.ArrayAdapterimport
android.widget.Buttonimport
android.widget.EditTextimport
android.widget.ImageViewimport
android.widget.ListViewimport
android.widget.Spinnerimport
android.widget.Toastimport br.
android.ima.dialog.SettingsDialogimport br.
android.ima.notificacao.ServicoNotificaChatpublic class Principal extends Activity / Called when the activity is first created. / private ArrayListltStringgt messages new ArrayListltStringgt private Handler mHandler new Handler private SettingsDialog xmppConecta private EditText editDestino null private String txtEmail null private EditText editMens null private ListView listChat public static XMPPConnection connection public static final int CONECTAR 0 public static final int CONTATOS 1 public static final int SAIR 2 public int FINISH 0 public static int PAUSA 0 private String stxtStatus null private String mStatus new String quotOnlinequot quotOcupadoquot quotAusentequot quotAusenteEstendidoquot private String txtStatus null private String apelido null public final static Intent itNotifica new IntentquotNOTIFICAquot public SimpleDateFormat fhora new SimpleDateFormatquotH:mmquot Override protected void onActivityResultint codigo int resultado Intent it super.onActivityResultcodigo resultado it if codigo 1 ifit null Toast.makeTextthis quotNenhum contato selecionadoquotToast.LENGTH_SHORT.show editDestino.setTextnull txtEmail null return else Bundle params it.getExtras apelido params.getStringquotapelidoquot txtEmail params.getStringquotemailquot editDestino.setTexttxtEmail private void setListAdapter ArrayAdapterltStringgt adapter new ArrayAdapterltStringgtthis R.layout.multi_line_list_item messages listChat.setAdapteradapter Override public void onCreateBundle savedInstanceState super.onCreatesavedInstanceState setContentViewR.layout.main editDestino EditText this.findViewByIdR.id.destino editMens EditText this.findViewByIdR.id.Mensagem listChat ListView this.findViewByIdR.id.listMessages // 提请 XMPP 的窗口配置 xmppConecta new SettingsDialogthis //Botes Button send Button this.findViewByIdR.id.btnEnviar Button limpar Button this.findViewByIdR.id.btnLimpar // Aes aos botes limpar.setOnClickListenernew View.OnClickListener public void onClickView view txtEmail quotquot editDestino.setTextquotquot editMens.setTextquothuyuanquot editMens.clearFocus messages.clear setListAdapter send.setOnClickListenernew View.OnClickListener public void onClickView view String to txtEmail String text editMens.getText.toString text text.trim if editDestino.getText.toString.length gt 0 txtEmail editDestino.getText.toString if txtEmail null editDestino.getText.toString null Toast.makeTextPrincipal.this quot选择联系人菜单-gt联系!quot Toast.LENGTH_LONG.show else iftext quotquot text.length gt 0 Message msg new Messageto Message.Type.chat msg.setBodytext connection.sendPacketmsg Long ms Calendar.getInstance.getTimeInMillis String hora String.valueOffhora.formatms messages.add0hora quot - para: quotapelidoquot - eu: nquottext setListAdapter editMens.setTextnull public void setConnectionfinal XMPPConnection connecta Button send Button this.findViewByIdR.id.btnEnviar Spinner comboStatus Spinner findViewByIdR.id.comboStatus final ImageView imgStatus ImageView this.findViewByIdR.id.seuStatus //TextView txtStatus TextView this.findViewByIdR.id.labelStatus send.setEnabledtrue comboStatus.setClickabletrue //当你点击微调 comboStatus.setOnItemSelectedListenernew AdapterView.OnItemSelectedListener public void onItemSelectedAdapterViewltgt parent View v int posicao long id ConnectionConfiguration connConfig null if connection null connection.disconnect //Toast.makeTextPrincipal.this quot: quotSettingsDialog.hostToast.LENGTH_LONG.show if SettingsDialog.service quotquot // 创建有关的 XMPP 服 connConfig new ConnectionConfigurationSettingsDialog.hostSettingsDialog.port SettingsDialog.service else // 没有建立相关的 XMPP 服务器 connConfig new ConnectionConfigurationSettingsDialog.hostSettingsDialog.port XMPPConnection connection new XMPPConnectionconnConfig //editDestino.setTextconnection.getServiceName try if connection.isConnected false connection.connect catch XMPPException ex Log.equotIMAquot quot连接无法连接到 quot connection.getHost //Log.equotIMAquot ex.toString//connection null Toast.makeTextPrincipal.this quot 失 败 的 连 接 : quotSettingsDialog.hostToast.LENGTH_LONG.show //setConnectionnull try if connection.isConnected connection.loginSettingsDialog.usernameSettingsDialog.password Presence presence new PresencePresence.Type.available if posicao 0 presence.setModePresence.Mode.available txtStatus quotOnlinequot imgStatus.setImageResourceR.drawable.online else if posicao 1 presence.setModePresence.Mode.dnd txtStatus quotOcupadoquot imgStatus.setImageResourceR.drawable.ocupado else if posicao 3 presence.setModePresence.Mode.xa txtStatus quotAusente Estendidoquot imgStatus.setImageResourceR.drawable.ausente else if posicao 2 presence.setModePresence.Mode.away txtStatus quotAusentequot imgStatus.setImageResourceR.drawable.ausente setarStatustxtStatus presence.setStatusSettingsDialog.mpessoal connection.sendPacketpresence setConnectionconnection catch XMPPException ex Log.equotIMAquot quot设定对话无法登录为 quot SettingsDialog.username Log.equotIMAquot ex.toString // connection null // setConnectionnull //dismiss Toast.makeTextPrincipal.this quot 失 败 的 连 接 : quotSettingsDialog.hostToast.LENGTH_LONG.show public void onNothingSelectedAdapterViewltgt parent connection connecta if connection.isConnected connection.isAuthenticated send.setEnabledtrue if stxtStatus quotOnlinequot imgStatus.setImageResourceR.drawable.online comboStatus.setSelection0 else if stxtStatus quotOcupadoquot imgStatus.setImageResourceR.drawable.ocupado comboStatus.setSelection1 else if stxtStatus quotAusentequot imgStatus.setImageResourceR.drawable.ausente comboStatus.setSelection2 else comboStatus.setSelection3 //txtStatus.setTextstxtStatus // 添加包来获得传入的消息监听器 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 String nickname null int end fromName.lastIndexOfquotquot if end -1nickname fromName.substring0end String chat message.getBody Long ms Calendar.getInstance.getTimeInMillis String hora String.valueOffhora.formatms messages.add0horaquot - quotnicknamequot diz: nquotchat mHandler.postnew Runnable public void run setListAdapter filter else connection.disconnect connection null / protected void onStart super.onStart Log.iquot-------quot quot--------------------------------------------------------------------quot Log.equotonStartquot quotENTRADA PAUSA:quotPAUSAquot FINISH:quotFINISH//ENTRADA 输入 / protected void onRestart super.onRestart Log.iquot-------quot quot--------------------------------------------------------------------quot Log.equotonRestartquot quotENTRADA 重 启 PAUSA:quotPAUSAquot FINISH:quotFINISHquotON:quotServicoNotificaChat.on if PAUSA 1 PAUSA 0 if ServicoNotificaChat.on //对于服务 stopServiceitNotifica Log.wquotonRestartquotquotPAREI SERVIOquot else Log.wquotonRestartquotquotNO PAREI SERVIOquot Log.equotonRestartquot quotSAIDA PAUSA:quotPAUSAquot FINISH:quotFINISHquotON:quotServicoNotificaChat.on / protected void onResume super.onResume Log.iquot-------quot quot--------------------------------------------------------------------quot Log.equotonResumequot quotENTRADA PAUSA:quotPAUSAquot FINISH:quotFINISH / protected void onPause super.onPause Log.iquot-------quot quot--------------------------------------------------------------------quot Log.equotonPausequot quotENTRADA PAUSA:quotPAUSAquot FINISH:quotFINISHquotON:quotServicoNotificaChat.on PAUSA 1 if FINISH 0 ampamp connection null ampamp ServicoNotificaChat.on false Log.wquotonPausequot quotINICIANDO SERVICOquot PAUSA 1 startServiceitNotifica else PAUSA 0 Log.equotonPausequot quotSAIDA 即 时 通 信 暂 停 PAUSA:quotPAUSAquot FINISH:quotFINISHquotON:quotServicoNotificaChat.on / protected void onStop super.onStop Log.iquot-------quot quot--------------------------------------------------------------------quot Log.equotonStopquot quotENTRADA PAUSA:quotPAUSAquot FINISH:quotFINISH / protected void onDestroy super.onDestroy Log.iquot-------quot quot--------------------------------------------------------------------quot Log.equotonDestroyquot quotENTRADA 即时通信退出 PAUSA:quotPAUSAquot FINISH:quotFINISHquotON:quotServicoNotificaChat.on PAUSA 0 FINISH 0 if connection null if ServicoNotificaChat.on true //对于服务 stopServiceitNotifica ServicoNotificaChat.on false Log.wquotonDestroyquotquot停止服务quot else Log.wquotonDestroyquotquot停止服务quot // 取消通知 NotificationManager nm NotificationManagergetSystemServiceContext.NOTIFICATION_SERVICE // 要取消您需要使用相同的 ID 是用于创建 nm.cancelR.string.app_name connection.disconnect connection null Log.equotonDestroyquot quotSAIDA PAUSA:quotPAUSAquot FINISH:quotFINISHquotON:quotServicoNotificaChat.on //打破输出 Override public boolean onCreateOptionsMenuandroid.view.Menu menu super.onCreateOptionsMenumenu //菜单中添加了三个 OPES menu.add0 CONECTAR 0 quot连接quot //连接 .setIconR.drawable.conectar menu.add0 CONTATOS 0 quot联系人quot//联系人 .setIconR.drawable.contatos menu.add0 SAIR 0 quot退出quot//退出 .setIconR.drawable.fechar//关闭 return true Override //行动的菜单按钮 public boolean onMenuItemSelectedint featureId MenuItem ite.