Commit 09dc93ae95c88671b90064c67f23ab4e342362bb
1 parent
be545209
修改聊天demo
Showing
3 changed files
with
63 additions
and
46 deletions
app/src/main/java/com/cnlive/im/ui/ChatRoomActivity.java
| @@ -36,7 +36,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | @@ -36,7 +36,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | ||
| 36 | private ListView listViewOne; | 36 | private ListView listViewOne; |
| 37 | private ChatListAdapter chatListAdapterOne, chatListAdapterTwo; | 37 | private ChatListAdapter chatListAdapterOne, chatListAdapterTwo; |
| 38 | private ListView listViewTwo; | 38 | private ListView listViewTwo; |
| 39 | - private Button user_switchBtn, reconnectionBtn, switch_visitorsBtn, foundChatBtn, disconnectBtn; | 39 | + private Button user_switchBtn, foundChatBtn; |
| 40 | private Button creatChat2Btn; | 40 | private Button creatChat2Btn; |
| 41 | private Handler mHandler; | 41 | private Handler mHandler; |
| 42 | private RelativeLayout re_chat; | 42 | private RelativeLayout re_chat; |
| @@ -72,10 +72,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | @@ -72,10 +72,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | ||
| 72 | lin_user = (LinearLayout) findViewById(R.id.lin_user); | 72 | lin_user = (LinearLayout) findViewById(R.id.lin_user); |
| 73 | re_chat = (RelativeLayout) findViewById(R.id.re_chat); | 73 | re_chat = (RelativeLayout) findViewById(R.id.re_chat); |
| 74 | creatChat2Btn = (Button) findViewById(R.id.creatChat2); | 74 | creatChat2Btn = (Button) findViewById(R.id.creatChat2); |
| 75 | - disconnectBtn = (Button) findViewById(R.id.disconnect); | ||
| 76 | foundChatBtn = (Button) findViewById(R.id.creatChat); | 75 | foundChatBtn = (Button) findViewById(R.id.creatChat); |
| 77 | - switch_visitorsBtn = (Button) findViewById(R.id.switch_visitors); | ||
| 78 | - reconnectionBtn = (Button) findViewById(R.id.connectionBtn); | ||
| 79 | user_switchBtn = (Button) findViewById(R.id.user_switch); | 76 | user_switchBtn = (Button) findViewById(R.id.user_switch); |
| 80 | listViewOne = (ListView) findViewById(R.id.chat_list_viewOne); | 77 | listViewOne = (ListView) findViewById(R.id.chat_list_viewOne); |
| 81 | listViewTwo = (ListView) findViewById(R.id.chat_list_viewTwo); | 78 | listViewTwo = (ListView) findViewById(R.id.chat_list_viewTwo); |
| @@ -85,13 +82,10 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | @@ -85,13 +82,10 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | ||
| 85 | exit_chat1.setOnClickListener(this); | 82 | exit_chat1.setOnClickListener(this); |
| 86 | exit_chat2.setOnClickListener(this); | 83 | exit_chat2.setOnClickListener(this); |
| 87 | user_imfor_send.setOnClickListener(this); | 84 | user_imfor_send.setOnClickListener(this); |
| 88 | - disconnectBtn.setOnClickListener(this); | ||
| 89 | foundChatBtn.setOnClickListener(this); | 85 | foundChatBtn.setOnClickListener(this); |
| 90 | send_messageOneBtn.setOnClickListener(this); | 86 | send_messageOneBtn.setOnClickListener(this); |
| 91 | send_messageOneTwo.setOnClickListener(this); | 87 | send_messageOneTwo.setOnClickListener(this); |
| 92 | - reconnectionBtn.setOnClickListener(this); | ||
| 93 | user_switchBtn.setOnClickListener(this); | 88 | user_switchBtn.setOnClickListener(this); |
| 94 | - switch_visitorsBtn.setOnClickListener(this); | ||
| 95 | creatChat2Btn.setOnClickListener(this); | 89 | creatChat2Btn.setOnClickListener(this); |
| 96 | groupChatOneListMy = new ArrayList<>(); | 90 | groupChatOneListMy = new ArrayList<>(); |
| 97 | groupChatTwoListMy = new ArrayList<>(); | 91 | groupChatTwoListMy = new ArrayList<>(); |
| @@ -114,7 +108,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | @@ -114,7 +108,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | ||
| 114 | @Override | 108 | @Override |
| 115 | public void onSuccess(String s) { | 109 | public void onSuccess(String s) { |
| 116 | Log.e(TAG, "ConnectListener 连接监听" + s); | 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 | @Override | 114 | @Override |
| @@ -241,7 +235,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | @@ -241,7 +235,6 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | ||
| 241 | @Override | 235 | @Override |
| 242 | public void onQuited(String s) { | 236 | public void onQuited(String s) { |
| 243 | Log.i(TAG, "chatRoomActionListener " + "onQuited" + s); | 237 | Log.i(TAG, "chatRoomActionListener " + "onQuited" + s); |
| 244 | - | ||
| 245 | } | 238 | } |
| 246 | 239 | ||
| 247 | @Override | 240 | @Override |
| @@ -269,7 +262,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | @@ -269,7 +262,7 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | ||
| 269 | private IChat.OnOperationListener onOperationListener = new IChat.OnOperationListener() { | 262 | private IChat.OnOperationListener onOperationListener = new IChat.OnOperationListener() { |
| 270 | @Override | 263 | @Override |
| 271 | public void onSuccess(int what, final String extra) { | 264 | public void onSuccess(int what, final String extra) { |
| 272 | - Log.v(TAG, + what + extra); | 265 | + Log.v(TAG, +what + extra); |
| 273 | mHandler.post(new Runnable() { | 266 | mHandler.post(new Runnable() { |
| 274 | @Override | 267 | @Override |
| 275 | public void run() { | 268 | public void run() { |
| @@ -312,27 +305,12 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | @@ -312,27 +305,12 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | ||
| 312 | break; | 305 | break; |
| 313 | // 用户 | 306 | // 用户 |
| 314 | case R.id.user_switch: | 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 | break; | 310 | break; |
| 328 | // 创建聊天 | 311 | // 创建聊天 |
| 329 | case R.id.creatChat: | 312 | case R.id.creatChat: |
| 330 | ChatUtil.joinChatRoom(roomIdOne, -1, onOperationListener); | 313 | ChatUtil.joinChatRoom(roomIdOne, -1, onOperationListener); |
| 331 | - break; | ||
| 332 | - // 断开连接 | ||
| 333 | - case R.id.disconnect: | ||
| 334 | - ChatUtil.disconnect(false); | ||
| 335 | - break; | ||
| 336 | //加入聊天室2 | 314 | //加入聊天室2 |
| 337 | case R.id.creatChat2: | 315 | case R.id.creatChat2: |
| 338 | ChatUtil.joinChatRoom(roomIdTwo, -1, onOperationListener); | 316 | ChatUtil.joinChatRoom(roomIdTwo, -1, onOperationListener); |
| @@ -354,21 +332,13 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | @@ -354,21 +332,13 @@ public class ChatRoomActivity extends AppCompatActivity implements View.OnClickL | ||
| 354 | Toast.makeText(ChatRoomActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show(); | 332 | Toast.makeText(ChatRoomActivity.this, "用户名不能为空", Toast.LENGTH_SHORT).show(); |
| 355 | else { | 333 | else { |
| 356 | cnUser = new CNUserInfo(user_id.getText().toString(), user_name.getText().toString(), ""); | 334 | cnUser = new CNUserInfo(user_id.getText().toString(), user_name.getText().toString(), ""); |
| 357 | - lin_user.setVisibility(View.INVISIBLE); | 335 | + ChatUtil.login(cnUser, connectListener); |
| 358 | re_chat.setVisibility(View.VISIBLE); | 336 | re_chat.setVisibility(View.VISIBLE); |
| 337 | + lin_user.setVisibility(View.INVISIBLE); | ||
| 359 | } | 338 | } |
| 360 | break; | 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,15 +3,24 @@ package com.cnlive.im.ui; | ||
| 3 | import android.content.Intent; | 3 | import android.content.Intent; |
| 4 | import android.os.Bundle; | 4 | import android.os.Bundle; |
| 5 | import android.support.v7.app.AppCompatActivity; | 5 | import android.support.v7.app.AppCompatActivity; |
| 6 | +import android.util.Log; | ||
| 6 | import android.view.View; | 7 | import android.view.View; |
| 7 | import android.widget.Button; | 8 | import android.widget.Button; |
| 9 | +import android.widget.Toast; | ||
| 8 | 10 | ||
| 9 | import com.cnlive.im.R; | 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 | public class MainActivity extends AppCompatActivity implements View.OnClickListener { | 18 | public class MainActivity extends AppCompatActivity implements View.OnClickListener { |
| 13 | 19 | ||
| 14 | private Button groupChatBtn, privateChat; | 20 | private Button groupChatBtn, privateChat; |
| 21 | + private String TAG = "MainActivity"; | ||
| 22 | + private Button connectBtn; | ||
| 23 | + private Button re_connect; | ||
| 15 | 24 | ||
| 16 | @Override | 25 | @Override |
| 17 | protected void onCreate(Bundle savedInstanceState) { | 26 | protected void onCreate(Bundle savedInstanceState) { |
| @@ -20,12 +29,40 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe | @@ -20,12 +29,40 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe | ||
| 20 | initUI(); | 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 | private void initUI() { | 59 | private void initUI() { |
| 24 | groupChatBtn = (Button) findViewById(R.id.groupChat); | 60 | groupChatBtn = (Button) findViewById(R.id.groupChat); |
| 25 | privateChat = (Button) findViewById(R.id.privateChat); | 61 | privateChat = (Button) findViewById(R.id.privateChat); |
| 62 | + connectBtn = (Button) findViewById(connect); | ||
| 26 | groupChatBtn.setOnClickListener(this); | 63 | groupChatBtn.setOnClickListener(this); |
| 27 | privateChat.setOnClickListener(this); | 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,6 +77,12 @@ public class MainActivity extends AppCompatActivity implements View.OnClickListe | ||
| 40 | Intent intent1 = new Intent(MainActivity.this, PrivateChatActivity.class); | 77 | Intent intent1 = new Intent(MainActivity.this, PrivateChatActivity.class); |
| 41 | startActivity(intent1); | 78 | startActivity(intent1); |
| 42 | break; | 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,6 +47,9 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli | ||
| 47 | private String my_id_editID; | 47 | private String my_id_editID; |
| 48 | private LinearLayout my_liner_userID; | 48 | private LinearLayout my_liner_userID; |
| 49 | private Handler mHandler; | 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 | @Override | 54 | @Override |
| 52 | protected void onCreate(Bundle savedInstanceState) { | 55 | protected void onCreate(Bundle savedInstanceState) { |
| @@ -56,11 +59,12 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli | @@ -56,11 +59,12 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli | ||
| 56 | mHandler = new Handler(); | 59 | mHandler = new Handler(); |
| 57 | ChatUtil.setConnectStatusListener(connectStatusListener); | 60 | ChatUtil.setConnectStatusListener(connectStatusListener); |
| 58 | ChatUtil.setOnReceiveMessageListener(receiveMessageListener); | 61 | ChatUtil.setOnReceiveMessageListener(receiveMessageListener); |
| 59 | - ChatUtil.connect(connectListener); | ||
| 60 | 62 | ||
| 61 | } | 63 | } |
| 62 | 64 | ||
| 63 | private void initView() { | 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 | chatListView = (ListView) findViewById(R.id.private_list_view); | 68 | chatListView = (ListView) findViewById(R.id.private_list_view); |
| 65 | chatListAdapter = new ChatListAdapter(this); | 69 | chatListAdapter = new ChatListAdapter(this); |
| 66 | chatListView.setAdapter(chatListAdapter); | 70 | chatListView.setAdapter(chatListAdapter); |
| @@ -96,15 +100,20 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli | @@ -96,15 +100,20 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli | ||
| 96 | Toast.makeText(this, "对方ID不能为空", Toast.LENGTH_SHORT).show(); | 100 | Toast.makeText(this, "对方ID不能为空", Toast.LENGTH_SHORT).show(); |
| 97 | } else if (my_id_edit.getText().toString().isEmpty()) { | 101 | } else if (my_id_edit.getText().toString().isEmpty()) { |
| 98 | Toast.makeText(this, "你的ID不能为空", Toast.LENGTH_SHORT).show(); | 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 | } else { | 107 | } else { |
| 100 | user_id_edit.getText().toString(); | 108 | user_id_edit.getText().toString(); |
| 101 | oppositeID = user_id_edit.getText().toString(); | 109 | oppositeID = user_id_edit.getText().toString(); |
| 102 | my_id_editID = my_id_edit.getText().toString(); | 110 | my_id_editID = my_id_edit.getText().toString(); |
| 111 | + my_name_edit1 = my_name_edit.getText().toString(); | ||
| 103 | linearLayoutUseID.setVisibility(View.INVISIBLE); | 112 | linearLayoutUseID.setVisibility(View.INVISIBLE); |
| 104 | edit_layout.setVisibility(View.VISIBLE); | 113 | edit_layout.setVisibility(View.VISIBLE); |
| 105 | chatListView.setVisibility(View.VISIBLE); | 114 | chatListView.setVisibility(View.VISIBLE); |
| 106 | my_liner_userID.setVisibility(View.INVISIBLE); | 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 | ChatUtil.login(cnName, connectListener); | 117 | ChatUtil.login(cnName, connectListener); |
| 109 | } | 118 | } |
| 110 | 119 | ||
| @@ -125,7 +134,7 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli | @@ -125,7 +134,7 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli | ||
| 125 | @Override | 134 | @Override |
| 126 | public void onSuccess(String s) { | 135 | public void onSuccess(String s) { |
| 127 | Log.e(TAG, "ConnectListener 连接监听" + s); | 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 | @Override | 140 | @Override |
| @@ -205,10 +214,5 @@ public class PrivateChatActivity extends AppCompatActivity implements View.OnCli | @@ -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 |