差别
这里会显示出您选择的修订版和当前版本之间的差别。
两侧同时换到之前的修订记录 前一修订版 后一修订版 | 前一修订版 | ||
start:200androidcleintintegration:360androidradpakge [2016/08/22 09:09] liulij [7.添加零钱页的入口] |
— (当前版本) | ||
---|---|---|---|
行 1: | 行 1: | ||
- | ====== 环信红包接入文档 ====== | ||
- | |||
- | ---- | ||
- | ===== 环信红包简介(此文档只针对非Demo版本的集成说明,仅做参考)===== | ||
- | |||
- | ---- | ||
- | 1.redpacketlibrary,在环信SDK的基础上提供了收发红包和零钱的功能。 | ||
- | |||
- | 2.环信官方版Demo已默认集成红包功能,可以直接下载试用。 | ||
- | |||
- | |||
- | ===== redpacketlibrary目录说明 ===== | ||
- | |||
- | ---- | ||
- | * libs :包含了集成红包功能所依赖的jar包。(红包使用了glide库做图片加载,由于已经依赖了easeui这里不重复添加) | ||
- | |||
- | * res :包含了红包SDK和聊天页面中的资源文件。(红包SDK相关以rp开头,聊天页面相关以em开头) | ||
- | |||
- | * utils : 封装了收发红包的相关方法。 | ||
- | |||
- | * widget :聊天界面中的红包以及领取红包后的chatrow。 | ||
- | |||
- | * RedPacketConstant.java 红包功能需要的常量。 | ||
- | |||
- | * 注意: 由于RedPacketUtil类中使用了环信SDK中相关方法,redpacketlibrary依赖了easeui。 | ||
- | |||
- | ===== 集成步骤 ===== | ||
- | |||
- | ---- | ||
- | |||
- | ==== 1.添加对红包工程的依赖 ==== | ||
- | |||
- | ---- | ||
- | |||
- | * ChatDemo的build.gradle中 | ||
- | |||
- | <code java> | ||
- | dependencies { | ||
- | compile project(':redpacketlibrary') | ||
- | compile project(':EaseUI') | ||
- | compile fileTree(dir: 'libs', include: '*.jar', exclude: 'android-support-multidex.jar') | ||
- | } | ||
- | </code> | ||
- | |||
- | * ChatDemo的setting.gradle中 | ||
- | |||
- | <code java> | ||
- | include ':EaseUI', ':redpacketlibrary' | ||
- | </code> | ||
- | |||
- | ==== 2.ChatDemo清单文件中注册红包相关组件 ==== | ||
- | |||
- | <code java> | ||
- | |||
- | <uses-sdk | ||
- | android:minSdkVersion="9" | ||
- | android:targetSdkVersion="19" | ||
- | tools:overrideLibrary="com.easemob.redpacketui" | ||
- | /> | ||
- | | ||
- | <!--红包相关界面start--> | ||
- | <activity | ||
- | android:name="com.easemob.redpacketui.ui.activity.RPRedPacketActivity" | ||
- | android:screenOrientation="portrait" | ||
- | android:theme="@style/horizontal_slide" | ||
- | android:windowSoftInputMode="adjustPan|stateVisible" | ||
- | /> | ||
- | |||
- | <activity | ||
- | android:name="com.easemob.redpacketui.ui.activity.RPDetailActivity" | ||
- | android:screenOrientation="portrait" | ||
- | android:theme="@style/horizontal_slide" | ||
- | android:windowSoftInputMode="adjustPan" | ||
- | /> | ||
- | |||
- | <activity | ||
- | android:name="com.easemob.redpacketui.ui.activity.RPRecordActivity" | ||
- | android:screenOrientation="portrait" | ||
- | android:theme="@style/horizontal_slide" | ||
- | android:windowSoftInputMode="adjustPan" | ||
- | /> | ||
- | |||
- | <activity | ||
- | android:name="com.easemob.redpacketui.ui.activity.RPWebViewActivity" | ||
- | android:screenOrientation="portrait" | ||
- | android:theme="@style/horizontal_slide" | ||
- | android:windowSoftInputMode="adjustResize|stateHidden" | ||
- | /> | ||
- | <activity | ||
- | android:name="com.easemob.redpacketui.ui.activity.RPChangeActivity" | ||
- | android:configChanges="orientation|keyboardHidden|screenSize" | ||
- | android:screenOrientation="portrait" | ||
- | android:theme="@style/horizontal_slide" | ||
- | android:windowSoftInputMode="adjustResize|stateHidden" | ||
- | /> | ||
- | |||
- | <activity | ||
- | android:name="com.easemob.redpacketui.ui.activity.RPBankCardActivity" | ||
- | android:screenOrientation="portrait" | ||
- | android:theme="@style/horizontal_slide" | ||
- | android:windowSoftInputMode="adjustPan|stateHidden" | ||
- | /> | ||
- | |||
- | <activity | ||
- | android:name="com.easemob.redpacketui.ui.activity.RPGroupMemberActivity" | ||
- | android:screenOrientation="portrait" | ||
- | android:theme="@style/horizontal_slide" | ||
- | android:windowSoftInputMode="adjustPan|stateHidden" | ||
- | /> | ||
- | |||
- | <activity | ||
- | android:name="com.alipay.sdk.app.H5PayActivity" | ||
- | android:configChanges="orientation|keyboardHidden|navigation|screenSize" | ||
- | android:exported="false" | ||
- | android:screenOrientation="behind" | ||
- | android:windowSoftInputMode="adjustResize|stateHidden" | ||
- | /> | ||
- | <!--红包相关界面end--> | ||
- | </code> | ||
- | |||
- | ==== 3.DemoApplication初始化红包上下文 ==== | ||
- | <code java> | ||
- | |||
- | import com.easemob.redpacketsdk.RedPacket; | ||
- | | ||
- | @Override | ||
- | public void onCreate() { | ||
- | super.onCreate(); | ||
- | RedPacket.getInstance().initContext(applicationContext); | ||
- | //打开Log开关 正式发布时请关闭 | ||
- | RedPacket.getInstance().setDebugMode(true); | ||
- | } | ||
- | | ||
- | </code> | ||
- | |||
- | ==== 4.ChatFragment中增加收发红包的功能 ==== | ||
- | |||
- | * 添加红包相关常量 | ||
- | | ||
- | <code java> | ||
- | | ||
- | private static final int MESSAGE_TYPE_RECV_RED_PACKET = 5; | ||
- | | ||
- | private static final int MESSAGE_TYPE_SEND_RED_PACKET = 6; | ||
- | | ||
- | private static final int MESSAGE_TYPE_SEND_RED_PACKET_ACK = 7; | ||
- | | ||
- | private static final int MESSAGE_TYPE_RECV_RED_PACKET_ACK = 8; | ||
- | | ||
- | private static final int ITEM_RED_PACKET = 16; | ||
- | | ||
- | private static final int REQUEST_CODE_SEND_RED_PACKET = 15; | ||
- | </code> | ||
- | * 添加红包入口 | ||
- | <code java> | ||
- | | ||
- | @Override | ||
- | protected void registerExtendMenuItem() { | ||
- | //demo这里不覆盖基类已经注册的item,item点击listener沿用基类的 | ||
- | super.registerExtendMenuItem(); | ||
- | //聊天室暂时不支持红包功能 | ||
- | if (chatType != Constant.CHATTYPE_CHATROOM) { | ||
- | inputMenu.registerExtendMenuItem(R.string.attach_red_packet, R.drawable.em_chat_red_packet_selector, ITEM_RED_PACKET, extendMenuItemClickListener); | ||
- | } | ||
- | } | ||
- | | ||
- | </code> | ||
- | * 添加自定义chatrow到CustomChatRowProvider,详见ChatFragment中的CustomChatRowProvider。 | ||
- | * ContextMenuActivity的onCreate()中屏蔽红包消息的转发和撤回功能。 | ||
- | | ||
- | <code java> | ||
- | | ||
- | if (type == EMMessage.Type.TXT.ordinal()) { | ||
- | if(message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VIDEO_CALL, false) || | ||
- | message.getBooleanAttribute(Constant.MESSAGE_ATTR_IS_VOICE_CALL, false) | ||
- | || message.getBooleanAttribute(RedPacketConstant.MESSAGE_ATTR_IS_RED_PACKET_MESSAGE, false)){ | ||
- | setContentView(R.layout.em_context_menu_for_location); | ||
- | } | ||
- | } | ||
- | | ||
- | if (message.direct == EMMessage.Direct.RECEIVE || message.getChatType() == EMMessage.ChatType.ChatRoom | ||
- | //red packet code : 屏蔽红包消息的撤回功能 | ||
- | || message.getBooleanAttribute(RedPacketConstant.MESSAGE_ATTR_IS_RED_PACKET_MESSAGE, false)) { | ||
- | //end of red packet code | ||
- | View view = findViewById(R.id.text_revoke); | ||
- | if(view != null){ | ||
- | view.setVisibility(View.GONE); | ||
- | } | ||
- | } | ||
- | | ||
- | </code> | ||
- | |||
- | * 进入发红包页面 | ||
- | |||
- | <code java> | ||
- | | ||
- | @Override | ||
- | public boolean onExtendMenuItemClick(int itemId, View view) { | ||
- | switch (itemId) { | ||
- | ... | ||
- | case ITEM_RED_PACKET: | ||
- | RedPacketUtils.startRedPacketActivityForResult(this, chatType, toChatUsername, REQUEST_CODE_SEND_RED_PACKET); | ||
- | break; | ||
- | default: | ||
- | break; | ||
- | } | ||
- | //不覆盖已有的点击事件 | ||
- | return false; | ||
- | } | ||
- | | ||
- | </code> | ||
- | |||
- | * 发送红包消息到聊天页面 | ||
- | |||
- | <code java> | ||
- | | ||
- | @Override | ||
- | public void onActivityResult(int requestCode, int resultCode, Intent data) { | ||
- | super.onActivityResult(requestCode, resultCode, data); | ||
- | ... | ||
- | if(resultCode == Activity.RESULT_OK){ | ||
- | switch (requestCode) { | ||
- | ... | ||
- | case REQUEST_CODE_SEND_RED_PACKET: | ||
- | if (data != null){ | ||
- | sendMessage(RedPacketUtils.createRPMessage(getActivity(), data, toChatUsername)); | ||
- | } | ||
- | break; | ||
- | default: | ||
- | break; | ||
- | } | ||
- | } | ||
- | } | ||
- | </code> | ||
- | |||
- | * 领取红包并发送回执消息到聊天窗口 | ||
- | | ||
- | <code java> | ||
- | | ||
- | @Override | ||
- | public boolean onMessageBubbleClick(EMMessage message) { | ||
- | //消息框点击事件,demo这里不做覆盖,如需覆盖,return true | ||
- | if (message.getBooleanAttribute(RedPacketConstant.MESSAGE_ATTR_IS_RED_PACKET_MESSAGE, false)){ | ||
- | RedPacketUtils.openRedPacket(getActivity(), chatType, message, toChatUsername, messageList); | ||
- | return true; | ||
- | } | ||
- | return false; | ||
- | } | ||
- | |||
- | </code> | ||
- | * ChatFragment中群红包领取回执的处理(聊天页面) | ||
- | <code java> | ||
- | | ||
- | @Override | ||
- | public void onEvent(EMNotifierEvent event) { | ||
- | switch (event.getEvent()) { | ||
- | case EventNewCMDMessage: | ||
- | EMMessage cmdMessage = (EMMessage) event.getData(); | ||
- | CmdMessageBody cmdMsgBody = (CmdMessageBody) cmdMessage.getBody(); | ||
- | final String action = cmdMsgBody.action;//获取自定义action | ||
- | if (action.equals(RedPacketConstant.REFRESH_GROUP_RED_PACKET_ACTION)) { | ||
- | RedPacketUtil.receiveRedPacketAckMessage(cmdMessage); | ||
- | messageList.refresh(); | ||
- | } | ||
- | break; | ||
- | } | ||
- | super.onEvent(event); | ||
- | } | ||
- | | ||
- | </code> | ||
- | * MainActivity中群红包领取回执的处理(导航页面) | ||
- | |||
- | <code java> | ||
- | | ||
- | @Override | ||
- | public void onEvent(EMNotifierEvent event) { | ||
- | switch (event.getEvent()) { | ||
- | ... | ||
- | case EventNewCMDMessage: | ||
- | EMMessage cmdMessage = (EMMessage) event.getData(); | ||
- | //获取消息body | ||
- | CmdMessageBody cmdMsgBody = (CmdMessageBody) cmdMessage.getBody(); | ||
- | final String action = cmdMsgBody.action;//获取自定义action | ||
- | ... | ||
- | if (action.equals(RedPacketConstant.REFRESH_GROUP_RED_PACKET_ACTION)) { | ||
- | RedPacketUtil.receiveRedPacketAckMessage(cmdMessage); | ||
- | } | ||
- | refreshUIWithMessage(); | ||
- | break; | ||
- | default: | ||
- | break; | ||
- | } | ||
- | } | ||
- | | ||
- | </code> | ||
- | ==== 5.群红包领取回执的全局处理 ==== | ||
- | * DemoHelper中 | ||
- | |||
- | <code java> | ||
- | | ||
- | protected void registerEventListener() { | ||
- | eventListener = new EMEventListener() { | ||
- | private BroadcastReceiver broadCastReceiver = null; | ||
- | @Override | ||
- | public void onEvent(EMNotifierEvent event) { | ||
- | switch (event.getEvent()) { | ||
- | ... | ||
- | case EventNewCMDMessage: | ||
- | //获取消息body | ||
- | CmdMessageBody cmdMsgBody = (CmdMessageBody) message.getBody(); | ||
- | final String action = cmdMsgBody.action;//获取自定义action | ||
- | if(!easeUI.hasForegroundActivies()){ | ||
- | if (action.equals(RedPacketConstant.REFRESH_GROUP_RED_PACKET_ACTION)){ | ||
- | RedPacketUtil.receiveRedPacketAckMessage(message); | ||
- | broadcastManager.sendBroadcast(new Intent(RedPacketConstant.REFRESH_GROUP_RED_PACKET_ACTION)); | ||
- | } | ||
- | } | ||
- | break; | ||
- | ... | ||
- | } | ||
- | } | ||
- | }; | ||
- | EMChatManager.getInstance().registerEventListener(eventListener); | ||
- | } | ||
- | |||
- | </code> | ||
- | * MainActivity中 | ||
- | | ||
- | <code java> | ||
- | | ||
- | private void registerBroadcastReceiver() { | ||
- | broadcastManager = LocalBroadcastManager.getInstance(this); | ||
- | IntentFilter intentFilter = new IntentFilter(); | ||
- | intentFilter.addAction(Constant.ACTION_CONTACT_CHANAGED); | ||
- | intentFilter.addAction(Constant.ACTION_GROUP_CHANAGED); | ||
- | intentFilter.addAction(RedPacketConstant.REFRESH_GROUP_RED_PACKET_ACTION); | ||
- | broadcastReceiver = new BroadcastReceiver() { | ||
- | | ||
- | @Override | ||
- | public void onReceive(Context context, Intent intent) { | ||
- | ... | ||
- | if (action.equals(RedPacketConstant.REFRESH_GROUP_RED_PACKET_ACTION)){ | ||
- | if (conversationListFragment != null){ | ||
- | conversationListFragment.refresh(); | ||
- | } | ||
- | } | ||
- | } | ||
- | }; | ||
- | broadcastManager.registerReceiver(broadcastReceiver, intentFilter); | ||
- | } | ||
- | | ||
- | </code> | ||
- | | ||
- | ==== 6.ConversationListFragment中对红包回执消息的处理 ==== | ||
- | | ||
- | <code java> | ||
- | | ||
- | @Override | ||
- | protected void setUpView() { | ||
- | ... | ||
- | conversationListView.setConversationListHelper(new EaseConversationListHelper() { | ||
- | @Override | ||
- | public String onSetItemSecondaryText(EMMessage lastMessage) { | ||
- | if (lastMessage.getBooleanAttribute(RedPacketConstant.MESSAGE_ATTR_IS_RED_PACKET_ACK_MESSAGE, false)) { | ||
- | String sendNick = lastMessage.getStringAttribute(RedPacketConstant.EXTRA_RED_PACKET_SENDER_NAME, ""); | ||
- | String receiveNick = lastMessage.getStringAttribute(RedPacketConstant.EXTRA_RED_PACKET_RECEIVER_NAME, ""); | ||
- | String msg; | ||
- | if (lastMessage.direct == EMMessage.Direct.RECEIVE) { | ||
- | msg = String.format(getResources().getString(R.string.money_msg_someone_take_money), receiveNick); | ||
- | } else { | ||
- | if (sendNick.equals(receiveNick)) { | ||
- | msg = getResources().getString(R.string.money_msg_take_money); | ||
- | } else { | ||
- | msg = String.format(getResources().getString(R.string.money_msg_take_someone_money), sendNick); | ||
- | } | ||
- | } | ||
- | return msg; | ||
- | } | ||
- | return null; | ||
- | } | ||
- | }); | ||
- | super.setUpView(); | ||
- | } | ||
- | | ||
- | </code> | ||
- | ==== 7.添加零钱页的入口 ==== | ||
- | <code java> | ||
- | RedPacketUtils.startChangeActivity(getActivity()); | ||
- | </code> | ||
- | |||
- | ---- | ||
- | <WRAP group> | ||
- | <WRAP half column> | ||
- | 上一页:[[start:200androidcleintintegration:90realtimeaudio|实时通话]] | ||
- | </WRAP> | ||
- | |||
- | <WRAP half column> | ||
- | 下一页:[[start:200androidcleintintegration:20iosnickname|设置当前登录用户的 iOS 推送昵称]] | ||
- | </WRAP> | ||
- | </WRAP> | ||
- | |||
- | |