Commit 8ee1b9a780bbd0a533b70ec25d715e339c319454

Authored by 王琳
1 parent 44327355

优化demo代码,添加注释

.idea/vcs.xml 0 → 100644
  1 +<?xml version="1.0" encoding="UTF-8"?>
  2 +<project version="4">
  3 + <component name="VcsDirectoryMappings">
  4 + <mapping directory="$PROJECT_DIR$" vcs="Git" />
  5 + </component>
  6 +</project>
0 7 \ No newline at end of file
... ...
IMLib/build/intermediates/bundles/release/classes.jar
No preview for this file type
IMLib/build/intermediates/classes/release/io/rong/imlib/Chat.class
No preview for this file type
IMLib/build/intermediates/incremental/mergeDebugAndroidTestResources/compile-file-map.properties
1   -#Wed Feb 22 10:05:11 CST 2017
  1 +#Wed Feb 22 15:18:03 CST 2017
... ...
IMLib/build/intermediates/incremental/mergeDebugResources/compile-file-map.properties
1   -#Wed Feb 22 10:05:09 CST 2017
  1 +#Wed Feb 22 15:18:02 CST 2017
... ...
IMLib/build/intermediates/incremental/mergeReleaseResources/compile-file-map.properties
1   -#Wed Feb 22 10:05:31 CST 2017
  1 +#Wed Feb 22 15:18:18 CST 2017
... ...
IMLib/build/intermediates/incremental/packageDebugResources/compile-file-map.properties
1   -#Wed Feb 22 10:05:11 CST 2017
  1 +#Wed Feb 22 15:18:03 CST 2017
... ...
IMLib/build/intermediates/incremental/packageReleaseResources/compile-file-map.properties
1   -#Wed Feb 22 10:05:11 CST 2017
  1 +#Wed Feb 22 15:18:03 CST 2017
... ...
IMLib/build/intermediates/res/resources-debug-androidTest.ap_
No preview for this file type
IMLib/build/intermediates/transforms/proguard/release/jars/3/3/main.jar
No preview for this file type
IMLib/build/outputs/aar/IMLib-debug.aar
No preview for this file type
IMLib/build/outputs/aar/IMLib-release.aar
No preview for this file type
IMLib/build/outputs/mapping/release/dump.txt
... ... @@ -6603,7 +6603,7 @@ Constant Pool (count = 426):
6603 6603 + Methodref [android/util/Log.e (Ljava/lang/String;Ljava/lang/String;)I]
6604 6604 + Methodref [io/rong/imlib/Chat.<init> ()V]
6605 6605 + Methodref [io/rong/imlib/Chat.cnMessage (Lio/rong/imlib/model/Message;)Lio/rong/imlib/model/CNMessage;]
6606   - + Methodref [io/rong/imlib/Chat.getToken (Lio/rong/imlib/model/UserInfo;Lio/rong/imlib/network/HttpUtil$OnResponse;)V]
  6606 + + Methodref [io/rong/imlib/Chat.getToken (Lio/rong/imlib/model/CNUserInfo;Lio/rong/imlib/network/HttpUtil$OnResponse;)V]
6607 6607 + Methodref [io/rong/imlib/Chat.sendChatRoomMessage (Lio/rong/imlib/model/MessageContent;)V]
6608 6608 + Methodref [io/rong/imlib/Chat.setChatRoomActionListener (Lio/rong/imlib/RongIMClient$ChatRoomActionListener;)V]
6609 6609 + Methodref [io/rong/imlib/Chat.setConnectStatusListener (Lio/rong/imlib/RongIMClient$ConnectionStatusListener;)V]
... ... @@ -6691,7 +6691,7 @@ Constant Pool (count = 426):
6691 6691 + NameAndType [getName ()Ljava/lang/String;]
6692 6692 + NameAndType [getPortraitUri ()Landroid/net/Uri;]
6693 6693 + NameAndType [getRcHeader (Ljava/lang/String;Ljava/lang/String;)Lio/rong/imlib/network/HttpUtil$Header;]
6694   - + NameAndType [getToken (Lio/rong/imlib/model/UserInfo;Lio/rong/imlib/network/HttpUtil$OnResponse;)V]
  6694 + + NameAndType [getToken (Lio/rong/imlib/model/CNUserInfo;Lio/rong/imlib/network/HttpUtil$OnResponse;)V]
6695 6695 + NameAndType [getUserId ()Ljava/lang/String;]
6696 6696 + NameAndType [getUserInfo ()Lio/rong/imlib/model/UserInfo;]
6697 6697 + NameAndType [getUserName ()Ljava/lang/String;]
... ... @@ -6760,12 +6760,12 @@ Constant Pool (count = 426):
6760 6760 + Utf8 [(Lio/rong/imlib/base/IChat$OnResultListener;)V]
6761 6761 + Utf8 [(Lio/rong/imlib/base/IChat$OnSendMessageListener;)V]
6762 6762 + Utf8 [(Lio/rong/imlib/model/CNUserInfo;)V]
  6763 + + Utf8 [(Lio/rong/imlib/model/CNUserInfo;Lio/rong/imlib/network/HttpUtil$OnResponse;)V]
