环信UI开源Demo情景分析六、主界面

终于来到正题了,这个应用最核心的地方,老规矩,先来看看清单里面的配置:

        
        
        

其中启动模式为:singleTask。

windowSoftInputMode:adjustPan。

先来看看界面:

环信UI开源Demo情景分析六、主界面_第1张图片环信UI开源Demo情景分析六、主界面_第2张图片环信UI开源Demo情景分析六、主界面_第3张图片

其实主界面很简单,就是这三个界面的载体。

Tab上面的是一个RelativeLayout布局,里面用来存放Fragment。可以从xml文件里了解这个布局的详细信息:


对,就是这个fragment_container来容纳三个界面的。再往下看,是三个按钮,继续来分析这三个按钮是怎么实现的:

    

        

            

很简单,就是三个RelativeLayout,里面都有一个Button,而且前两个右上角还有一个TextView来显示消息数,就跟QQ上面的小红点一样哦。

接下来咱们进入正题,这个界面是怎么组织的,以及里面的逻辑。

// 未读消息textview
	private TextView unreadLabel;
	// 未读通讯录textview
	private TextView unreadAddressLable;
	private Button[] mTabs;
	private Fragment[] fragments;
	private ContactlistFragment contactListFragment;
	private ChatAllHistoryFragment chatHistoryFragment;
	private SettingsFragment settingFragment;
	private int index;
	// 当前fragment的index
	private int currentTabIndex;

	// 账号在别处登录
	public boolean isConflict = false;
	// 账号被移除
	private boolean isCurrentAccountRemoved = false;
	/**
	 * 检查当前用户是否被删除
	 */
	public boolean getCurrentAccountRemoved() {
		return isCurrentAccountRemoved;
	}

首先定义了几个变量,两个TextView小红点。然后一个Button数组和一个Fragment数组分别来存放三个Fragment,当前的下标。还有两个Boolean值,用来检测当前账号是否别处登录和移出。

@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (savedInstanceState != null && savedInstanceState.getBoolean(Constant.ACCOUNT_REMOVED, false)) {
			// 防止被移除后,没点确定按钮然后按了home键,长期在后台又进app导致的crash
			// 三个fragment里加的判断同理
			DemoApplication.getInstance().logout(null);
			finish();
			startActivity(new Intent(this, LoginActivity.class));
			return;
		} else if (savedInstanceState != null && savedInstanceState.getBoolean("isConflict", false)) {
			// 防止被T后,没点确定按钮然后按了home键,长期在后台又进app导致的crash
			// 三个fragment里加的判断同理
			finish();
			startActivity(new Intent(this, LoginActivity.class));
			return;
		}
		setContentView(R.layout.activity_main);
		initView();
		// MobclickAgent.setDebugMode( true );
		// --?--
		MobclickAgent.updateOnlineConfig(this);
		if (getIntent().getBooleanExtra("conflict", false) && !isConflictDialogShow) {
			showConflictDialog();
		} else if (getIntent().getBooleanExtra(Constant.ACCOUNT_REMOVED, false) && !isAccountRemovedDialogShow) {
			showAccountRemovedDialog();
		}
		inviteMessgeDao = new InviteMessgeDao(this);
		userDao = new UserDao(this);
		// 这个fragment只显示好友和群组的聊天记录
		// chatHistoryFragment = new ChatHistoryFragment();
		// 显示所有人消息记录的fragment
		chatHistoryFragment = new ChatAllHistoryFragment();
		contactListFragment = new ContactlistFragment();
		settingFragment = new SettingsFragment();
		fragments = new Fragment[] { chatHistoryFragment, contactListFragment, settingFragment };
		// 添加显示第一个fragment
		getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, chatHistoryFragment).add(R.id.fragment_container, contactListFragment).hide(contactListFragment).show(chatHistoryFragment).commit();
		// setContactListener监听联系人的变化等
		EMContactManager.getInstance().setContactListener(new MyContactListener());
		// 注册一个监听连接状态的listener
		EMChatManager.getInstance().addConnectionListener(new MyConnectionListener());
		// 注册群聊相关的listener
		EMGroupManager.getInstance().addGroupChangeListener(new MyGroupChangeListener());
		// 通知sdk,UI 已经初始化完毕,注册了相应的receiver和listener, 可以接受broadcast了
		EMChat.getInstance().setAppInited();
	}

