|
11 | 11 | {% block demo %}
|
12 | 12 | * [LeanMessage](https://github.com/leancloud/LeanMessage-Demo)(推荐)
|
13 | 13 | * [LeanChat](https://github.com/leancloud/leanchat-android)
|
14 |
| - {% endblock %} |
| 14 | +{% endblock %} |
15 | 15 |
|
16 | 16 | {% block oneOnOneChat_sent %}
|
17 | 17 | {{ docs.alert("启用实时通信一定要正确配置 `AndroidManifest.xml`,请仔细阅读 [Android SDK 初始化配置](sdk_setup-android.html#初始化)。") }}
|
@@ -161,89 +161,92 @@ public void jerryReceiveMsgFromTom(){
|
161 | 161 |
|
162 | 162 | {% block groupChat_received %}
|
163 | 163 | ```
|
164 |
| -public class MyApplication extends Application{ |
165 |
| - public void onCreate(){ |
166 |
| - ... |
167 |
| - AVOSCloud.initialize(this,"{{appid}}","{{appkey}}"); |
168 |
| - //这里指定只处理AVIMTextMessage类型的消息 |
169 |
| - AVIMMessageManager.registerMessageHandler(AVIMTextMessage.class,new CustomMessageHanlder()); |
| 164 | +public class MyApplication extends Application { |
| 165 | + public void onCreate() { |
| 166 | + ... |
| 167 | + AVOSCloud.initialize(this, "{{appid}}", "{{appkey}}"); |
| 168 | + //这里指定只处理AVIMTextMessage类型的消息 |
| 169 | + AVIMMessageManager.registerMessageHandler(AVIMTextMessage.class, new CustomMessageHanlder()); |
170 | 170 | }
|
171 | 171 | }
|
172 | 172 |
|
173 |
| -- CustomMessageHandler.java |
174 |
| -public class CustomMessageHandler<AVIMTextMessage> implements AVIMTypedMessageHandler{ |
175 |
| - |
| 173 | +// ========================= |
| 174 | +// CustomMessageHandler.java |
| 175 | +// ========================= |
| 176 | +public class CustomMessageHandler < AVIMTextMessage > implements AVIMTypedMessageHandler { |
| 177 | +
|
176 | 178 | @Override
|
177 |
| - public void onMessage(AVIMTextMessage msg,AVIMConversation conv,AVIMClient client){ |
178 |
| - Log.d("Tom & Jerry",msg.getText();)//你们在哪儿? |
| 179 | + public void onMessage(AVIMTextMessage msg, AVIMConversation conv, AVIMClient client) { |
| 180 | + Log.d("Tom & Jerry", msg.getText();) //你们在哪儿? |
179 | 181 | // 收到消息之后一般的做法是做 UI 展现,示例代码在此处做消息回复,仅为了演示收到消息之后的操作,仅供参考。
|
180 | 182 | AVIMTextMessage reply = new AVIMTextMessage();
|
181 | 183 | reply.setText("Tom,我在 Jerry 家,你跟 Harry 什么时候过来?还有 William 和你在一起么?");
|
182 |
| - conv.sendMessage(reply,new AVIMConversationCallback(){ |
183 |
| - public void done(AVIMException e){ |
184 |
| - if(e==null){ |
185 |
| - //回复成功! |
186 |
| - } |
187 |
| - } |
188 |
| - }); |
| 184 | + conv.sendMessage(reply, new AVIMConversationCallback() { |
| 185 | + public void done(AVIMException e) { |
| 186 | + if (e == null) { |
| 187 | + //回复成功! |
| 188 | + } |
| 189 | + } |
| 190 | + }); |
189 | 191 | }
|
190 |
| - |
191 |
| -public void onMessageReceipt(AVIMTextMessage msg,AVIMConversation conv,AVIMClient client){ |
192 |
| - |
193 |
| -} |
194 |
| -} |
195 | 192 |
|
| 193 | + public void onMessageReceipt(AVIMTextMessage msg, AVIMConversation conv, AVIMClient client) { |
196 | 194 |
|
197 |
| -- SomeActivity.java |
198 |
| -public void loginAsBob(){ |
199 |
| - AVIMClient bob = AVIMClient.getInstance("Bob"); |
200 |
| - //Bob登录 |
201 |
| - bob.open(new AVIMClientCallback(){ |
202 |
| - public void done(AVIMClient client,AVIMException e){ |
203 |
| - if(e==null){ |
204 |
| - //登录成功 |
205 |
| - } |
206 |
| - } |
207 |
| - }); |
| 195 | + } |
| 196 | +} |
| 197 | +
|
| 198 | +// ================= |
| 199 | +// SomeActivity.java |
| 200 | +// ================= |
| 201 | +public void loginAsBob() { |
| 202 | + AVIMClient bob = AVIMClient.getInstance("Bob"); |
| 203 | + //Bob登录 |
| 204 | + bob.open(new AVIMClientCallback() { |
| 205 | + public void done(AVIMClient client, AVIMException e) { |
| 206 | + if (e == null) { |
| 207 | + //登录成功 |
| 208 | + } |
| 209 | + } |
| 210 | + }); |
208 | 211 | }
|
209 | 212 | ```
|
210 | 213 | {% endblock %}
|
211 | 214 |
|
212 | 215 | {% block imageMessage_local_sent %}
|
213 | 216 |
|
214 | 217 | ```
|
215 |
| -public void sendImage(String filePath){ |
| 218 | +public void sendImage(String filePath) { |
216 | 219 | AVIMClient tom = AVIMClient.getInstance("Tom");
|
217 | 220 |
|
218 |
| - tom.open(new AVIMClientCallback(){ |
219 |
| - |
| 221 | + tom.open(new AVIMClientCallback() { |
| 222 | +
|
220 | 223 | @Override
|
221 |
| - public void done(AVIMClient client,AVIMException e){ |
222 |
| - if(e==null){ |
223 |
| - //登录成功 |
224 |
| - // 创建对话,默认创建者是在包含在成员列表中的 |
225 |
| - client.createConversation(Arrays.asList("Jerry"),new AVIMConversationCreatedCallback(){ |
226 |
| - |
227 |
| - @Override |
228 |
| - public void done(AVIMConversation conv,AVIMException e){ |
229 |
| - if(e==null){ |
230 |
| - AVIMImageMessage picture = new AVIMImageMessage(filePath); |
231 |
| - picture.setText("发自我的小米"); |
232 |
| - Map<String,Object> attributes = new HashMap<String,Object>(); |
233 |
| - attributes.put("location","旧金山"); |
234 |
| - picture.setAttribute(attributes); |
235 |
| - conv.sendMessage(picture,new AVIMConversationCallback(){ |
236 |
| - |
237 |
| - @Override |
238 |
| - public void done(AVIMException e){ |
239 |
| - if(e==null){ |
240 |
| - //发送成功! |
| 224 | + public void done(AVIMClient client, AVIMException e) { |
| 225 | + if (e == null) { |
| 226 | + //登录成功 |
| 227 | + // 创建对话,默认创建者是在包含在成员列表中的 |
| 228 | + client.createConversation(Arrays.asList("Jerry"), new AVIMConversationCreatedCallback() { |
| 229 | +
|
| 230 | + @Override |
| 231 | + public void done(AVIMConversation conv, AVIMException e) { |
| 232 | + if (e == null) { |
| 233 | + AVIMImageMessage picture = new AVIMImageMessage(filePath); |
| 234 | + picture.setText("发自我的小米"); |
| 235 | + Map < String, Object > attributes = new HashMap < String, Object > (); |
| 236 | + attributes.put("location", "旧金山"); |
| 237 | + picture.setAttribute(attributes); |
| 238 | + conv.sendMessage(picture, new AVIMConversationCallback() { |
| 239 | +
|
| 240 | + @Override |
| 241 | + public void done(AVIMException e) { |
| 242 | + if (e == null) { |
| 243 | + //发送成功! |
| 244 | + } |
241 | 245 | }
|
242 |
| - } |
243 |
| - }); |
| 246 | + }); |
| 247 | + } |
244 | 248 | }
|
245 |
| - } |
246 |
| - }); |
| 249 | + }); |
247 | 250 | }
|
248 | 251 | }
|
249 | 252 | });
|
|
0 commit comments