6763 6764 + Utf8 [(Lio/rong/imlib/model/Conversation$ConversationType;Ljava/lang/String;ILio/rong/imlib/RongIMClient$ResultCallback;)V]
6764 6765 + Utf8 [(Lio/rong/imlib/model/Message;)Lio/rong/imlib/model/CNMessage;]
6765 6766 + Utf8 [(Lio/rong/imlib/model/Message;Ljava/lang/String;Ljava/lang/String;Lio/rong/imlib/RongIMClient$SendMessageCallback;Lio/rong/imlib/RongIMClient$ResultCallback;)V]
6766 6767 + Utf8 [(Lio/rong/imlib/model/MessageContent;)V]
6767 6768 + Utf8 [(Lio/rong/imlib/model/UserInfo;)V]
6768   - + Utf8 [(Lio/rong/imlib/model/UserInfo;Lio/rong/imlib/network/HttpUtil$OnResponse;)V]
6769 6769 + Utf8 [(Ljava/lang/Object;)Ljava/lang/StringBuilder;]
6770 6770 + Utf8 [(Ljava/lang/String;)Landroid/net/Uri;]
6771 6771 + Utf8 [(Ljava/lang/String;)Lio/rong/message/TextMessage;]
... ... @@ -7098,7 +7098,7 @@ Methods (count = 37):
7098 7098 Access flags: 0x1
7099 7099 = public void connect(io.rong.imlib.model.CNUserInfo)
7100 7100 Class member attributes (count = 1):
7101   - + Code attribute instructions (code length = 118, locals = 5, stack = 5):
  7101 + + Code attribute instructions (code length = 116, locals = 5, stack = 5):
7102 7102 [0] aload_1 v1
7103 7103 [1] invokevirtual #116
7104 7104 + Methodref [io/rong/imlib/model/CNUserInfo.getUserId ()Ljava/lang/String;]
... ... @@ -7170,17 +7170,16 @@ Methods (count = 37):
7170 7170 + Methodref [io/rong/imlib/model/UserInfo.<init> (Ljava/lang/String;Ljava/lang/String;Landroid/net/Uri;)V]
7171 7171 [100] putstatic #64
7172 7172 + Fieldref [io/rong/imlib/Chat.currentUser Lio/rong/imlib/model/UserInfo;]
7173   - [103] getstatic #64
7174   - + Fieldref [io/rong/imlib/Chat.currentUser Lio/rong/imlib/model/UserInfo;]
7175   - [106] new #12
  7173 + [103] aload_1 v1
  7174 + [104] new #12
7176 7175 + Class [io/rong/imlib/Chat$1]
7177   - [109] dup
7178   - [110] aload_0 v0
7179   - [111] invokespecial #86
  7176 + [107] dup
  7177 + [108] aload_0 v0
  7178 + [109] invokespecial #86
7180 7179 + Methodref [io/rong/imlib/Chat$1.<init> (Lio/rong/imlib/Chat;)V]
7181   - [114] invokestatic #81
7182   - + Methodref [io/rong/imlib/Chat.getToken (Lio/rong/imlib/model/UserInfo;Lio/rong/imlib/network/HttpUtil$OnResponse;)V]
7183   - [117] return
  7180 + [112] invokestatic #81
  7181 + + Methodref [io/rong/imlib/Chat.getToken (Lio/rong/imlib/model/CNUserInfo;Lio/rong/imlib/network/HttpUtil$OnResponse;)V]
  7182 + [115] return
7184 7183 Code attribute exceptions (count = 0):
7185 7184 Code attribute attributes (attribute count = 1):
7186 7185 + Line number table attribute (count = 7)
... ... @@ -7190,12 +7189,12 @@ Methods (count = 37):
7190 7189 [52] -> line 74
7191 7190 [86] -> line 75
7192 7191 [103] -> line 76
7193   - [117] -> line 91
7194   - + Method: getToken(Lio/rong/imlib/model/UserInfo;Lio/rong/imlib/network/HttpUtil$OnResponse;)V
  7192 + [115] -> line 91
  7193 + + Method: getToken(Lio/rong/imlib/model/CNUserInfo;Lio/rong/imlib/network/HttpUtil$OnResponse;)V
7195 7194 Access flags: 0x9
7196   - = public static void getToken(io.rong.imlib.model.UserInfo,io.rong.imlib.network.HttpUtil$OnResponse)
  7195 + = public static void getToken(io.rong.imlib.model.CNUserInfo,io.rong.imlib.network.HttpUtil$OnResponse)
7197 7196 Class member attributes (count = 1):
7198   - + Code attribute instructions (code length = 82, locals = 6, stack = 5):
  7197 + + Code attribute instructions (code length = 85, locals = 6, stack = 5):
