Commit 09dc93ae95c88671b90064c67f23ab4e342362bb

Authored by 陈硕
1 parent be545209

修改聊天demo

app/src/main/java/com/cnlive/im/ui/ChatRoomActivity.java
... ... @@ -36,7 +36,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
36 36 private ListView listViewOne;
37 37 private ChatListAdapter chatListAdapterOne, chatListAdapterTwo;
38 38 private ListView listViewTwo;
39   - private Button user_switchBtn, reconnectionBtn, switch_visitorsBtn, foundChatBtn, disconnectBtn;
  39 + private Button user_switchBtn, foundChatBtn;
40 40 private Button creatChat2Btn;
41 41 private Handler mHandler;
42 42 private RelativeLayout re_chat;
... ... @@ -72,10 +72,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
72 72 lin_user = (LinearLayout) findViewById(R.id.lin_user);
73 73 re_chat = (RelativeLayout) findViewById(R.id.re_chat);
74 74 creatChat2Btn = (Button) findViewById(R.id.creatChat2);
75   - disconnectBtn = (Button) findViewById(R.id.disconnect);
76 75 foundChatBtn = (Button) findViewById(R.id.creatChat);
77   - switch_visitorsBtn = (Button) findViewById(R.id.switch_visitors);
78   - reconnectionBtn = (Button) findViewById(R.id.connectionBtn);
79 76 user_switchBtn = (Button) findViewById(R.id.user_switch);
80 77 listViewOne = (ListView) findViewById(R.id.chat_list_viewOne);
81 78 listViewTwo = (ListView) findViewById(R.id.chat_list_viewTwo);
... ... @@ -85,13 +82,10 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
85 82 exit_chat1.setOnClickListener(this);
86 83 exit_chat2.setOnClickListener(this);
87 84 user_imfor_send.setOnClickListener(this);
88   - disconnectBtn.setOnClickListener(this);
89 85 foundChatBtn.setOnClickListener(this);
90 86 send_messageOneBtn.setOnClickListener(this);
91 87 send_messageOneTwo.setOnClickListener(this);
92   - reconnectionBtn.setOnClickListener(this);
93 88 user_switchBtn.setOnClickListener(this);
94   - switch_visitorsBtn.setOnClickListener(this);
95 89 creatChat2Btn.setOnClickListener(this);
96 90 groupChatOneListMy = new ArrayList<>();
97 91 groupChatTwoListMy = new ArrayList<>();
... ... @@ -114,7 +108,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
114 108 @Override
115 109 public void onSuccess(String s) {
116 110 Log.e(TAG, "ConnectListener 连接监听" + s);
117   - Toast.makeText(ChatRoomActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
  111 + //Toast.makeText(ChatRoomActivity.this, s, Toast.LENGTH_SHORT).show();
118 112 }
119 113  
120 114 @Override
... ... @@ -241,7 +235,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
241 235 @Override
242 236 public void onQuited(String s) {
243 237 Log.i(TAG, "chatRoomActionListener " + "onQuited" + s);
244   -
245 238 }
246 239  
247 240 @Override
... ... @@ -269,7 +262,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
269 262 private IChat.OnOperationListener onOperationListener = new IChat.OnOperationListener() {
270 263 @Override
271 264 public void onSuccess(int what, final String extra) {
272   - Log.v(TAG, + what + extra);
  265 + Log.v(TAG, +what + extra);
273 266 mHandler.post(new Runnable() {
274 267 @Override
275 268 public void run() {
... ... @@ -312,27 +305,12 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
312 305 break;
313 306 // 用户
314 307 case R.id.user_switch:
315   - ChatUtil.login(cnUser, connectListener);
316   - break;
317   - //连接
318   - case R.id.connectionBtn:
319   - ChatUtil.connect(connectListener);
320   - break;
321   -
322   - // 切换成游客
323   - case R.id.switch_visitors:
324   -
325   -
326   - ChatUtil.logOut(connectListener);
  308 + re_chat.setVisibility(View.INVISIBLE);
  309 + lin_user.setVisibility(View.VISIBLE);
327 310 break;
328 311 // 创建聊天
329 312 case R.id.creatChat:
330 313 ChatUtil.joinChatRoom(roomIdOne, -1, onOperationListener);
331   - break;
332   - // 断开连接
333   - case R.id.disconnect:
334   - ChatUtil.disconnect(false);
335   - break;
336 314 //加入聊天室2
337 315 case R.id.creatChat2:
338 316 ChatUtil.joinChatRoom(roomIdTwo, -1, onOperationListener);
... ... @@ -354,21 +332,13 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL
354 332 Toast.makeText(ChatRoomActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show();
355 333 else {
356 334 cnUser = new CNUserInfo(user_id.getText().toString(), user_name.getText().toString(), "");
357   - lin_user.setVisibility(View.INVISIBLE);
  335 + ChatUtil.login(cnUser, connectListener);
358 336 re_chat.setVisibility(View.VISIBLE);
  337 + lin_user.setVisibility(View.INVISIBLE);
359 338 }
360 339 break;
361   -
362   -
363 340 }
364 341 }
365 342  
366   - @Override
367   - protected void onPause() {
368   - super.onPause();
369   - ChatUtil.quitChatRoom(roomIdTwo, onOperationListener);
370   - ChatUtil.quitChatRoom(roomIdOne, onOperationListener);
371   - }
372   -
373 343  
374 344 }
... ...
app/src/main/java/com/cnlive/im/ui/MainActivity.java
... ... @@ -3,15 +3,24 @@ package com.cnlive.im.ui;
3 3 import android.content.Intent;
4 4 import android.os.Bundle;
5 5 import android.support.v7.app.AppCompatActivity;
  6 +import android.util.Log;
6 7 import android.view.View;
7 8 import android.widget.Button;
  9 +import android.widget.Toast;
8 10  
9 11 import com.cnlive.im.R;
  12 +import com.cnlive.libs.util.chat.ChatUtil;
  13 +import com.cnlive.libs.util.chat.base.IChat;
  14 +
  15 +import static com.cnlive.im.R.id.connect;
10 16  
11 17  
12 18 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
13 19  
14 20 private Button groupChatBtn, privateChat;
  21 + private String TAG = "MainActivity";
  22 + private Button connectBtn;
  23 + private Button re_connect;
15 24  
16 25 @Override
17 26 protected void onCreate(Bundle savedInstanceState) {
... ... @@ -20,12 +29,40 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
20 29 initUI();
21 30 }
22 31  
  32 + //连接监听
  33 + private IChat.OnConnectListener connectListener = new IChat.OnConnectListener() {
  34 + @Override
  35 + public void onTokenIncorrect() {
  36 + Log.e(TAG, "ConnectListener TokenIncorrect");
  37 + Toast.makeText(MainActivity.this, "TokenIncorrect", Toast.LENGTH_SHORT).show();
  38 + connectBtn.setText("重新连接");
  39 + }
  40 +
  41 + @Override
  42 + public void onSuccess(String s) {
  43 + Log.e(TAG, "ConnectListener 连接监听" + s);
  44 + Toast.makeText(MainActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
  45 + connectBtn.setVisibility(View.INVISIBLE);
  46 + groupChatBtn.setVisibility(View.VISIBLE);
  47 + privateChat.setVisibility(View.VISIBLE);
  48 + }
  49 +
  50 + @Override
  51 + public void onError(int i, String s) {
  52 + Log.e(TAG, "ConnectListener errorCode : " + i + " : errorMessage : " + s);
  53 + Toast.makeText(MainActivity.this, s, Toast.LENGTH_SHORT).show();
  54 + connectBtn.setText("重新连接");
  55 +
  56 + }
  57 + };
  58 +
23 59 private void initUI() {
24 60 groupChatBtn = (Button) findViewById(R.id.groupChat);
25 61 privateChat = (Button) findViewById(R.id.privateChat);
  62 + connectBtn = (Button) findViewById(connect);
26 63 groupChatBtn.setOnClickListener(this);
27 64 privateChat.setOnClickListener(this);
28   -
  65 + connectBtn.setOnClickListener(this);
29 66 }
30 67  
31 68  
... ... @@ -40,6 +77,12 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe
40 77 Intent intent1 = new Intent(MainActivity.this, PrivateChatActivity.class);
41 78 startActivity(intent1);
42 79 break;
  80 + case connect:
  81 + //调用此方法在聊天室中不切换用户默认为游客
  82 + // 调用此方法之后不调用断开连接,网络掉线等原因系统会自动重连用户无需处理
  83 + ChatUtil.connect(connectListener);
  84 +
  85 +
43 86 }
44 87 }
45 88 }
... ...
app/src/main/java/com/cnlive/im/ui/PrivateChatActivity.java
... ... @@ -47,6 +47,9 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
47 47 private String my_id_editID;
48 48 private LinearLayout my_liner_userID;
49 49 private Handler mHandler;
  50 + private EditText opposite_name_edit;
  51 + private EditText my_name_edit;
  52 + private String my_name_edit1;
50 53  
51 54 @Override
52 55 protected void onCreate(Bundle savedInstanceState) {
... ... @@ -56,11 +59,12 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
56 59 mHandler = new Handler();
57 60 ChatUtil.setConnectStatusListener(connectStatusListener);
58 61 ChatUtil.setOnReceiveMessageListener(receiveMessageListener);
59   - ChatUtil.connect(connectListener);
60 62  
61 63 }
62 64  
63 65 private void initView() {
  66 + my_name_edit = (EditText) findViewById(R.id.my_name_edit);
  67 + opposite_name_edit = (EditText) findViewById(R.id.opposite_name_edit);
64 68 chatListView = (ListView) findViewById(R.id.private_list_view);
65 69 chatListAdapter = new ChatListAdapter(this);
66 70 chatListView.setAdapter(chatListAdapter);
... ... @@ -96,15 +100,20 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
96 100 Toast.makeText(this, "对方ID不能为空", Toast.LENGTH_SHORT).show();
97 101 } else if (my_id_edit.getText().toString().isEmpty()) {
98 102 Toast.makeText(this, "你的ID不能为空", Toast.LENGTH_SHORT).show();
  103 + } else if (my_name_edit.getText().toString().isEmpty()) {
  104 + Toast.makeText(this, "你的name不能为空", Toast.LENGTH_SHORT).show();
  105 + } else if (opposite_name_edit.getText().toString().isEmpty()) {
  106 + Toast.makeText(this, "对方name不能为空", Toast.LENGTH_SHORT).show();
99 107 } else {
100 108 user_id_edit.getText().toString();
101 109 oppositeID = user_id_edit.getText().toString();
102 110 my_id_editID = my_id_edit.getText().toString();
  111 + my_name_edit1 = my_name_edit.getText().toString();
103 112 linearLayoutUseID.setVisibility(View.INVISIBLE);
104 113 edit_layout.setVisibility(View.VISIBLE);
105 114 chatListView.setVisibility(View.VISIBLE);
106 115 my_liner_userID.setVisibility(View.INVISIBLE);
107   - CNUserInfo cnName = new CNUserInfo(my_id_editID, "cnName", "");
  116 + CNUserInfo cnName = new CNUserInfo(my_id_editID, my_name_edit1, "");
108 117 ChatUtil.login(cnName, connectListener);
109 118 }
110 119  
... ... @@ -125,7 +134,7 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
125 134 @Override
126 135 public void onSuccess(String s) {
127 136 Log.e(TAG, "ConnectListener 连接监听" + s);
128   - Toast.makeText(PrivateChatActivity.this, "连接成功", Toast.LENGTH_SHORT).show();
  137 + Toast.makeText(PrivateChatActivity.this, s, Toast.LENGTH_SHORT).show();
129 138 }
130 139  
131 140 @Override
... ... @@ -205,10 +214,5 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli
205 214 });
206 215 }
207 216 };
208   -
209   -
210   -
211   -
212   -
213 217 }
214 218  
... ...