第一个if是用来判断账号移除或者被迫下线后没有退出账号以及当前界面可能产生的crash.

接下来的if是用来判断账号移除或者被迫下线后来显示对话框。

下面两个是用来获取本地数据库实例:

public InviteMessgeDao(Context context){
		dbHelper = DbOpenHelper.getInstance(context);
	}
public UserDao(Context context) {
		dbHelper = DbOpenHelper.getInstance(context);
	}

下面是将三个Fragment加载到界面中来。

	/**
	 * 初始化组件
	 */
	private void initView() {
		unreadLabel = (TextView) findViewById(R.id.unread_msg_number);
		unreadAddressLable = (TextView) findViewById(R.id.unread_address_number);
		mTabs = new Button[3];
		mTabs[0] = (Button) findViewById(R.id.btn_conversation);
		mTabs[1] = (Button) findViewById(R.id.btn_address_list);
		mTabs[2] = (Button) findViewById(R.id.btn_setting);
		// 把第一个tab设为选中状态
		mTabs[0].setSelected(true);
	}
            /**
     * button点击事件
     * 
     * @param view
     */
    public void onTabClicked(View view) {
        switch (view.getId()) {
        case R.id.btn_conversation:
            index = 0;
            break;
        case R.id.btn_address_list:
            index = 1;
            break;
        case R.id.btn_setting:
            index = 2;
            break;
        }
        if (currentTabIndex != index) {
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();
            trx.hide(fragments[currentTabIndex]);
            if (!fragments[index].isAdded()) {
                trx.add(R.id.fragment_container, fragments[index]);
            }
            trx.show(fragments[index]).commit();
        }
        mTabs[currentTabIndex].setSelected(false);
        // 把当前tab设为选中状态
        mTabs[index].setSelected(true);
        currentTabIndex = index;
    }

以及同时让Button与响应的Fragment同步。还有SDK的监听设置,实时获取消息或联系人状态。

接下来看看如何响应账号被强迫下线和移出:

private android.app.AlertDialog.Builder conflictBuilder;
	private android.app.AlertDialog.Builder accountRemovedBuilder;
	private boolean isConflictDialogShow;
	private boolean isAccountRemovedDialogShow;
	/**
	 * 显示帐号在别处登录dialog
	 */
	private void showConflictDialog() {
		isConflictDialogShow = true;
		DemoApplication.getInstance().logout(null);
		String st = getResources().getString(R.string.Logoff_notification);
		if (!MainActivity.this.isFinishing()) {
			// clear up global variables
			try {
				if (conflictBuilder == null)
					conflictBuilder = new android.app.AlertDialog.Builder(MainActivity.this);
				conflictBuilder.setTitle(st);
				conflictBuilder.setMessage(R.string.connect_conflict);
				conflictBuilder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						conflictBuilder = null;
						finish();
						startActivity(new Intent(MainActivity.this, LoginActivity.class));
					}
				});
				conflictBuilder.setCancelable(false);
				conflictBuilder.create().show();
				isConflict = true;
			} catch (Exception e) {
				EMLog.e(TAG, "---------color conflictBuilder error" + e.getMessage());
			}
		}
	}

通过对全局变量的赋值,来辨别对话框有没有显示。并且退出账号和该界面,重新进入登录界面。

/**
	 * 帐号被移除的dialog
	 */
	private void showAccountRemovedDialog() {
		isAccountRemovedDialogShow = true;
		DemoApplication.getInstance().logout(null);
		String st5 = getResources().getString(R.string.Remove_the_notification);
		if (!MainActivity.this.isFinishing()) {
			// clear up global variables
			try {
				if (accountRemovedBuilder == null)
					accountRemovedBuilder = new android.app.AlertDialog.Builder(MainActivity.this);
				accountRemovedBuilder.setTitle(st5);
				accountRemovedBuilder.setMessage(R.string.em_user_remove);
				accountRemovedBuilder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						accountRemovedBuilder = null;
						finish();
						startActivity(new Intent(MainActivity.this, LoginActivity.class));
					}
				});
				accountRemovedBuilder.setCancelable(false);
				accountRemovedBuilder.create().show();
				isCurrentAccountRemoved = true;
			} catch (Exception e) {
				EMLog.e(TAG, "---------color userRemovedBuilder error" + e.getMessage());
			}
		}
	}