7199 7198 [0] ldc #7
7200 7199 + String [https://api.cn.ronghub.com/user/getToken.json]
7201 7200 [2] astore_2 v2
... ... @@ -7216,8 +7215,8 @@ Methods (count = 37):
7216 7215 [22] invokevirtual #139
7217 7216 + Methodref [java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;]
7218 7217 [25] aload_0 v0
7219   - [26] invokevirtual #128
7220   - + Methodref [io/rong/imlib/model/UserInfo.getUserId ()Ljava/lang/String;]
  7218 + [26] invokevirtual #116
  7219 + + Methodref [io/rong/imlib/model/CNUserInfo.getUserId ()Ljava/lang/String;]
7221 7220 [29] invokevirtual #139
7222 7221 + Methodref [java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;]
7223 7222 [32] ldc #3
... ... @@ -7225,8 +7224,8 @@ Methods (count = 37):
7225 7224 [34] invokevirtual #139
7226 7225 + Methodref [java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;]
7227 7226 [37] aload_0 v0
7228   - [38] invokevirtual #126
7229   - + Methodref [io/rong/imlib/model/UserInfo.getName ()Ljava/lang/String;]
  7227 + [38] invokevirtual #117
  7228 + + Methodref [io/rong/imlib/model/CNUserInfo.getUserName ()Ljava/lang/String;]
7230 7229 [41] invokevirtual #139
7231 7230 + Methodref [java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;]
7232 7231 [44] ldc #4
... ... @@ -7234,37 +7233,39 @@ Methods (count = 37):
7234 7233 [46] invokevirtual #139
7235 7234 + Methodref [java/lang/StringBuilder.append (Ljava/lang/String;)Ljava/lang/StringBuilder;]
7236 7235 [49] aload_0 v0
7237   - [50] invokevirtual #127
7238   - + Methodref [io/rong/imlib/model/UserInfo.getPortraitUri ()Landroid/net/Uri;]
7239   - [53] invokevirtual #138
  7236 + [50] invokevirtual #115
  7237 + + Methodref [io/rong/imlib/model/CNUserInfo.getHeadUrl ()Ljava/lang/String;]
  7238 + [53] invokestatic #76
  7239 + + Methodref [android/net/Uri.parse (Ljava/lang/String;)Landroid/net/Uri;]
  7240 + [56] invokevirtual #138
7240 7241 + Methodref [java/lang/StringBuilder.append (Ljava/lang/Object;)Ljava/lang/StringBuilder;]
7241   - [56] invokevirtual #140
  7242 + [59] invokevirtual #140
7242 7243 + Methodref [java/lang/StringBuilder.toString ()Ljava/lang/String;]
7243   - [59] astore v4
7244   - [61] new #50
  7244 + [62] astore v4
  7245 + [64] new #50
7245 7246 + Class [io/rong/imlib/network/HttpUtil]
7246   - [64] dup
7247   - [65] invokespecial #129
  7247 + [67] dup
  7248 + [68] invokespecial #129
7248 7249 + Methodref [io/rong/imlib/network/HttpUtil.<init> ()V]
7249   - [68] astore v5
7250   - [70] aload v5
7251   - [72] ldc #7
  7250 + [71] astore v5
  7251 + [73] aload v5
  7252 + [75] ldc #7
7252 7253 + String [https://api.cn.ronghub.com/user/getToken.json]
7253   - [74] aload_3 v3
7254   - [75] aload v4
7255   - [77] aload_1 v1
7256   - [78] invokevirtual #131
  7254 + [77] aload_3 v3
  7255 + [78] aload v4
  7256 + [80] aload_1 v1
  7257 + [81] invokevirtual #131
7257 7258 + Methodref [io/rong/imlib/network/HttpUtil.post (Ljava/lang/String;Lio/rong/imlib/network/HttpUtil$Header;Ljava/lang/String;Lio/rong/imlib/network/HttpUtil$OnResponse;)V]
7258   - [81] return
  7259 + [84] return
7259 7260 Code attribute exceptions (count = 0):
7260 7261 Code attribute attributes (attribute count = 1):
7261 7262 + Line number table attribute (count = 6)
7262 7263 [0] -> line 95
7263 7264 [3] -> line 96
7264 7265 [13] -> line 97
7265   - [61] -> line 98
7266   - [70] -> line 99
7267   - [81] -> line 100
  7266 + [64] -> line 98
  7267 + [73] -> line 99
  7268 + [84] -> line 100
7268 7269 + Method: connect(Ljava/lang/String;)V
7269 7270 Access flags: 0x1
7270 7271 = public void connect(java.lang.String)
... ...
IMLib/build/outputs/mapping/release/mapping.txt
... ... @@ -149,7 +149,7 @@ io.rong.imlib.Chat -&gt; io.rong.imlib.Chat:
149 149 58:324:void <init>() -> <init>
150 150 65:68:void init(android.content.Context,java.lang.String,java.lang.String) -> init
151 151 71:91:void connect(io.rong.imlib.model.CNUserInfo) -> connect
152   - 95:100:void getToken(io.rong.imlib.model.UserInfo,io.rong.imlib.network.HttpUtil$OnResponse) -> getToken
  152 + 95:100:void getToken(io.rong.imlib.model.CNUserInfo,io.rong.imlib.network.HttpUtil$OnResponse) -> getToken
153 153 104:124:void connect(java.lang.String) -> connect
154 154 128:130:void disconnect(boolean) -> disconnect
155 155 134:148:void joinChatRoom(java.lang.String,int) -> joinChatRoom
... ...
IMLib/build/outputs/mapping/release/seeds.txt
... ... @@ -149,7 +149,7 @@ io.rong.imlib.Chat: io.rong.imlib.Chat getInstance()
149 149 io.rong.imlib.Chat: Chat()
150 150 io.rong.imlib.Chat: void init(android.content.Context,java.lang.String,java.lang.String)
151 151 io.rong.imlib.Chat: void connect(io.rong.imlib.model.CNUserInfo)
152   -io.rong.imlib.Chat: void getToken(io.rong.imlib.model.UserInfo,io.rong.imlib.network.HttpUtil$OnResponse)
  152 +io.rong.imlib.Chat: void getToken(io.rong.imlib.model.CNUserInfo,io.rong.imlib.network.HttpUtil$OnResponse)
153 153 io.rong.imlib.Chat: void connect(java.lang.String)
154 154 io.rong.imlib.Chat: void disconnect(boolean)
155 155 io.rong.imlib.Chat: void joinChatRoom(java.lang.String,int)
... ...
IMLib/src/main/java/io.rong.imlib/Chat.java
... ... @@ -73,7 +73,7 @@ public class Chat implements IChat {
73 73 String headUrl = cnUserInfo.getHeadUrl() == null ? "" : cnUserInfo.getHeadUrl();
74 74 Log.e("DemoView", userId + " : " + userName + " : ");
75 75 currentUser = new UserInfo(userId, userName, Uri.parse(headUrl));
76   - getToken(currentUser, new HttpUtil.OnResponse() {
  76 + getToken(cnUserInfo, new HttpUtil.OnResponse() {
77 77 @Override
78 78 public void onResponse(int code, String body) {
79 79 if (code != 200) return;
... ... @@ -91,10 +91,10 @@ public class Chat implements IChat {
91 91 }
92 92  
93 93 //获得token
94   - public static void getToken(UserInfo user, HttpUtil.OnResponse callback) {
  94 + public static void getToken(CNUserInfo user, HttpUtil.OnResponse callback) {
95 95 final String HTTP_GET_TOKEN = "https://api.cn.ronghub.com/user/getToken.json";
96 96 HttpUtil.Header header = HttpUtil.getRcHeader(APP_KEY, APP_SECRET);
97   - String body = "userId=" + user.getUserId() + "&name=" + user.getName() + "&portraitUri=" + user.getPortraitUri();
  97 + String body = "userId=" + user.getUserId() + "&name=" + user.getUserName() + "&portraitUri=" + Uri.parse(user.getHeadUrl());
98 98 HttpUtil httpUtil = new HttpUtil();
99 99 httpUtil.post(HTTP_GET_TOKEN, header, body, callback);
100 100 }
... ... @@ -415,5 +415,4 @@ public class Chat implements IChat {
415 415 public void setCurrentUser(UserInfo user) {
416 416 currentUser = user;
417 417 }
418   -
419 418 }
... ...
app/src/main/AndroidManifest.xml
... ... @@ -18,11 +18,16 @@
18 18 <category android:name="android.intent.category.LAUNCHER" />
19 19 </intent-filter>
20 20 </activity>
21   - <activity android:name=".ui.ChatRoomActivity" />
  21 + <activity
  22 + android:name=".ui.ChatRoomActivity"
  23 + android:launchMode="singleTop"
  24 + android:screenOrientation="portrait"
  25 + android:windowSoftInputMode="adjustResize" />
22 26 <activity
23 27 android:name=".ui.PrivateChatActivity"
24 28 android:launchMode="singleTop"
25   - android:screenOrientation="portrait">
  29 + android:screenOrientation="portrait"
  30 + android:windowSoftInputMode="adjustResize">
26 31 <intent-filter>
27 32 <action android:name="android.intent.action.VIEW" />
28 33  
... ...
app/src/main/java/com/cnlive/chat/receiver/CNPushMessageReceiver.java
... ... @@ -13,11 +13,13 @@ public class CNPushMessageReceiver extends SealNotificationReceiver {
13 13  
14 14 @Override
15 15 public boolean onNotificationMessageArrived(Context var1, CNNotificationMessage var2) {
  16 + //返回false,会弹出SDK默认的通知;返回true,SDK不会弹出通知,通知需要您自定义
16 17 return false;
17 18 }
18 19  
19 20 @Override
20 21 public boolean onNotificationMessageClicked(Context var1, CNNotificationMessage var2) {
  22 + //返回false,会走SDK默认的处理逻辑,即点击该通知则会打开会话列表或会话界面;返回true,则由您自定义处理逻辑
21 23 return false;
22 24 }
23 25 }
... ...
app/src/main/java/com/cnlive/chat/ui/ChatRoomActivity.java
1 1 package com.cnlive.chat.ui;
2 2  
3   -import android.content.Intent;
4   -import android.net.Uri;
5 3 import android.os.Handler;
6 4 import android.support.v7.app.AppCompatActivity;
7 5 import android.os.Bundle;
... ... @@ -15,24 +13,22 @@ import com.cnlive.chat.R;
15 13 import com.cnlive.chat.adapter.ChatListAdapter;
16 14  
17 15 import java.util.ArrayList;
18   -import java.util.StringTokenizer;
19 16  
20   -import io.rong.imlib.Chat;
21 17 import io.rong.imlib.model.CNMessage;
22 18 import io.rong.imlib.ChatUtil;
23 19 import io.rong.imlib.base.IChat;
24 20 import io.rong.imlib.model.CNUserInfo;
25   -import io.rong.imlib.model.UserInfo;
26 21  
27   -public class ChatRoomActivity extends AppCompatActivity implements View.OnClickListener, IChat.OnMessageListener, IChat.OnConnectStatusListener, IChat.OnChatRoomActionListener {
  22 +public class ChatRoomActivity extends AppCompatActivity implements View.OnClickListener {
28 23  
29 24 private static final String TAG = "ChatRoomActivity";
30 25 private ListView chatListView;
31 26 private EditText messageContent;
32 27 private Button sendMessageBtn;
33   - private Button changeUserInfo;
  28 +// private Button changeUserInfo;
34 29  
35 30 private String roomId;
  31 + private CNUserInfo userInfo;
36 32  
37 33 private ChatListAdapter chatListAdapter;
38 34 private ArrayList<CNMessage> messageList = new ArrayList<>();
... ... @@ -44,15 +40,21 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
44 40 protected void onCreate(Bundle savedInstanceState) {
45 41 super.onCreate(savedInstanceState);
46 42 setContentView(R.layout.activity_chat_room);
  43 +
  44 + ChatUtil.setOnConnectionListener(onConnectListener);
  45 + userInfo = new CNUserInfo("2017010102", "J", "http://7xs9j5.com1.z0.glb.clouddn.com/liveapp/guojunwang.jpg");
  46 + //连接服务器
  47 + ChatUtil.connect(userInfo);
  48 +
47 49 mHandler = new Handler(this.getMainLooper());
48 50 initView();
49 51 ChatUtil.setOnSendMessageListener(onSendMessageListener);
50 52 ChatUtil.setOnOperationListener(onOperationListener);
51 53 ChatUtil.setOnResultListener(onResultListener);
52 54 joinChatRoom();
53   - ChatUtil.setOnMessageListener(this);
54   - ChatUtil.setConnectStatusListener(this);
55   - ChatUtil.setOnChatRoomActionListener(this);
  55 + ChatUtil.setOnMessageListener(onMessageListener);
  56 + ChatUtil.setConnectStatusListener(onConnectStatusListener);
  57 + ChatUtil.setOnChatRoomActionListener(onChatRoomActionListener);
56 58 }
57 59  
58 60 private void initView() {
... ... @@ -62,8 +64,8 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
62 64 messageContent = (EditText) findViewById(R.id.message_content);
63 65 sendMessageBtn = (Button) findViewById(R.id.send_message);
64 66 sendMessageBtn.setOnClickListener(this);
65   - changeUserInfo = (Button) findViewById(R.id.change_user_info);
66   - changeUserInfo.setOnClickListener(this);
  67 +// changeUserInfo = (Button) findViewById(R.id.change_user_info);
  68 +// changeUserInfo.setOnClickListener(this);
67 69 }
68 70  
69 71 //加入聊天室
... ... @@ -80,60 +82,26 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
80 82 public void onClick(View v) {
81 83 switch (v.getId()) {
82 84 case R.id.send_message:
  85 + //发送聊天室消息
83 86 ChatUtil.sendChatRoomMessage(messageContent.getText().toString());
84 87 messageContent.setText("");
85 88 break;
86   - case R.id.change_user_info:
87   - ChatUtil.currentUser().setName("Ha");
88   - break;
  89 +// case R.id.change_user_info:
  90 +// ChatUtil.currentUser().setName("Ha");
  91 +// break;
89 92 }
90 93 }
91 94  
92   - @Override
93   - public void processMessage(final CNMessage message) {
94   -
95   - Log.e("DemoView", message.getUserInfo().getUserName() + " : " + message.getContent());
96   -
97   - mHandler.post(new Runnable() {
98   - @Override
99   - public void run() {
100   - messageList.add(message);
101   - chatListAdapter.updateItems(messageList);
102   - chatListAdapter.notifyDataSetChanged();
103   - }
104   - });
105   - }
106   -
107   - @Override
108   - public void onConnectStatus(int what, String extra) {
109   - Log.e(TAG, "status : what " + what + " extra : " + extra);
110   - }
111   -
112   - @Override
113   - public void onJoining(String s) {
114   - Log.e("DemoView", "onJoining + : " + s);
115   - }
116   -
117   - @Override
118   - public void onJoined(String s) {
119   - Log.e("DemoView", "onJoined + : " + s);
120   - }
121   -
122   - @Override
123   - public void onQuited(String s) {
124   - Log.e(TAG, "onQuited + : " + s);
125   - }
126   -
127   - @Override
128   - public void onError(String s, int errorCode, String errorMessage) {
129   - Log.e("DemoView", "onError + : " + s);
130   - }
131 95  
  96 + /**
  97 + * 发送消息监听
  98 + */
132 99 private IChat.OnSendMessageListener onSendMessageListener = new IChat.OnSendMessageListener() {
133 100 @Override
134 101 public void onSuccess(Integer integer) {
135 102 Log.i(TAG, "SendMessageListener SUCCESS");
136 103 //聊天SDK屏蔽用户自己发送的消息,所以在发送消息成功的时候需要根据需要处理自己的消息
  104 + //本demo操作是在发送消息成功后,将发送消息结果监听中的message添加到listview中
137 105  
138 106 mHandler.post(new Runnable() {
139 107 @Override
... ... @@ -151,6 +119,26 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
151 119 }
152 120 };
153 121  
  122 + /**
  123 + * 发送消息结果监听
  124 + * 在onSuccess中返回发送的消息体
  125 + */
  126 + private IChat.OnResultListener onResultListener = new IChat.OnResultListener() {
  127 + @Override
  128 + public void onSuccess(CNMessage message) {
  129 + Log.i(TAG, "ResultListener SUCCESS " + message.getContent());
  130 + cnMessage = message;
  131 + }
  132 +
  133 + @Override
  134 + public void onError(int what, String extra) {
  135 + Log.e(TAG, "ResultListener errorCode : " + what + " errorMessage : " + extra);
  136 + }
  137 + };
  138 +
  139 + /**
  140 + * 加入或退出聊天室监听
  141 + */
154 142 private IChat.OnOperationListener onOperationListener = new IChat.OnOperationListener() {
155 143 @Override
156 144 public void onSuccess(int what, String extra) {
... ... @@ -160,6 +148,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
160 148 break;
161 149 case IChat.SUCCESS_QUIT_CHATROOM:
162 150 Log.i(TAG, "退出聊天室成功");
  151 + //断开连接
163 152 ChatUtil.disconnect(false);
164 153 break;
165 154 }
... ... @@ -171,16 +160,104 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
171 160 }
172 161 };
173 162  
174   - private IChat.OnResultListener onResultListener = new IChat.OnResultListener() {
  163 + /**
  164 + * 接收消息监听
  165 + */
  166 + private IChat.OnMessageListener onMessageListener = new IChat.OnMessageListener() {
175 167 @Override
176   - public void onSuccess(CNMessage message) {
177   - Log.i(TAG, "ResultListener SUCCESS " + message.getContent());
178   - cnMessage = message;
  168 + public void processMessage(final CNMessage message) {
  169 + Log.e(TAG, message.getUserInfo().getUserName() + " : " + message.getContent());
  170 +
  171 + mHandler.post(new Runnable() {
  172 + @Override
  173 + public void run() {
  174 + messageList.add(message);
  175 + chatListAdapter.updateItems(messageList);
  176 + chatListAdapter.notifyDataSetChanged();
  177 + }
  178 + });
  179 + }
  180 + };
  181 +
  182 + /**
  183 + * 连接状态监听
  184 + */
  185 + private IChat.OnConnectStatusListener onConnectStatusListener = new IChat.OnConnectStatusListener() {
  186 + @Override
  187 + public void onConnectStatus(int what, String extra) {
  188 + Log.e(TAG, "errorCode : " + what + " errorMessage : " + extra);
  189 + switch (what) {
  190 + case IChat.CONNECTED:
  191 + //连接成功
  192 + break;
  193 + case IChat.CONNECTING:
  194 + //连接中
  195 + break;
  196 + case IChat.DISCONNECTED:
  197 + //断开连接
  198 + break;
  199 + case IChat.NETWORK_UNAVAILABLE:
  200 + //网络不可以
  201 + break;
  202 + case IChat.ERROR_TOKEN_INCORRECT:
  203 + //Token错误
  204 + break;
  205 + case IChat.SERVER_INVALID:
  206 + //服务器无效
  207 + break;
  208 + case IChat.KICKED_OFFLINE_BY_OTHER_CLIENT:
  209 + //用户在其他设备登录,本机会被踢掉线
  210 + break;
  211 + }
  212 + }
  213 + };
  214 +
  215 + /**
  216 + * 聊天室动作监听
  217 + */
  218 + private IChat.OnChatRoomActionListener onChatRoomActionListener = new IChat.OnChatRoomActionListener() {
  219 + @Override
  220 + public void onJoining(String s) {
  221 + //加入中。。。
  222 + Log.i(TAG, s);
  223 + }
  224 +
  225 + @Override
  226 + public void onJoined(String s) {
  227 + //成功加入聊天室
  228 + Log.i(TAG, s);
  229 + }
  230 +
  231 + @Override
  232 + public void onQuited(String s) {
  233 + //退出聊天室
  234 + Log.i(TAG, s);
  235 + }
  236 +
  237 + @Override
  238 + public void onError(String s, int errorCode, String errorMessage) {
  239 + Log.e(TAG, s + " errorCode : " + errorCode + " errorMessage : " + errorMessage);
  240 + }
  241 + };
  242 +
  243 + /**
  244 + * 连接服务器监听
  245 + */
  246 + protected IChat.OnConnectListener onConnectListener = new IChat.OnConnectListener() {
  247 + @Override
  248 + public void onTokenIncorrect() {
  249 + //Token错误
  250 + Log.e(TAG, "ConnectListener TokenIncorrect");
  251 + }
  252 +
  253 + @Override
  254 + public void onSuccess(String s) {
  255 + Log.i(TAG, "ConnectListener " + s);
179 256 }
180 257  
181 258 @Override
182 259 public void onError(int what, String extra) {
183   - Log.e(TAG, "ResultListener errorCode : " + what + " errorMessage : " + extra);
  260 + Log.e(TAG, "ConnectListener errorCode : " + what + " : errorMessage : " + extra);
184 261 }
185 262 };
186 263  
... ... @@ -192,6 +269,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
192 269 @Override
193 270 public void onBackPressed() {
194 271 super.onBackPressed();
  272 + //退出聊天室
195 273 ChatUtil.quitChatRoom();
196 274 }
197 275 }
... ...
app/src/main/java/com/cnlive/chat/ui/MainActivity.java
... ... @@ -21,15 +21,11 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
21 21  
22 22 private Button chatRoom;
23 23 private Button privateChat;
24   - private CNUserInfo userInfo;
25 24  
26 25 @Override
27 26 protected void onCreate(Bundle savedInstanceState) {
28 27 super.onCreate(savedInstanceState);
29 28 setContentView(R.layout.activity_main);
30   - ChatUtil.setOnConnectionListener(onConnectListener);
31   -// ChatUtil.connect("WAkcsLgCj23M30nGybKzncw4SBtlLkK9SlzzuzBtTglso17GA6CGh1e++CukcOkNsdtWdRpLlYwva7BX3uQS5g==");
32   - userInfo = new CNUserInfo("2017010102", "J", "http://7xs9j5.com1.z0.glb.clouddn.com/liveapp/guojunwang.jpg");
33 29 initView();
34 30 }
35 31  
... ... @@ -45,8 +41,6 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
45 41 public void onClick(View v) {
46 42 switch (v.getId()) {
47 43 case R.id.chat_room:
48   - ChatUtil.connect(userInfo);
49   -
50 44 Intent intent = new Intent(this, ChatRoomActivity.class);
51 45 startActivity(intent);
52 46 break;
... ... @@ -56,21 +50,4 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
56 50 break;
57 51 }
58 52 }
59   -
60   - protected IChat.OnConnectListener onConnectListener = new IChat.OnConnectListener() {
61   - @Override
62   - public void onTokenIncorrect() {
63   - Log.e(TAG, "ConnectListener TokenIncorrect");
64   - }
65   -
66   - @Override
67   - public void onSuccess(String s) {
68   - Log.i(TAG, "ConnectListener " + s);
69   - }
70   -
71   - @Override
72   - public void onError(int what, String extra) {
73   - Log.e(TAG, "ConnectListener errorCode : " + what + " : errorMessage : " + extra);
74   - }
75   - };
76 53 }
... ...
app/src/main/java/com/cnlive/chat/ui/PrivateChatActivity.java
1 1 package com.cnlive.chat.ui;
2 2  
3   -import android.content.Intent;
4   -import android.net.Uri;
5 3 import android.os.Handler;
6 4 import android.support.v7.app.AppCompatActivity;
7 5 import android.os.Bundle;
8   -import android.support.v7.widget.ListViewCompat;
9 6 import android.util.Log;
10 7 import android.view.View;
11 8 import android.widget.Button;
... ... @@ -19,14 +16,12 @@ import java.util.ArrayList;
19 16 import java.util.Collections;
20 17 import java.util.List;
21 18  
22   -import io.rong.imlib.RongIMClient;
23 19 import io.rong.imlib.model.CNMessage;
24 20 import io.rong.imlib.ChatUtil;
25 21 import io.rong.imlib.base.IChat;
26 22 import io.rong.imlib.model.CNUserInfo;
27   -import io.rong.imlib.model.UserInfo;
28 23  
29   -public class PrivateChatActivity extends AppCompatActivity implements View.OnClickListener, IChat.OnMessageListener, IChat.OnConnectStatusListener {
  24 +public class PrivateChatActivity extends AppCompatActivity implements View.OnClickListener {
30 25  
31 26 private static final String TAG = "PrivateChatActivity";
32 27 private ListView chatListView;
... ... @@ -46,9 +41,10 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
46 41 @Override
47 42 protected void onCreate(Bundle savedInstanceState) {
48 43 super.onCreate(savedInstanceState);
49   - setContentView(R.layout.activity_chat_room);
  44 + setContentView(R.layout.activity_private_chat);
50 45  
51 46 userInfo = new CNUserInfo("2017010102", "J", "http://7xs9j5.com1.z0.glb.clouddn.com/liveapp/guojunwang.jpg");
  47 + //连接服务器
52 48 ChatUtil.connect(userInfo);
53 49  
54 50 targetId = "2017010101";
... ... @@ -56,17 +52,17 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
56 52 ChatUtil.setOnResultListener(onResultListener);
57 53 ChatUtil.setOnSendMessageListener(onSendMessageListener);
58 54 ChatUtil.setOnLatestMessageListener(latestMessageListener);
59   - ChatUtil.getLatestMessages(targetId, 20);
60 55 initView();
61   - ChatUtil.setOnMessageListener(this);
62   - ChatUtil.setConnectStatusListener(this);
  56 + ChatUtil.setOnMessageListener(onMessageListener);
  57 + ChatUtil.setConnectStatusListener(onConnectStatusListener);
  58 + ChatUtil.setOnConnectionListener(onConnectListener);
63 59 // Intent intent = getIntent();
64 60 // getPushMessage(intent);
65 61 }
66 62  
67 63  
68 64 private void initView() {
69   - chatListView = (ListView) findViewById(R.id.chat_list_view);
  65 + chatListView = (ListView) findViewById(R.id.private_list_view);
70 66 chatListAdapter = new ChatListAdapter(this);
71 67 chatListView.setAdapter(chatListAdapter);
72 68 messageContent = (EditText) findViewById(R.id.message_content);
... ... @@ -84,22 +80,21 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
84 80 public void onClick(View v) {
85 81 switch (v.getId()) {
86 82 case R.id.send_message:
  83 + //发送私聊消息,需要传入目标ID与发送的消息内容
87 84 ChatUtil.sendPrivateMessage(targetId, messageContent.getText().toString());
88 85 messageContent.setText("");
89 86 break;
90 87 }
91 88 }
92 89  
93   - @Override
94   - public void onConnectStatus(int what, String extra) {
95   - Log.e("DemoView", "errorCode : " + what + " errorMessage : " + extra);
96   -
97   - }
98   -
  90 + /**
  91 + * 发送消息监听
  92 + */
99 93 private IChat.OnSendMessageListener onSendMessageListener = new IChat.OnSendMessageListener() {
100 94 @Override
101 95 public void onSuccess(Integer integer) {
102   -
  96 + //聊天SDK屏蔽用户自己发送的消息,所以在发送消息成功的时候需要根据需要处理自己的消息
  97 + //本demo操作是在发送消息成功后,将发送消息结果监听中的message添加到listview中
103 98 mHandler.post(new Runnable() {
104 99 @Override
105 100 public void run() {
... ... @@ -108,7 +103,6 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
108 103 chatListAdapter.notifyDataSetChanged();
109 104 }
110 105 });
111   - Log.i(TAG, "SendMessageListener SUCCESS");
112 106 }
113 107  
114 108 @Override
... ... @@ -117,6 +111,10 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
117 111 }
118 112 };
119 113  
  114 + /**
  115 + * 发送消息结果监听
  116 + * 在onSuccess中返回发送的消息体
  117 + */
120 118 private IChat.OnResultListener onResultListener = new IChat.OnResultListener() {
121 119 @Override
122 120 public void onSuccess(CNMessage message) {
... ... @@ -130,7 +128,9 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
130 128 }
131 129 };
132 130  
133   - //单聊的历史消息,按照时间顺序从新到旧排列
  131 + /**
  132 + * 获取单聊的历史消息监听,消息按照时间顺序从新到旧排列
  133 + */
134 134 private IChat.OnLatestMessageListener latestMessageListener = new IChat.OnLatestMessageListener() {
135 135 @Override
136 136 public void onSuccess(final List<CNMessage> messages) {
... ... @@ -155,19 +155,79 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
155 155 }
156 156 };
157 157  
158   - @Override
159   - public void processMessage(final CNMessage message) {
160   - Log.e("DemoView", message.getContent());
161   -
162   - mHandler.post(new Runnable() {
163   - @Override
164   - public void run() {
165   - messageList.add(message);
166   - chatListAdapter.updateItems(messageList);
167   - chatListAdapter.notifyDataSetChanged();
  158 + /**
  159 + * 连接服务器监听
  160 + */
  161 + protected IChat.OnConnectListener onConnectListener = new IChat.OnConnectListener() {
  162 + @Override
  163 + public void onTokenIncorrect() {
  164 + Log.e(TAG, "ConnectListener TokenIncorrect");
  165 + }
  166 +
  167 + @Override
  168 + public void onSuccess(String s) {
  169 + Log.i(TAG, "ConnectListener " + s);
  170 + //聊天SDK若想获得聊天历史,需要调用此方法,需要传入目标ID与获取消息数量
  171 + ChatUtil.getLatestMessages(targetId, 20);
  172 + }
  173 +
  174 + @Override
  175 + public void onError(int what, String extra) {
  176 + Log.e(TAG, "ConnectListener errorCode : " + what + " : errorMessage : " + extra);
  177 + }
  178 + };
  179 +
  180 + /**
  181 + * 接收消息监听
  182 + */
  183 + private IChat.OnMessageListener onMessageListener = new IChat.OnMessageListener() {
  184 + @Override
  185 + public void processMessage(final CNMessage message) {
  186 + Log.e(TAG, message.getContent());
  187 +
  188 + mHandler.post(new Runnable() {
  189 + @Override
  190 + public void run() {
  191 + messageList.add(message);
  192 + chatListAdapter.updateItems(messageList);
  193 + chatListAdapter.notifyDataSetChanged();
  194 + }
  195 + });
  196 + }
  197 + };
  198 +
  199 + /**
  200 + * 连接状态监听
  201 + */
  202 + private IChat.OnConnectStatusListener onConnectStatusListener = new IChat.OnConnectStatusListener() {
  203 + @Override
  204 + public void onConnectStatus(int what, String extra) {
  205 + Log.e(TAG, "errorCode : " + what + " errorMessage : " + extra);
  206 + switch (what) {
  207 + case IChat.CONNECTED:
  208 + //连接成功
  209 + break;
  210 + case IChat.CONNECTING:
  211 + //连接中
  212 + break;
  213 + case IChat.DISCONNECTED:
  214 + //断开连接
  215 + break;
  216 + case IChat.NETWORK_UNAVAILABLE:
  217 + //网络不可以
  218 + break;
  219 + case IChat.ERROR_TOKEN_INCORRECT:
  220 + //Token错误
  221 + break;
  222 + case IChat.SERVER_INVALID:
  223 + //服务器无效
  224 + break;
  225 + case IChat.KICKED_OFFLINE_BY_OTHER_CLIENT:
  226 + //用户在其他设备登录,本机会被踢掉线
  227 + break;
168 228 }
169   - });
170   - }
  229 + }
  230 + };
171 231  
172 232 @Override
173 233 protected void onDestroy() {
... ... @@ -191,6 +251,7 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
191 251 @Override
192 252 public void onBackPressed() {
193 253 super.onBackPressed();
  254 + //断开连接 传入true时,可以接收push消息;传入false时,不能接收push消息
194 255 ChatUtil.disconnect(true);
195 256 }
196 257 }
... ...
app/src/main/res/layout/activity_chat_room.xml
... ... @@ -14,19 +14,13 @@
14 14 android:id="@+id/chat_list_view"
15 15 android:layout_width="match_parent"
16 16 android:layout_height="300dp"
  17 + android:layout_above="@+id/edit_layout"
17 18 android:divider="@null"
18 19 android:scrollbars="none"
19 20 android:transcriptMode="alwaysScroll" />
20 21  
21   - <Button
22   - android:id="@+id/change_user_info"
23   - android:layout_width="match_parent"
24   - android:layout_height="wrap_content"
25   - android:layout_below="@id/chat_list_view"
26   - android:layout_marginTop="10dp"
27   - android:text="更新用户信息" />
28   -
29 22 <LinearLayout
  23 + android:id="@+id/edit_layout"
30 24 android:layout_width="match_parent"
31 25 android:layout_height="wrap_content"
32 26 android:layout_alignParentBottom="true"
... ... @@ -48,4 +42,11 @@
48 42 android:text="发送" />
49 43 </LinearLayout>
50 44  
  45 + <!--<Button-->
  46 + <!--android:id="@+id/change_user_info"-->
  47 + <!--android:layout_width="match_parent"-->
  48 + <!--android:layout_height="wrap_content"-->
  49 + <!--android:layout_above="@+id/chat_list_view"-->
  50 + <!--android:text="更新用户信息" />-->
  51 +
51 52 </RelativeLayout>
... ...
app/src/main/res/layout/activity_private_chat.xml
... ... @@ -10,10 +10,36 @@
10 10 android:paddingTop="@dimen/activity_vertical_margin"
11 11 tools:context="com.cnlive.chat.ui.PrivateChatActivity">
12 12  
13   - <TextView
14   - android:layout_width="wrap_content"
  13 + <ListView
  14 + android:id="@+id/private_list_view"
  15 + android:layout_width="match_parent"
  16 + android:layout_height="300dp"
  17 + android:layout_above="@+id/edit_layout"
  18 + android:divider="@null"
  19 + android:scrollbars="none"
  20 + android:transcriptMode="alwaysScroll" />
  21 +
  22 + <LinearLayout
  23 + android:id="@+id/edit_layout"
  24 + android:layout_width="match_parent"
15 25 android:layout_height="wrap_content"
16   - android:layout_centerInParent="true"
17   - android:text="我是单聊" />
  26 + android:layout_alignParentBottom="true"
  27 + android:orientation="horizontal">
  28 +
  29 + <EditText
  30 + android:id="@+id/message_content"
  31 + android:layout_width="0dp"
  32 + android:layout_height="wrap_content"
  33 + android:layout_gravity="center"
  34 + android:layout_weight="0.7" />
  35 +
  36 + <Button
  37 + android:id="@+id/send_message"
  38 + android:layout_width="0dp"
  39 + android:layout_height="wrap_content"
  40 + android:layout_gravity="center"
  41 + android:layout_weight="0.2"
  42 + android:text="发送" />
  43 + </LinearLayout>
18 44  
19 45 </RelativeLayout>
... ...