{"action":"created","comment":{"url":"https://api.github.com/repos/leancloud/docs/pulls/comments/26364051","id":26364051,"diff_hunk":"@@ -0,0 +1,774 @@\n+# Android 实时通信服务(v2)\n+\n+## 简介\n+\n+在阅读本开发指南之前，请先阅读下[《实时通信开发指南(v2)》](./realtime_v2.html)，了解实时通信的基本概念和模型。\n+\n+一对一的文本聊天\n+------\n+\n+我们先从最简单的环节入手，看看怎么用 LeanCloud IM SDK v2 实现一对一文本聊天。\n+\n+###初始化\n+\n+和 LeanCloud 其他服务一样，实时聊天服务的初始化也是在 Application 的 onCreate 方法中进行的：\n+\n+```\n+public class MyApplication extends Application{\n+\n+    public void onCreate(){\n+        AVOSCloud.initialize(this,\"{{appId}}\",\"{{appKey}}\");\n+    }\n+}\n+```\n+\n+并且在AndroidManifest.xml中间声明：\n+\n+```\n+<manifest ...\n+\n+   <application\n+        android:name=\".MyApplication\"\n+        ....>\n+\n+        <service android:name=\"com.avos.avoscloud.PushService\" />\n+\n+        <receiver android:name=\"com.avos.avoscloud.AVBroadcastReceiver\">\n+            <intent-filter>\n+                <action android:name=\"android.intent.action.BOOT_COMPLETED\" />\n+                <action android:name=\"android.intent.action.USER_PRESENT\" />\n+            </intent-filter>\n+        </receiver>\n+\n+   </application>\n+\n+</manifest>\n+```\n+\n+接下来我们需要完成用户登录。\n+\n+###登录\n+\n+假定聊天发起方名叫 Tom，为直观起见，我们使用用户名来作为 `clientId` 登录聊天系统，代码如下：\n+\n+```\n+AVIMClient imClient = AVIMClient.getInstance(\"Tom\");\n+imClient.open(new IMClientCallback(){\n+  @Override\n+  public void done(AVIMClient client, AVException e) {\n+    if (e) {\n+        // 出错了，可能是网络问题无法连接 LeanCloud 云端，请检查网络之后重试。\n+        // 此时聊天服务不可用。\n+    } else {\n+        // 成功登录，可以开始进行聊天了。\n+    };\n+  }\n+});\n+```\n+\n+### 建立对话\n+\n+假定我们要跟「Bob」这个用户进行聊天，我们先创建一个对话，代码如下：\n+\n+```\n+List<String> clientIds = new ArrayList<String>();\n+clientIds.add(\"Tom\");\n+clientIds.add(\"Bob\");\n+\n+AVIMConversationQuery conversationQuery = imClient.getQuery();\n+conversationQuery.whereContainsAll(\"m\", clientIds);\n+// 之前有常量定义：\n+// const int ConversationType_OneOne = 0;\n+// const int ConversationType_Group = 1;\n+conversationQuery.whereEqualTo(\"type\", ConversationType_OneOne);\n+\n+conversationQuery.findInBackground(new AVIMConversationQueryCallback(){\n+  @Override\n+  public void done(List<AVIMConversation> conversations, AVException e) {\n+    if (null != e) {\n+      // 出错了。。。\n+    } else if (null != conversations && conversations.size() > 0){\n+      // 已经有一个和 Bob 的对话存在，继续在这一对话中聊天\n+      ...\n+    } else {\n+      // 不曾和 Bob 聊过，新建一个对话\n+      Map<String, Object> attr = new HashMap<String, Object>();\n+      attr.put(\"type\", ConversationType_OneOne);\n+      imClient.createConversation(clientIds, attr, new AVIMConversationCreatedCallback() {\n+        @Override\n+        public void done(AVIMConversation conversation, AVException e) {\n+          if (null != conversation) {\n+            // 成功了！\n+          }\n+        }\n+      });\n+    }\n+  }\n+});\n+```\n+\n+###发送消息\n+\n+建立好对话之后，要发送消息是很简单的：\n+\n+```\n+AVIMMessage message = new AVIMMessage();\n+message.setContent(\"hello\");\n+conversation.sendMessage(message, new AVIMConversationCallback() {\n+  @Override\n+  public void done(AVException e) {\n+    if (null != e) {\n+       // 出错了。。。\n+    } else {\n+    }\n+  }\n+});\n+```\n+\n+好了，这样一条消息就发送过去了。但是问题来了，对于「Bob」而言，他怎么才能收到别人发给他的消息呢？\n+\n+### 消息接收\n+\n+在 Bob 这一端，要能接收到消息，需要如下几步：\n+\n+1，进行初始化和登录，代码与发送端并无二致；\n+\n+2，实现自己的 `AVIMMessageHandler`，响应新消息到达通知，主要是如下函数：\n+\n+`public abstract void onMessage(AVIMMessage message, AVIMConversation conversation);`\n+\n+对于 Tom 发过来的消息，要显示出来，我们只需实现 `onMessage` 即可，示例代码如下：\n+\n+```\n+AVIMMessageManager.registerDefaultMessageHandler(new AVIMMessageHandler() {\n+  @Override\n+  public void onMessage(AVIMMessage message, AVIMConversation conversation) {\n+    // 新消息到来了\n+  }\n+\n+  @Override\n+  public void onMessageReceipt(AVIMMessage message, AVIMConversation conversation) {\n+    // 消息已经被接收。\n+  }\n+});\n+```\n+\n+几个主要的回调接口","path":"md/android_realtime_v2.md","position":156,"original_position":156,"commit_id":"4e15f575a1277405a9e078134f7f80251cbc37e1","original_commit_id":"4e15f575a1277405a9e078134f7f80251cbc37e1","user":{"login":"killme2008","id":14142,"avatar_url":"https://avatars.githubusercontent.com/u/14142?v=3","gravatar_id":"","url":"https://api.github.com/users/killme2008","html_url":"https://github.com/killme2008","followers_url":"https://api.github.com/users/killme2008/followers","following_url":"https://api.github.com/users/killme2008/following{/other_user}","gists_url":"https://api.github.com/users/killme2008/gists{/gist_id}","starred_url":"https://api.github.com/users/killme2008/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/killme2008/subscriptions","organizations_url":"https://api.github.com/users/killme2008/orgs","repos_url":"https://api.github.com/users/killme2008/repos","events_url":"https://api.github.com/users/killme2008/events{/privacy}","received_events_url":"https://api.github.com/users/killme2008/received_events","type":"User","site_admin":false},"body":"加上 `###` 标题。","created_at":"2015-03-13T04:17:02Z","updated_at":"2015-03-13T04:17:02Z","html_url":"https://github.com/leancloud/docs/pull/126#discussion_r26364051","pull_request_url":"https://api.github.com/repos/leancloud/docs/pulls/126","_links":{"self":{"href":"https://api.github.com/repos/leancloud/docs/pulls/comments/26364051"},"html":{"href":"https://github.com/leancloud/docs/pull/126#discussion_r26364051"},"pull_request":{"href":"https://api.github.com/repos/leancloud/docs/pulls/126"}}},"pull_request":{"url":"https://api.github.com/repos/leancloud/docs/pulls/126","id":31021004,"html_url":"https://github.com/leancloud/docs/pull/126","diff_url":"https://github.com/leancloud/docs/pull/126.diff","patch_url":"https://github.com/leancloud/docs/pull/126.patch","issue_url":"https://api.github.com/repos/leancloud/docs/issues/126","number":126,"state":"open","locked":false,"title":"add android realtime v2 guide","user":{"login":"jwfing","id":985038,"avatar_url":"https://avatars.githubusercontent.com/u/985038?v=3","gravatar_id":"","url":"https://api.github.com/users/jwfing","html_url":"https://github.com/jwfing","followers_url":"https://api.github.com/users/jwfing/followers","following_url":"https://api.github.com/users/jwfing/following{/other_user}","gists_url":"https://api.github.com/users/jwfing/gists{/gist_id}","starred_url":"https://api.github.com/users/jwfing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jwfing/subscriptions","organizations_url":"https://api.github.com/users/jwfing/orgs","repos_url":"https://api.github.com/users/jwfing/repos","events_url":"https://api.github.com/users/jwfing/events{/privacy}","received_events_url":"https://api.github.com/users/jwfing/received_events","type":"User","site_admin":false},"body":"@lzwjava @lbt05 ","created_at":"2015-03-12T09:56:20Z","updated_at":"2015-03-13T04:17:02Z","closed_at":null,"merged_at":null,"merge_commit_sha":"aae35d3d3b798d922193ab085b999b263cb3bf3f","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/leancloud/docs/pulls/126/commits","review_comments_url":"https://api.github.com/repos/leancloud/docs/pulls/126/comments","review_comment_url":"https://api.github.com/repos/leancloud/docs/pulls/comments{/number}","comments_url":"https://api.github.com/repos/leancloud/docs/issues/126/comments","statuses_url":"https://api.github.com/repos/leancloud/docs/statuses/4e15f575a1277405a9e078134f7f80251cbc37e1","head":{"label":"jwfing:master","ref":"master","sha":"4e15f575a1277405a9e078134f7f80251cbc37e1","user":{"login":"jwfing","id":985038,"avatar_url":"https://avatars.githubusercontent.com/u/985038?v=3","gravatar_id":"","url":"https://api.github.com/users/jwfing","html_url":"https://github.com/jwfing","followers_url":"https://api.github.com/users/jwfing/followers","following_url":"https://api.github.com/users/jwfing/following{/other_user}","gists_url":"https://api.github.com/users/jwfing/gists{/gist_id}","starred_url":"https://api.github.com/users/jwfing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jwfing/subscriptions","organizations_url":"https://api.github.com/users/jwfing/orgs","repos_url":"https://api.github.com/users/jwfing/repos","events_url":"https://api.github.com/users/jwfing/events{/privacy}","received_events_url":"https://api.github.com/users/jwfing/received_events","type":"User","site_admin":false},"repo":{"id":29513312,"name":"docs","full_name":"jwfing/docs","owner":{"login":"jwfing","id":985038,"avatar_url":"https://avatars.githubusercontent.com/u/985038?v=3","gravatar_id":"","url":"https://api.github.com/users/jwfing","html_url":"https://github.com/jwfing","followers_url":"https://api.github.com/users/jwfing/followers","following_url":"https://api.github.com/users/jwfing/following{/other_user}","gists_url":"https://api.github.com/users/jwfing/gists{/gist_id}","starred_url":"https://api.github.com/users/jwfing/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jwfing/subscriptions","organizations_url":"https://api.github.com/users/jwfing/orgs","repos_url":"https://api.github.com/users/jwfing/repos","events_url":"https://api.github.com/users/jwfing/events{/privacy}","received_events_url":"https://api.github.com/users/jwfing/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/jwfing/docs","description":"LeanCloud Documentation","fork":true,"url":"https://api.github.com/repos/jwfing/docs","forks_url":"https://api.github.com/repos/jwfing/docs/forks","keys_url":"https://api.github.com/repos/jwfing/docs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/jwfing/docs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/jwfing/docs/teams","hooks_url":"https://api.github.com/repos/jwfing/docs/hooks","issue_events_url":"https://api.github.com/repos/jwfing/docs/issues/events{/number}","events_url":"https://api.github.com/repos/jwfing/docs/events","assignees_url":"https://api.github.com/repos/jwfing/docs/assignees{/user}","branches_url":"https://api.github.com/repos/jwfing/docs/branches{/branch}","tags_url":"https://api.github.com/repos/jwfing/docs/tags","blobs_url":"https://api.github.com/repos/jwfing/docs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/jwfing/docs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/jwfing/docs/git/refs{/sha}","trees_url":"https://api.github.com/repos/jwfing/docs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/jwfing/docs/statuses/{sha}","languages_url":"https://api.github.com/repos/jwfing/docs/languages","stargazers_url":"https://api.github.com/repos/jwfing/docs/stargazers","contributors_url":"https://api.github.com/repos/jwfing/docs/contributors","subscribers_url":"https://api.github.com/repos/jwfing/docs/subscribers","subscription_url":"https://api.github.com/repos/jwfing/docs/subscription","commits_url":"https://api.github.com/repos/jwfing/docs/commits{/sha}","git_commits_url":"https://api.github.com/repos/jwfing/docs/git/commits{/sha}","comments_url":"https://api.github.com/repos/jwfing/docs/comments{/number}","issue_comment_url":"https://api.github.com/repos/jwfing/docs/issues/comments{/number}","contents_url":"https://api.github.com/repos/jwfing/docs/contents/{+path}","compare_url":"https://api.github.com/repos/jwfing/docs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/jwfing/docs/merges","archive_url":"https://api.github.com/repos/jwfing/docs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/jwfing/docs/downloads","issues_url":"https://api.github.com/repos/jwfing/docs/issues{/number}","pulls_url":"https://api.github.com/repos/jwfing/docs/pulls{/number}","milestones_url":"https://api.github.com/repos/jwfing/docs/milestones{/number}","notifications_url":"https://api.github.com/repos/jwfing/docs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/jwfing/docs/labels{/name}","releases_url":"https://api.github.com/repos/jwfing/docs/releases{/id}","created_at":"2015-01-20T05:21:44Z","updated_at":"2015-03-12T10:38:25Z","pushed_at":"2015-03-12T10:38:25Z","git_url":"git://github.com/jwfing/docs.git","ssh_url":"git@github.com:jwfing/docs.git","clone_url":"https://github.com/jwfing/docs.git","svn_url":"https://github.com/jwfing/docs","homepage":"http://leancloud.cn/docs/","size":16392,"stargazers_count":0,"watchers_count":0,"language":"HTML","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":1,"mirror_url":null,"open_issues_count":0,"forks":1,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"leancloud:master","ref":"master","sha":"0be4f67eae791bd99e54b94bf86a829a62cce242","user":{"login":"leancloud","id":6660518,"avatar_url":"https://avatars.githubusercontent.com/u/6660518?v=3","gravatar_id":"","url":"https://api.github.com/users/leancloud","html_url":"https://github.com/leancloud","followers_url":"https://api.github.com/users/leancloud/followers","following_url":"https://api.github.com/users/leancloud/following{/other_user}","gists_url":"https://api.github.com/users/leancloud/gists{/gist_id}","starred_url":"https://api.github.com/users/leancloud/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leancloud/subscriptions","organizations_url":"https://api.github.com/users/leancloud/orgs","repos_url":"https://api.github.com/users/leancloud/repos","events_url":"https://api.github.com/users/leancloud/events{/privacy}","received_events_url":"https://api.github.com/users/leancloud/received_events","type":"Organization","site_admin":false},"repo":{"id":25067518,"name":"docs","full_name":"leancloud/docs","owner":{"login":"leancloud","id":6660518,"avatar_url":"https://avatars.githubusercontent.com/u/6660518?v=3","gravatar_id":"","url":"https://api.github.com/users/leancloud","html_url":"https://github.com/leancloud","followers_url":"https://api.github.com/users/leancloud/followers","following_url":"https://api.github.com/users/leancloud/following{/other_user}","gists_url":"https://api.github.com/users/leancloud/gists{/gist_id}","starred_url":"https://api.github.com/users/leancloud/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/leancloud/subscriptions","organizations_url":"https://api.github.com/users/leancloud/orgs","repos_url":"https://api.github.com/users/leancloud/repos","events_url":"https://api.github.com/users/leancloud/events{/privacy}","received_events_url":"https://api.github.com/users/leancloud/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/leancloud/docs","description":"LeanCloud Documentation","fork":false,"url":"https://api.github.com/repos/leancloud/docs","forks_url":"https://api.github.com/repos/leancloud/docs/forks","keys_url":"https://api.github.com/repos/leancloud/docs/keys{/key_id}","collaborators_url":"https://api.github.com/repos/leancloud/docs/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/leancloud/docs/teams","hooks_url":"https://api.github.com/repos/leancloud/docs/hooks","issue_events_url":"https://api.github.com/repos/leancloud/docs/issues/events{/number}","events_url":"https://api.github.com/repos/leancloud/docs/events","assignees_url":"https://api.github.com/repos/leancloud/docs/assignees{/user}","branches_url":"https://api.github.com/repos/leancloud/docs/branches{/branch}","tags_url":"https://api.github.com/repos/leancloud/docs/tags","blobs_url":"https://api.github.com/repos/leancloud/docs/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/leancloud/docs/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/leancloud/docs/git/refs{/sha}","trees_url":"https://api.github.com/repos/leancloud/docs/git/trees{/sha}","statuses_url":"https://api.github.com/repos/leancloud/docs/statuses/{sha}","languages_url":"https://api.github.com/repos/leancloud/docs/languages","stargazers_url":"https://api.github.com/repos/leancloud/docs/stargazers","contributors_url":"https://api.github.com/repos/leancloud/docs/contributors","subscribers_url":"https://api.github.com/repos/leancloud/docs/subscribers","subscription_url":"https://api.github.com/repos/leancloud/docs/subscription","commits_url":"https://api.github.com/repos/leancloud/docs/commits{/sha}","git_commits_url":"https://api.github.com/repos/leancloud/docs/git/commits{/sha}","comments_url":"https://api.github.com/repos/leancloud/docs/comments{/number}","issue_comment_url":"https://api.github.com/repos/leancloud/docs/issues/comments{/number}","contents_url":"https://api.github.com/repos/leancloud/docs/contents/{+path}","compare_url":"https://api.github.com/repos/leancloud/docs/compare/{base}...{head}","merges_url":"https://api.github.com/repos/leancloud/docs/merges","archive_url":"https://api.github.com/repos/leancloud/docs/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/leancloud/docs/downloads","issues_url":"https://api.github.com/repos/leancloud/docs/issues{/number}","pulls_url":"https://api.github.com/repos/leancloud/docs/pulls{/number}","milestones_url":"https://api.github.com/repos/leancloud/docs/milestones{/number}","notifications_url":"https://api.github.com/repos/leancloud/docs/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/leancloud/docs/labels{/name}","releases_url":"https://api.github.com/repos/leancloud/docs/releases{/id}","created_at":"2014-10-11T07:14:37Z","updated_at":"2015-03-12T12:44:24Z","pushed_at":"2015-03-12T12:44:24Z","git_url":"git://github.com/leancloud/docs.git","ssh_url":"git@github.com:leancloud/docs.git","clone_url":"https://github.com/leancloud/docs.git","svn_url":"https://github.com/leancloud/docs","homepage":"http://leancloud.cn/docs/","size":18917,"stargazers_count":32,"watchers_count":32,"language":"HTML","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":34,"mirror_url":null,"open_issues_count":10,"forks":34,"open_issues":10,"watchers":32,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/leancloud/docs/pulls/126"},"html":{"href":"https://github.com/leancloud/docs/pull/126"},"issue":{"href":"https://api.github.com/repos/leancloud/docs/issues/126"},"comments":{"href":"https://api.github.com/repos/leancloud/docs/issues/126/comments"},"review_comments":{"href":"https://api.github.com/repos/leancloud/docs/pulls/126/comments"},"review_comment":{"href":"https://api.github.com/repos/leancloud/docs/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/leancloud/docs/pulls/126/commits"},"statuses":{"href":"https://api.github.com/repos/leancloud/docs/statuses/4e15f575a1277405a9e078134f7f80251cbc37e1"}}}}