与上面的类似。

	/**
	 * 消息监听可以注册多个,SDK支持事件链的传递,不过一旦消息链中的某个监听返回能够处理某一事件,消息将不会进一步传递。
	 * 后加入的事件监听会先收到事件的通知
	 * 如果收到的事件,能够被处理并且不需要其他的监听再处理,可以返回true,否则返回false
	 */
	@Override
	public void onEvent(EMNotifierEvent event) {
		switch (event.getEvent()) {
		case EventNewMessage: // 普通消息
		{
			EMMessage message = (EMMessage) event.getData();
			// 提示新消息
			HXSDKHelper.getInstance().getNotifier().onNewMsg(message);
			refreshUI();
			break;
		}
		case EventOfflineMessage: {
			refreshUI();
			break;
		}
		default:
			break;
		}
	}
	private void refreshUI() {
		runOnUiThread(new Runnable() {
			public void run() {
				// 刷新bottom bar消息未读数
				updateUnreadLabel();
				if (currentTabIndex == 0) {
					// 当前页面如果为聊天历史页面,刷新此页面
					if (chatHistoryFragment != null) {
						chatHistoryFragment.refresh();
					}
				}
			}
		});
	}

对消息的处理,统一更新界面,如果当前界面是聊天历史界面则刷新这个界面。同时调用HXNotifier类的onNewMsg方法,

 /**
     * 处理新收到的消息,然后发送通知
     * 
     * 开发者可以重载此函数
     * this function can be override
     * 
     * @param message
     */
    public synchronized void onNewMsg(final EMMessage message) {
        if(EMChatManager.getInstance().isSlientMessage(message)){
            return;
        }
        
        // 判断app是否在后台
        if (!EasyUtils.isAppRunningForeground(appContext)) {
            EMLog.d(TAG, "app is running in backgroud");
            sendNotification(message, false);
        } else {
            sendNotification(message, true);

        }
        
        viberateAndPlayTone(message);
    }
判断app是否在后台,是的话发送Notification,

