前面两章已经了解了大部分功能,不过还有一些东西没有讲到,接下来咱们就继续将剩下的部分讲完。
@Override protected void onDestroy() { super.onDestroy(); activityInstance = null; EMGroupManager.getInstance().removeGroupChangeListener(groupListener); }
/** * 监测群组解散或者被T事件 * */ class GroupListener extends GroupReomveListener { @Override public void onUserRemoved(final String groupId, String groupName) { runOnUiThread(new Runnable() { String st13 = getResources().getString(R.string.you_are_group); public void run() { if (toChatUsername.equals(groupId)) { Toast.makeText(ChatActivity.this, st13, 1).show(); if (GroupDetailsActivity.instance != null) GroupDetailsActivity.instance.finish(); finish(); } } }); } @Override public void onGroupDestroy(final String groupId, String groupName) { // 群组解散正好在此页面,提示群组被解散,并finish此页面 runOnUiThread(new Runnable() { String st14 = getResources().getString(R.string.the_current_group); public void run() { if (toChatUsername.equals(groupId)) { Toast.makeText(ChatActivity.this, st14, 1).show(); if (GroupDetailsActivity.instance != null) GroupDetailsActivity.instance.finish(); finish(); } } }); } }声明一个类,继承GroupReomveListener类。其中有实现两个方法,一个是用户移出,一个是群组销毁。用户移除的时候结束群组界面,当群组解散的时候,如果正处于群组界面,提示一下然后结束群组界面。跳出当前界面。
@Override protected void onResume() { Log.i("ChatActivity", "onResume"); super.onResume(); if (group != null) ((TextView) findViewById(R.id.name)).setText(group.getGroupName()); adapter.refresh(); DemoHXSDKHelper sdkHelper = (DemoHXSDKHelper) DemoHXSDKHelper.getInstance(); sdkHelper.pushActivity(this); // register the event listener when enter the foreground EMChatManager.getInstance().registerEventListener(this, new EMNotifierEvent.Event[] { EMNotifierEvent.Event.EventNewMessage, EMNotifierEvent.Event.EventDeliveryAck, EMNotifierEvent.Event.EventReadAck }); }
@Override protected void onStop() { Log.i("ChatActivity", "onStop"); // unregister this event listener when this activity enters the // background EMChatManager.getInstance().unregisterEventListener(this); DemoHXSDKHelper sdkHelper = (DemoHXSDKHelper) DemoHXSDKHelper.getInstance(); // 把此activity 从foreground activity 列表里移除 sdkHelper.popActivity(this); super.onStop(); }
@Override protected void onPause() { super.onPause(); if (wakeLock.isHeld()) wakeLock.release(); if (VoicePlayClickListener.isPlaying && VoicePlayClickListener.currentPlayListener != null) { // 停止语音播放 VoicePlayClickListener.currentPlayListener.stopPlayVoice(); } try { // 停止录音 if (voiceRecorder.isRecording()) { voiceRecorder.discardRecording(); recordingContainer.setVisibility(View.INVISIBLE); } } catch (Exception e) { } }
/** * 隐藏软键盘 */ private void hideKeyboard() { if (getWindow().getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) { if (getCurrentFocus() != null) manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } }
/** * 加入到黑名单 * * @param username */ private void addUserToBlacklist(final String username) { final ProgressDialog pd = new ProgressDialog(this); pd.setMessage(getString(R.string.Is_moved_into_blacklist)); pd.setCanceledOnTouchOutside(false); pd.show(); new Thread(new Runnable() { public void run() { try { EMContactManager.getInstance().addUserToBlackList(username, false); runOnUiThread(new Runnable() { public void run() { pd.dismiss(); Toast.makeText(getApplicationContext(), R.string.Move_into_blacklist_success, 0).show(); } }); } catch (EaseMobException e) { e.printStackTrace(); runOnUiThread(new Runnable() { public void run() { pd.dismiss(); Toast.makeText(getApplicationContext(), R.string.Move_into_blacklist_failure, 0).show(); } }); } } }).start(); }
/** * 覆盖手机返回键 */ @Override public void onBackPressed() { if (more.getVisibility() == View.VISIBLE) { more.setVisibility(View.GONE); iv_emoticons_normal.setVisibility(View.VISIBLE); iv_emoticons_checked.setVisibility(View.INVISIBLE); } else { super.onBackPressed(); } }
/** * listview滑动监听listener * */ private class ListScrollListener implements OnScrollListener { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { case OnScrollListener.SCROLL_STATE_IDLE: //当第一项显示在屏幕上,并且没有再继续加载 if (view.getFirstVisiblePosition() == 0 && !isloading && haveMoreData) { isloading = true; loadmorePB.setVisibility(View.VISIBLE); // sdk初始化加载的聊天记录为20条,到顶时去db里获取更多 List<EMMessage> messages; EMMessage firstMsg = conversation.getAllMessages().get(0); try { // 获取更多messges,调用此方法的时候从db获取的messages // sdk会自动存入到此conversation中 if (chatType == CHATTYPE_SINGLE) messages = conversation.loadMoreMsgFromDB(firstMsg.getMsgId(), pagesize); else messages = conversation.loadMoreGroupMsgFromDB(firstMsg.getMsgId(), pagesize); } catch (Exception e1) { loadmorePB.setVisibility(View.GONE); return; } try { Thread.sleep(300); } catch (InterruptedException e) { } if (messages.size() != 0) { // 刷新ui if (messages.size() > 0) { adapter.refreshSeekTo(messages.size() - 1); } if (messages.size() != pagesize) haveMoreData = false; } else { haveMoreData = false; } loadmorePB.setVisibility(View.GONE); isloading = false; } break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { } }
@Override protected void onNewIntent(Intent intent) { // 点击notification bar进入聊天页面,保证只有一个聊天页面 String username = intent.getStringExtra("userId"); if (toChatUsername.equals(username)) super.onNewIntent(intent); else { finish(); startActivity(intent); } }
最后,还有一个聊天界面的UI模式,通过ListView来显示消息,由于内容比较多。所以下一章再将啦。
原文地址:http://blog.csdn.net/caowei0403/article/details/45362915