Android high imitation mobile phone QQ chat

I downloaded the source point                           

Please indicate the source, thank you!

         The final version has been uploaded. Optimization drop-down refresh, to increase the voice message prompts, the main interface into ViewPager, achieve sliding around, the new group, show the number of recent sessions, began to work, not modified. Thank you!

        National Day these days, idle boredom, modeled QQ2012, made a socket-based chat tool, because the more the code, the code today is not posted in the article, a friend in need can click to download the above, thank you, follow-up will be posted in detail each module code, and explain, so stay tuned, O (∩_∩) O, what is the problem or bug, please leave me a message.

        First talk about my overall thoughts: The entire chat is transmitted through the server, so that treatment is relatively simple, but the server pressure is particularly great. Really recommend doing the project, the server only handles user registration, login, and determine whether the user dropped and so on, as chat, file transfer and video on a single connection is established between the user, which can greatly reduce the pressure on the server, I'm here even considered so much.

        First: We define a super message object (remember to serialization), which includes : a message type, a specific message object is sent to whom and from whom. Server and the client is to communicate by sending this super message object.

        Second: server, after accepting user connections, the socket immediately thrown into the thread pool, which can support multi-user concurrent access, then the user's socket object were to establish a read message thread and write a message thread ( here, write message thread first established, we need to pass to read the message thread, because then we will give users reply to a message after reading the message ). In a message read thread which according to the message type processed super message object, namely: registration, login, offline, forward messages, files, refresh the list of friends and so on. Back-end database processing, we pass dao mode, this is very convenient, but also makes the code look simple, straightforward, methodical, in short, all kinds of good, ha ha. Finally, note that: because we are forwarding the message, so the user login is successful, we need to put the user's written message threads based on user ID is stored in a Map, so, you can remove the corresponding user ID when forwarding messages write a message thread, in order to achieve forward the message.

       Third: the client, the server with similar, but not the thread pool, after the user has connected to the server, and is based on the object after the connection socket, respectively, establishes a read message thread and write a message thread . Then in the code where you need a message, you get the get method by writing a message thread , where the need to read the message, you get a get method to read the message thread .

        Fourth: About Write message threading , because the server or client, can not always need to write messages, so if we have to deal with an endless loop write thread, obviously it would be unwise, so I made a simple process, after endless loop written message to wait (), when we call setMessage way to write a message thread, the thread will notify wake up to write, after sending the message, continue to wait (), where I put core code:

 public void setMsg(TranObject msg) {
		this.msg = msg;
		synchronized (this) {
			notify();
		}
	}

	@Override
	public void run() {
		try {
			while (isStart) {
				if (msg != null) {
					oos.writeObject(msg);
					oos.flush();
					if (msg.getType() == TranObjectType.LOGOUT) {// 如果是发送下线的消息,就直接跳出循环
						break;
					}}
					synchronized (this) {
						wait();// 发送完消息后,线程进入等待状态
					}
				
			}
			oos.close();// 循环结束后,关闭输出流和socket
			if (socket != null)
				socket.close();
		} catch (InterruptedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}


        Fifth: concrete mobile client process, because android has its own characteristics, but also has its own advantages, so we have to make full use of its advantages, avoiding his shortcomings to process the message, I said what I deal with messages in this small project the idea: when I start the program user, open a service get the message , establish a connection to the service, and then go read the message thread monitor via an interface to the information received, at the same time receive a message, the message is the super the object through the broadcast sent out , then a custom abstract MyActivity Activity inheritance, in which a broadcast receiver receiving MyActivity message sent from the service by, and passed to the sub-Activity by an abstract method, we received to be processed if the activity of other message, you can inherit our custom myActivity, and then implement the abstract methods on it , so a good deal of the disadvantages of the different activity receiving messages, and background processing is also very convenient, I do not know Tencent QQ in this regard how to deal with, it is my personal wish Only. Posted MyActivity following code:

/**
 * 自定义一个抽象的MyActivity类,每个Activity都继承他,实现消息的接收(优化性能,减少代码重复)
 * 
 * @author way
 * 
 */
public abstract class MyActivity extends Activity {
	/**
	 * 广播接收者,接收GetMsgService发送过来的消息
	 */
	private BroadcastReceiver MsgReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			TranObject msg = (TranObject) intent
					.getSerializableExtra(Constants.MSGKEY);
			if (msg != null) {//如果不是空,说明是消息广播
				// System.out.println("MyActivity:" + msg);
				getMessage(msg);// 把收到的消息传递给子类
			} else {//如果是空消息,说明是关闭应用的广播
				close();
			}
		}
	};

	/**
	 * 抽象方法,用于子类处理消息,
	 * 
	 * @param msg
	 *            传递给子类的消息对象
	 */
	public abstract void getMessage(TranObject msg);

	/**
	 * 子类直接调用这个方法关闭应用
	 */
	public void close() {
		Intent i = new Intent();
		i.setAction(Constants.ACTION);
		sendBroadcast(i);
		finish();
	}

	@Override
	public void onStart() {// 在start方法中注册广播接收者
		super.onStart();
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Constants.ACTION);
		registerReceiver(MsgReceiver, intentFilter);// 注册接受消息广播

	}

	@Override
	protected void onStop() {// 在stop方法中注销广播接收者
		super.onStop();
		unregisterReceiver(MsgReceiver);// 注销接受消息广播
	}
}

 

       Well, this idea is probably below based on specific test shots, talk about my ideas:

1. 2. Welcome screen desktop shortcut

        

 

3. buddy list is landed 4. after a successful landing, accomplished by ViewPager

       

 

The buddy list is a custom ExpandableListView, you can drop down the refresh 6. Write implemented chat group

                  

 

7. The main chat interface, 8.ViewPager sliding movement in the left and right

      

 

9. 10. The most recent session does not enter the chat interface displayed to alert message, and save the database

       

 

11. When the alert message to the background, sound vibration, receive a new message is left, when the right is no new state,

  

 

12. The back-end database (on: user table below: buddy list), password encrypted via MD5 way, the user registration is successful, that generates a user-id named table, to save a friend.

 

 

13. The server running tips

 

14. The prompt registration status has been successful

 

 

Finally, to chat with a few shots, well here today, follow-up will continue to share with you the various other small modular concrete realization, take a break, play two days, immediately go to work, left ....

 

Reproduced in: https: //my.oschina.net/cjkall/blog/195786

Guess you like

Origin blog.csdn.net/weixin_33755847/article/details/91756560