/**
	 * 发送通知栏提示 This can be override by subclass to provide customer
	 * implementation
	 * 
	 * @param message
	 */
	protected void sendNotification(EMMessage message, boolean isForeground) {
		String username = message.getFrom();
		try {
			String notifyText = username + " ";
			switch (message.getType()) {
                        //protected final static String[] msg_ch = { "发来一条消息", "发来一张图片", "发来一段语音", "发来位置信息", "发来一个视频", "发来一个文件", "%1个联系人发来%2
                        //条消息" };
                        case TXT:
				notifyText += msgs[0];
				break;
			case IMAGE:
				 notifyText += msgs[1];
				break;
			case VOICE:
				notifyText += msgs[2];
				break;
			case LOCATION:
				notifyText += msgs[3];
				break;
			case VIDEO:
				notifyText += msgs[4];
				break;
			case FILE:
				notifyText += msgs[5];
				break;
			}
			PackageManager packageManager = appContext.getPackageManager();
			String appname = (String) packageManager.getApplicationLabel(appContext.getApplicationInfo());
			// notification titile
			String contentTitle = appname;
			if (notificationInfoProvider != null) {
				String customNotifyText = notificationInfoProvider.getDisplayedText(message);
				String customCotentTitle = notificationInfoProvider.getTitle(message);
				if (customNotifyText != null) {
					// 设置自定义的状态栏提示内容
					notifyText = customNotifyText;
				}
				if (customCotentTitle != null) {
					// 设置自定义的通知栏标题
					contentTitle = customCotentTitle;
				}
			}
			// create and send notificaiton
			NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(appContext).setSmallIcon(appContext.getApplicationInfo().icon).setWhen(System.currentTimeMillis()).setAutoCancel(true);
			Intent msgIntent = appContext.getPackageManager().getLaunchIntentForPackage(packageName);
			if (notificationInfoProvider != null) {
				// 设置自定义的notification点击跳转intent
				msgIntent = notificationInfoProvider.getLaunchIntent(message);
			}
			PendingIntent pendingIntent = PendingIntent.getActivity(appContext, notifyID, msgIntent, PendingIntent.FLAG_UPDATE_CURRENT);
			// prepare latest event info section
			notificationNum++;
			fromUsers.add(message.getFrom());
			int fromUsersNum = fromUsers.size();
			String summaryBody = msgs[6].replaceFirst("%1", Integer.toString(fromUsersNum)).replaceFirst("%2", Integer.toString(notificationNum));
			if (notificationInfoProvider != null) {
				// lastest text
				String customSummaryBody = notificationInfoProvider.getLatestText(message, fromUsersNum, notificationNum);
				if (customSummaryBody != null) {
					summaryBody = customSummaryBody;
				}
				// small icon
				int smallIcon = notificationInfoProvider.getSmallIcon(message);
				if (smallIcon != 0) {
					mBuilder.setSmallIcon(smallIcon);
				}
			}
			mBuilder.setContentTitle(contentTitle);
			mBuilder.setTicker(notifyText);
			mBuilder.setContentText(summaryBody);
			mBuilder.setContentIntent(pendingIntent);
			// mBuilder.setNumber(notificationNum);
			Notification notification = mBuilder.build();
			if (isForeground) {
				notificationManager.notify(foregroundNotifyID, notification);
				notificationManager.cancel(foregroundNotifyID);
			} else {
				notificationManager.notify(notifyID, notification);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
首先获取用户名称,然后组织消息类型,根据自定义的通知来显示消息:

/**
	 * 设置NotificationInfoProvider
	 * 
	 * @param provider
	 */
	public void setNotificationInfoProvider(HXNotificationInfoProvider provider) {
		notificationInfoProvider = provider;
	}

	public interface HXNotificationInfoProvider {
		/**
		 * 设置发送notification时状态栏提示新消息的内容(比如Xxx发来了一条图片消息)
		 * 
		 * @param message
		 *            接收到的消息
		 * @return null为使用默认
		 */
		String getDisplayedText(EMMessage message);

		/**
		 * 设置notification持续显示的新消息提示(比如2个联系人发来了5条消息)
		 * 
		 * @param message
		 *            接收到的消息
		 * @param fromUsersNum
		 *            发送人的数量
		 * @param messageNum
		 *            消息数量
		 * @return null为使用默认
		 */
		String getLatestText(EMMessage message, int fromUsersNum, int messageNum);

		/**
		 * 设置notification标题
		 * 
		 * @param message
		 * @return null为使用默认
		 */
		String getTitle(EMMessage message);

		/**
		 * 设置小图标
		 * 
		 * @param message
		 * @return 0使用默认图标
		 */
		int getSmallIcon(EMMessage message);

		/**
		 * 设置notification点击时的跳转intent
		 * 
		 * @param message
		 *            显示在notification上最近的一条消息
		 * @return null为使用默认
		 */
		Intent getLaunchIntent(EMMessage message);
	}

发送通知之后设置手机震动和声音提示:

/**
	 * 手机震动和声音提示
	 */
	public void viberateAndPlayTone(EMMessage message) {
		if (EMChatManager.getInstance().isSlientMessage(message)) {
			return;
		}

		HXSDKModel model = HXSDKHelper.getInstance().getModel();
		if (!model.getSettingMsgNotification()) {
			return;
		}

		if (System.currentTimeMillis() - lastNotifiyTime < 1000) {
			// received new messages within 2 seconds, skip play ringtone
			return;
		}

		try {
			lastNotifiyTime = System.currentTimeMillis();

			// 判断是否处于静音模式
			if (audioManager.getRingerMode() == AudioManager.RINGER_MODE_SILENT) {
				EMLog.e(TAG, "in slient mode now");
				return;
			}

			if (model.getSettingMsgVibrate()) {
				long[] pattern = new long[] { 0, 180, 80, 120 };
				vibrator.vibrate(pattern, -1);
			}

			if (model.getSettingMsgSound()) {
				if (ringtone == null) {
					Uri notificationUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

					ringtone = RingtoneManager.getRingtone(appContext, notificationUri);
					if (ringtone == null) {
						EMLog.d(TAG, "cant find ringtone at:" + notificationUri.getPath());
						return;
					}
				}

				if (!ringtone.isPlaying()) {
					String vendor = Build.MANUFACTURER;

					ringtone.play();
					// for samsung S3, we meet a bug that the phone will
					// continue ringtone without stop
					// so add below special handler to stop it after 3s if
					// needed
					if (vendor != null && vendor.toLowerCase().contains("samsung")) {
						Thread ctlThread = new Thread() {
							public void run() {
								try {
									Thread.sleep(3000);
									if (ringtone.isPlaying()) {
										ringtone.stop();
									}
								} catch (Exception e) {
								}
							}
						};
						ctlThread.run();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
判断应用对震动和铃声的设置,然后再检测系统设置。

在更新UI的时候,同时刷新未读消息数:

	/**
     * 刷新未读消息数
     */
    public void updateUnreadLabel() {
        int count = getUnreadMsgCountTotal();
        if (count > 0) {
            unreadLabel.setText(String.valueOf(count));
            unreadLabel.setVisibility(View.VISIBLE);
        } else {
            unreadLabel.setVisibility(View.INVISIBLE);
        }
    }
    /**
     * 刷新申请与通知消息数
     */
    public void updateUnreadAddressLable() {
        runOnUiThread(new Runnable() {
            public void run() {
                int count = getUnreadAddressCountTotal();
                if (count > 0) {
                    unreadAddressLable.setText(String.valueOf(count));
                    unreadAddressLable.setVisibility(View.VISIBLE);
                } else {
                    unreadAddressLable.setVisibility(View.INVISIBLE);
                }
            }
        });
    }
    /**
     * 获取未读申请与通知消息
     * 
     * @return
     */
    public int getUnreadAddressCountTotal() {
        int unreadAddressCountTotal = 0;
        if (DemoApplication.getInstance().getContactList().get(Constant.NEW_FRIENDS_USERNAME) != null)
            unreadAddressCountTotal = DemoApplication.getInstance().getContactList().get(Constant.NEW_FRIENDS_USERNAME).getUnreadMsgCount();
        return unreadAddressCountTotal;
    }
    /**
     * 获取内存中好友user list
     *
     * @return
     */
    public Map getContactList() {
        return hxSDKHelper.getContactList();
    }
    /**
     * 获取内存中好友user list
     *
     * @return
     */
    public Map getContactList() {
        if (getHXId() != null && contactList == null) {
            contactList = ((DemoHXSDKModel) getModel()).getContactList();
        }
        return contactList;
    }
    /**
     * 获取未读消息数
     * 
     * @return
     */
    public int getUnreadMsgCountTotal() {
        int unreadMsgCountTotal = 0;
        unreadMsgCountTotal = EMChatManager.getInstance().getUnreadMsgsCount();
        return unreadMsgCountTotal;
    }

在最开始的时候,有监听MyContactListener,MyConnectionListener,MyGroupChangeListener。

/***
	 * 好友变化listener
	 * 
	 */
	private class MyContactListener implements EMContactListener {

		@Override
		public void onContactAdded(List usernameList) {
			// 保存增加的联系人
			Map localUsers = DemoApplication.getInstance().getContactList();
			Map toAddUsers = new HashMap();
			for (String username : usernameList) {
				User user = setUserHead(username);
				// 添加好友时可能会回调added方法两次
				if (!localUsers.containsKey(username)) {
					userDao.saveContact(user);
				}
				toAddUsers.put(username, user);
			}
			localUsers.putAll(toAddUsers);
			// 刷新ui
			if (currentTabIndex == 1)
				contactListFragment.refresh();
		}
		@Override
		public void onContactDeleted(final List usernameList) {
			// 被删除
			Map localUsers = DemoApplication.getInstance().getContactList();
			for (String username : usernameList) {
				localUsers.remove(username);
				userDao.deleteContact(username);
				inviteMessgeDao.deleteMessage(username);
			}
			runOnUiThread(new Runnable() {
				public void run() {
					// 如果正在与此用户的聊天页面
					String st10 = getResources().getString(R.string.have_you_removed);
					if (ChatActivity.activityInstance != null && usernameList.contains(ChatActivity.activityInstance.getToChatUsername())) {
						Toast.makeText(MainActivity.this, ChatActivity.activityInstance.getToChatUsername() + st10, 1).show();
						ChatActivity.activityInstance.finish();
					}
					updateUnreadLabel();
					// 刷新ui
					contactListFragment.refresh();
					chatHistoryFragment.refresh();
				}
			});
		}

		@Override
		public void onContactInvited(String username, String reason) {
			// 接到邀请的消息,如果不处理(同意或拒绝),掉线后,服务器会自动再发过来,所以客户端不需要重复提醒
			List msgs = inviteMessgeDao.getMessagesList();
			for (InviteMessage inviteMessage : msgs) {
				if (inviteMessage.getGroupId() == null && inviteMessage.getFrom().equals(username)) {
					inviteMessgeDao.deleteMessage(username);
				}
			}
			// 自己封装的javabean
			InviteMessage msg = new InviteMessage();
			msg.setFrom(username);
			msg.setTime(System.currentTimeMillis());
			msg.setReason(reason);
			Log.d(TAG, username + "请求加你为好友,reason: " + reason);
			// 设置相应status
			msg.setStatus(InviteMesageStatus.BEINVITEED);
			notifyNewIviteMessage(msg);
		}

		@Override
		public void onContactAgreed(String username) {
			List msgs = inviteMessgeDao.getMessagesList();
			for (InviteMessage inviteMessage : msgs) {
				if (inviteMessage.getFrom().equals(username)) {
					return;
				}
			}
			// 自己封装的javabean
			InviteMessage msg = new InviteMessage();
			msg.setFrom(username);
			msg.setTime(System.currentTimeMillis());
			Log.d(TAG, username + "同意了你的好友请求");
			msg.setStatus(InviteMesageStatus.BEAGREED);
			notifyNewIviteMessage(msg);
		}
		@Override
		public void onContactRefused(String username) {
			// 参考同意,被邀请实现此功能,demo未实现
			Log.d(username, username + "拒绝了你的好友请求");
		}
	}
/**
     * 连接监听listener
     * 
     */
    private class MyConnectionListener implements EMConnectionListener {
        @Override
        public void onConnected() {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    chatHistoryFragment.errorItem.setVisibility(View.GONE);
                }
            });
        }
        @Override
        public void onDisconnected(final int error) {
            final String st1 = getResources().getString(R.string.Less_than_chat_server_connection);
            final String st2 = getResources().getString(R.string.the_current_network);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (error == EMError.USER_REMOVED) {
                        // 显示帐号已经被移除
                        showAccountRemovedDialog();
                    } else if (error == EMError.CONNECTION_CONFLICT) {
                        // 显示帐号在其他设备登陆dialog
                        showConflictDialog();
                    } else {
                        chatHistoryFragment.errorItem.setVisibility(View.VISIBLE);
                        if (NetUtils.hasNetwork(MainActivity.this))
                            chatHistoryFragment.errorText.setText(st1);
                        else
                            chatHistoryFragment.errorText.setText(st2);
                    }
                }
            });
        }
    }
/**
	 * MyGroupChangeListener
	 */
	private class MyGroupChangeListener implements GroupChangeListener {
		@Override
		public void onInvitationReceived(String groupId, String groupName, String inviter, String reason) {
			boolean hasGroup = false;
			for (EMGroup group : EMGroupManager.getInstance().getAllGroups()) {
				if (group.getGroupId().equals(groupId)) {
					hasGroup = true;
					break;
				}
			}
			if (!hasGroup)
				return;
			// 被邀请
			String st3 = getResources().getString(R.string.Invite_you_to_join_a_group_chat);
			EMMessage msg = EMMessage.createReceiveMessage(Type.TXT);
			msg.setChatType(ChatType.GroupChat);
			msg.setFrom(inviter);
			msg.setTo(groupId);
			msg.setMsgId(UUID.randomUUID().toString());
			msg.addBody(new TextMessageBody(inviter + st3));
			// 保存邀请消息
			EMChatManager.getInstance().saveMessage(msg);
			// 提醒新消息
			EMNotifier.getInstance(getApplicationContext()).notifyOnNewMsg();
			runOnUiThread(new Runnable() {
				public void run() {
					updateUnreadLabel();
					// 刷新ui
					if (currentTabIndex == 0)
						chatHistoryFragment.refresh();
					if (CommonUtils.getTopActivity(MainActivity.this).equals(GroupsActivity.class.getName())) {
						GroupsActivity.instance.onResume();
					}
				}
			});
		}
		@Override
		public void onInvitationAccpted(String groupId, String inviter, String reason) {
		}
		@Override
		public void onInvitationDeclined(String groupId, String invitee, String reason) {
		}
		@Override
		public void onUserRemoved(String groupId, String groupName) {
			// 提示用户被T了,demo省略此步骤
			// 刷新ui
			runOnUiThread(new Runnable() {
				public void run() {
					try {
						updateUnreadLabel();
						if (currentTabIndex == 0)
							chatHistoryFragment.refresh();
						if (CommonUtils.getTopActivity(MainActivity.this).equals(GroupsActivity.class.getName())) {
							GroupsActivity.instance.onResume();
						}
					} catch (Exception e) {
						EMLog.e(TAG, "refresh exception " + e.getMessage());
					}
				}
			});
		}
		@Override
		public void onGroupDestroy(String groupId, String groupName) {
			// 群被解散
			// 提示用户群被解散,demo省略
			// 刷新ui
			runOnUiThread(new Runnable() {
				public void run() {
					updateUnreadLabel();
					if (currentTabIndex == 0)
						chatHistoryFragment.refresh();
					if (CommonUtils.getTopActivity(MainActivity.this).equals(GroupsActivity.class.getName())) {
						GroupsActivity.instance.onResume();
					}
				}
			});
		}
		@Override
		public void onApplicationReceived(String groupId, String groupName, String applyer, String reason) {
			// 用户申请加入群聊
			InviteMessage msg = new InviteMessage();
			msg.setFrom(applyer);
			msg.setTime(System.currentTimeMillis());
			msg.setGroupId(groupId);
			msg.setGroupName(groupName);
			msg.setReason(reason);
			Log.d(TAG, applyer + " 申请加入群聊:" + groupName);
			msg.setStatus(InviteMesageStatus.BEAPPLYED);
			notifyNewIviteMessage(msg);
		}
		@Override
		public void onApplicationAccept(String groupId, String groupName, String accepter) {
			String st4 = getResources().getString(R.string.Agreed_to_your_group_chat_application);
			// 加群申请被同意
			EMMessage msg = EMMessage.createReceiveMessage(Type.TXT);
			msg.setChatType(ChatType.GroupChat);
			msg.setFrom(accepter);
			msg.setTo(groupId);
			msg.setMsgId(UUID.randomUUID().toString());
			msg.addBody(new TextMessageBody(accepter + st4));
			// 保存同意消息
			EMChatManager.getInstance().saveMessage(msg);
			// 提醒新消息
			EMNotifier.getInstance(getApplicationContext()).notifyOnNewMsg();
			runOnUiThread(new Runnable() {
				public void run() {
					updateUnreadLabel();
					// 刷新ui
					if (currentTabIndex == 0)
						chatHistoryFragment.refresh();
					if (CommonUtils.getTopActivity(MainActivity.this).equals(GroupsActivity.class.getName())) {
						GroupsActivity.instance.onResume();
					}
				}
			});
		}
		@Override
		public void onApplicationDeclined(String groupId, String groupName, String decliner, String reason) {
			// 加群申请被拒绝,demo未实现
		}
	}

关于SDK监听此处不再详细解释。在界面重新可见的时候:

@Override
	protected void onResume() {
		super.onResume();
		if (!isConflict && !isCurrentAccountRemoved) {
			updateUnreadLabel();
			updateUnreadAddressLable();
			EMChatManager.getInstance().activityResumed();
		}
		// unregister this event listener when this activity enters the
		// background
		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 });
	}

	/**
	 * 用来记录foreground Activity
	 */
	private List activityList = new ArrayList();
	public void pushActivity(Activity activity) {
		if (!activityList.contains(activity)) {
			activityList.add(0, activity);
		}
	}
记录一下后台的Activity。

当界面停止的时候移出界面:

	@Override
	protected void onStop() {
		EMChatManager.getInstance().unregisterEventListener(this);
		DemoHXSDKHelper sdkHelper = (DemoHXSDKHelper) DemoHXSDKHelper.getInstance();
		sdkHelper.popActivity(this);
		super.onStop();
	}
    public void popActivity(Activity activity) {
        activityList.remove(activity);
    }
最后:

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		outState.putBoolean("isConflict", isConflict);
		outState.putBoolean(Constant.ACCOUNT_REMOVED, isCurrentAccountRemoved);
		super.onSaveInstanceState(outState);
	}
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			moveTaskToBack(false);
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
保存消息,并设置moveTaskToBack,让程序不退出。

到此,主界面就完成了。接下来咱们分别对三个界面来进行分析。


你可能感兴趣的:(环信UI开源Demo情景分析)