基於xmpp openfire smack 開發(四)單聊

1、

class Thread1 extends Thread {
		@Override
		public void run() {
			uid = preferences.getString("uid", "0");
			if (!uid.equals("0")) {
				Login(Content.DaTingSERVICE_URL, 5222, uid, "111111", uid + "@"
						+ Content.DaTingSERVICE_Name);
			} else {
				handler.sendEmptyMessage(4);
				Intent intent = new Intent(SiXinActivity.this,
						LoginActivity.class);
				SiXinActivity.this.startActivity(intent);
				exitApp();
			}
		}
	}
2、

public void Login(final String ServerIP, final int serverport,
			final String Username, final String Password, String userid) {
		UserId = userid;

		preferences = GoosunApplication.mApplication.getPreferences();
		User = WonickName + "#" + uid + "#" + WoUrl;
		connConfig = new ConnectionConfiguration(ServerIP, serverport,
				Content.DaTingSERVICE_Name);
		connConfig.setReconnectionAllowed(true);
		connConfig.setSASLAuthenticationEnabled(false);
		connConfig
				.setSecurityMode(ConnectionConfiguration.SecurityMode.disabled);
		xmppConnection = new XMPPConnection(connConfig);

		new Thread() {
			@Override
			public void run() {
				try {
					xmppConnection.connect();
					xmppConnection.login(Username, Password);
					RegisterMessageListener();
				} catch (Exception ex) {
					ex.printStackTrace();
					handler.sendEmptyMessage(3);
				}
			}
		}.start();
	}
3、
// 註冊單人對話監聽
	protected void RegisterMessageListener() {
		chatManager = xmppConnection.getChatManager();
		chatManager.addChatListener(new ChatManagerListener() {
			public void chatCreated(Chat chat, boolean arg1) {
				chat.addMessageListener(new MessageListener() {
					public void processMessage(Chat arg0, Message message) {
						String msg = message.getBody();
						String fromString = message.getFrom();
						if (msg.contains("危險")) {
						}
						sendhandlemsg(msg,fromString,0);
					}
				});
			}
		});
	}

4、

// 發送1 to 1的消息
	public void semdmessage(final String msg, final String msgto) {
		// 初始化發送消息
		chat = chatManager.createChat(msgto, null);
		new Thread() {
			@Override
			public void run() {
				try {
					chat.sendMessage(msg);
					sendhandlemsg(msg,User,1);
				} catch (XMPPException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}.start();
	}
5、

public void sendhandlemsg(String msg,String fromString,int i)
	{
		entity = new ChatMsgEntity();
		fromString=TanickName+"#"+TaUid+"#"+TaUrl;
		
		if(i==0){
			entity.setMsgType(true);
			entity.setName(fromString);
		}else{
			entity.setMsgType(false);
			entity.setName(User);
		}
		entity.setDate(new Date());
		entity.setText(msg.toString());
		mDataArrays.add(entity);

		handler.sendEmptyMessage(1);
	}
6、

	private void send() {
		contString = mEditTextContent.getText().toString();
		if ("".equals(contString.trim())) {
			Toast.makeText(this, "不能發送空白消息", Toast.LENGTH_SHORT).show();
		} else if (contString.length() > 0) {
			semdmessage(contString, TaUid + "@" + Content.DaTingSERVICE_Name);
			msgStr = null;
			mEditTextContent.setText(null);
		}
	}


7、

public Handler handler = new Handler(Looper.myLooper()) {
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 1:
				mAdapter.notifyDataSetChanged();
				mListView.setSelection(mListView.getCount() - 1);
				zhuangtai = true;
				break;


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章