/** 提交账号密码信息到服务器 */ private void submit() { String accounts = mAccounts.getText().toString(); String password = mPassword.getText().toString(); // accounts = "test"+accounts+"@"+FriendListActivity.SERVICE_NAME ; // password = "******"; if (accounts.length() == 0 || password.length() == 0) { DialogFactory.ToastDialog(this, "notice", "password cann not be empty"); } else { try { SmackAndroid.init(LoginActivity.this); Log.i("tong test", "accounts : " + accounts + " password:"******"notice", "login success"); // 跳转到好友列表 Intent intent = new Intent(); intent.putExtra("USERID", accounts); intent.setClass(LoginActivity.this, FriendListActivity.class); startActivity(intent); } catch (XMPPException e) { XmppConnection.closeConnection(); handler.sendEmptyMessage(2); e.printStackTrace(); } } }
/** * 接收文件 * * @author Administrator */ public void receivedFile() { /** 接收文件 */ // Create the file transfer manager final FileTransferManager manager = new FileTransferManager(XmppConnection.getConnection()); // Create the listener manager.addFileTransferListener( new FileTransferListener() { public void fileTransferRequest(FileTransferRequest request) { // Check to see if the request should be accepted Log.d("receivedFile ", " receive file"); if (shouldAccept(request)) { // Accept it IncomingFileTransfer transfer = request.accept(); try { System.out.println(request.getFileName()); File file = new File(RECORD_ROOT_PATH + request.getFileName()); android.os.Message msg = handler.obtainMessage(); transfer.recieveFile(file); MessageInfo msgInfo = queryMsgForListMsg(file.getName()); msgInfo.setFilePath(file.getPath()); // 更新 filepath new MyFileStatusThread(transfer, msgInfo).start(); } catch (XMPPException e) { e.printStackTrace(); } } else { // Reject it request.reject(); String[] args = new String[] { userChat, request.getFileName(), TimeRender.getDate(), "IN", MessageInfo.TYPE[0], MessageInfo.STATUS[1] }; MessageInfo msgInfo = new MessageInfo( args[0], "redio", args[2], args[3], MessageInfo.TYPE[0], MessageInfo.STATUS[1]); // 在handler里取出来显示消息 android.os.Message msg = handler.obtainMessage(); msg.what = 5; msg.obj = msgInfo; handler.sendMessage(msg); } } }); }
/** * 发送文件 * * @param path */ public void sendFile(String path, MessageInfo msg) { /** 发送文件 */ // Create the file transfer manager FileTransferManager sendFilemanager = new FileTransferManager(XmppConnection.getConnection()); // Create the outgoing file transfer sendTransfer = sendFilemanager.createOutgoingFileTransfer(userChatSendFile); // Send the file try { sendTransfer.sendFile(new java.io.File(path), "send file"); new MyFileStatusThread(sendTransfer, msg).start(); /** 监听 */ } catch (XMPPException e) { e.printStackTrace(); } }
private void init() { mNotificationManager = (NotificationManager) this.getSystemService(Service.NOTIFICATION_SERVICE); // 获取Intent传过来的用户名 this.pUSERID = getIntent().getStringExtra("USERID"); this.userChat = getIntent().getStringExtra("user"); /* * + "/" + * FriendListActivity * .RESOUCE_NAME; */ userChatSendFile = userChat + "/" + FriendListActivity.MY_RESOUCE_NAME; this.pFRIENDID = getIntent().getStringExtra("FRIENDID"); listMsg.addAll( (Collection<? extends MessageInfo>) getIntent().getParcelableArrayListExtra("cached")); /* * System.out.println("接收消息的用户pFRIENDID是:" + userChat); * System.out.println("发送消息的用户pUSERID是:" + pUSERID); * System.out.println(" 消息的用户pFRIENDID是:" + pFRIENDID); */ chat_name = (TextView) findViewById(R.id.chat_name); chat_name.setText(pFRIENDID); ListView listview = (ListView) findViewById(R.id.formclient_listview); listview.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL); this.adapter = new ChatListAdapter(this, listMsg); listview.setAdapter(adapter); // 获取文本信息 this.msgText = (EditText) findViewById(R.id.formclient_text); // 消息监听 cm = XmppConnection.getConnection().getChatManager(); // 返回按钮 Button mBtnBack = (Button) findViewById(R.id.chat_back); mBtnBack.setOnClickListener( new OnClickListener() { @Override public void onClick(View arg0) { finish(); } }); // receivedMsg();// 接收消息 textMsg(); // 发送+接受消息 receivedFile(); // 接收文件 }