Android:仿微信源码

移动开发
对于个人开发者或者小企业来说,做IM用第三方的也是一种趋势吧,毕竟自己整一套可供运营使用的IM系统几乎是不切实际的(技术门槛和维护成本)。所以总结了以前的经验和收获,做了这套全仿微信的APP。

源码简介:测试账号:123456888 密码:123456888 对于个人开发者或者小企业来说,做IM用第三方的也是一种趋势吧,毕竟自己整一套可供运营使用的IM系统几乎是不切实际的(技术门槛和维护成本)。所以总结了以前的经验和收获,做了这套全仿微信的APP。总的来说,这里面大的架构还是沿用了环信官方提供的UIdemo,但是里面的很多的细节和大部分页面都是我精心按照微信的逻辑和UI设计进行编写的。功能上目前解决了好友体系、用户资料、单聊、群聊等社交功能,后续还会继续更新类似于更换聊天背景、搜索本地用户、朋友圈、更多聊天表情等功能。。。 1.注册登录部分: 1.1重写了EditText的默认的蓝色底线,变成微信的绿色 1.2 登录和注册按钮对输入框进行监听,并变色 1.3 密码明文和隐藏 2.主页 2.1全仿微信底部导航 2.2 右上角加号按钮可进入 “发起群聊”和“添加朋友“ 2.3 显示未读消息数和未读通知数 3.聊天列表页: 3.1 群聊头像是群成员头像的组合,有1.2.3.4.5种类型的头像 3.2 置顶功能。置顶后该会话item置于列表顶端,并像微信一样变底色 3.3 删除列表功能,删除该条会话记录 4.通讯录页 4.1 显示好友列表 4.2 进入还有申请通知 4.3 进入群聊列表 5.发现页(正在开发朋友圈功能.....) 6.用户详情页 6.1 资料设置(目前可更改的资料是:头像、昵称、性别) 6.2 微信号只能设置一次。 6.3 设置页(通知、声音、震动、退出登录) 7.用户申请通知 7.1按时间由近及远排序 7.2 显示申请理由 7.3 处理状态(已添加、同意) 8.查找添加用户 8.1 按用户的手机号查找用户 8.2 搜索的用户的存在显示用户资料 8.3 若该用户已经是好友,显示”发送消息“按钮 8.4 若该用户不是好友,显示添加好友,并要求输入申请理由 9.发起群聊 9.1可以在输入框内按用户昵称搜索好友 9.2 添加群聊的用户的头像可以动态显示在顶部 10.聊天页面 可以发送语音文字图片和视频(后续更新更多表情和设置聊天背景) 11聊天设置 11.1单聊:置顶聊天、免打扰、和清除聊天记录 11.2 群聊:显示群成员列表、增加群成员、删除群成员(群主可操作)、 修改群名称、置顶聊天、屏蔽清除群消息

源码效果:

源码片段:

  1. package com.fanxin.app.fx; 
  2.   
  3. import java.util.ArrayList; 
  4. import java.util.HashMap; 
  5. import java.util.List; 
  6. import java.util.Map; 
  7. import java.util.UUID; 
  8.   
  9. import com.alibaba.fastjson.JSONArray; 
  10. import com.alibaba.fastjson.JSONException; 
  11. import com.alibaba.fastjson.JSONObject; 
  12. import com.easemob.EMConnectionListener; 
  13. import com.easemob.EMError; 
  14. import com.easemob.chat.EMChat; 
  15. import com.easemob.chat.EMChatManager; 
  16. import com.easemob.chat.EMContactListener; 
  17. import com.easemob.chat.EMContactManager; 
  18. import com.easemob.chat.EMConversation; 
  19. import com.easemob.chat.EMGroupManager; 
  20. import com.easemob.chat.EMMessage; 
  21. import com.easemob.chat.EMNotifier; 
  22. import com.easemob.chat.GroupChangeListener; 
  23. import com.easemob.chat.TextMessageBody; 
  24. import com.easemob.chat.EMMessage.ChatType; 
  25. import com.easemob.chat.EMMessage.Type; 
  26. import com.fanxin.app.Constant; 
  27. import com.fanxin.app.DemoApplication; 
  28. import com.fanxin.app.R; 
  29. import com.fanxin.app.activity.BaseActivity; 
  30. import com.fanxin.app.db.InviteMessgeDao; 
  31. import com.fanxin.app.db.UserDao; 
  32. import com.fanxin.app.domain.InviteMessage; 
  33. import com.fanxin.app.domain.InviteMessage.InviteMesageStatus; 
  34. import com.fanxin.app.domain.User; 
  35. import com.fanxin.app.fx.others.LoadDataFromServer; 
  36. import com.fanxin.app.fx.others.LoadDataFromServer.DataCallBack; 
  37. import com.zdp.aseo.content.AseoZdpAseo; 
  38. import com.easemob.exceptions.EaseMobException; 
  39. import com.easemob.util.EMLog; 
  40. import com.easemob.util.HanziToPinyin; 
  41. import com.easemob.util.NetUtils; 
  42.   
  43. import android.annotation.SuppressLint; 
  44. import android.content.BroadcastReceiver; 
  45. import android.content.Context; 
  46. import android.content.DialogInterface; 
  47. import android.content.Intent; 
  48. import android.content.IntentFilter; 
  49. import android.os.Bundle; 
  50. import android.support.v4.app.Fragment; 
  51. import android.support.v4.app.FragmentTransaction; 
  52. import android.text.TextUtils; 
  53. import android.util.Log; 
  54. import android.view.KeyEvent; 
  55. import android.view.View; 
  56. import android.view.View.OnClickListener; 
  57. import android.widget.ImageView; 
  58. import android.widget.TextView; 
  59. import android.widget.Toast; 
  60.   
  61. @SuppressLint("DefaultLocale"
  62. public class MainActivity extends BaseActivity { 
  63.     // 未读消息textview 
  64.     private TextView unreadLabel; 
  65.     // 未读通讯录textview 
  66.     TextView unreadAddressLable; 
  67.     protected static final String TAG = "MainActivity"
  68.   
  69.     private Fragment[] fragments; 
  70.     public FragmentCoversation homefragment; 
  71.     private FragmentFriends contactlistfragment; 
  72.     private FragmentFind findfragment; 
  73.     private FragmentProfile profilefragment; 
  74.     private ImageView[] imagebuttons; 
  75.     private TextView[] textviews; 
  76.     private int index; 
  77.     // 当前fragment的index 
  78.     private int currentTabIndex; 
  79.     private NewMessageBroadcastReceiver msgReceiver; 
  80.     private android.app.AlertDialog.Builder conflictBuilder; 
  81.     private android.app.AlertDialog.Builder accountRemovedBuilder; 
  82.     private boolean isConflictDialogShow; 
  83.     private boolean isAccountRemovedDialogShow; 
  84.     // 账号在别处登录 
  85.     public boolean isConflict = false
  86.     // 账号被移除 
  87.     private boolean isCurrentAccountRemoved = false
  88.   
  89.     private InviteMessgeDao inviteMessgeDao; 
  90.     private UserDao userDao; 
  91.   
  92.     private ImageView iv_add; 
  93.     private ImageView iv_search; 
  94.   
  95.     /** 
  96.      * 检查当前用户是否被删除 
  97.      */ 
  98.     public boolean getCurrentAccountRemoved() { 
  99.         return isCurrentAccountRemoved; 
  100.     } 
  101.   
  102.     @Override 
  103.     protected void onCreate(Bundle savedInstanceState) { 
  104.         super.onCreate(savedInstanceState); 
  105.         if (savedInstanceState != null 
  106.                 && savedInstanceState.getBoolean(Constant.ACCOUNT_REMOVED, 
  107.                         false)) { 
  108.             // 防止被移除后,没点确定按钮然后按了home键,长期在后台又进app导致的crash 
  109.             // 三个fragment里加的判断同理 
  110.             DemoApplication.getInstance().logout(null); 
  111.             finish(); 
  112.             startActivity(new Intent(this, LoginActivity.class)); 
  113.             return
  114.         } else if (savedInstanceState != null 
  115.                 && savedInstanceState.getBoolean("isConflict"false)) { 
  116.             // 防止被T后,没点确定按钮然后按了home键,长期在后台又进app导致的crash 
  117.             // 三个fragment里加的判断同理 
  118.             finish(); 
  119.             startActivity(new Intent(this, LoginActivity.class)); 
  120.             return
  121.         } 
  122.         setContentView(R.layout.activity_mian_temp); 
  123.         initView(); 
  124.   
  125.         if (getIntent().getBooleanExtra("conflict"false
  126.                 && !isConflictDialogShow) { 
  127.             showConflictDialog(); 
  128.         } else if (getIntent().getBooleanExtra(Constant.ACCOUNT_REMOVED, false
  129.                 && !isAccountRemovedDialogShow) { 
  130.             showAccountRemovedDialog(); 
  131.         } 
  132.         AseoZdpAseo.init(this, AseoZdpAseo.SCREEN_TYPE); 
  133.         iv_add = (ImageView) this.findViewById(R.id.iv_add); 
  134.         iv_search = (ImageView) this.findViewById(R.id.iv_search); 
  135.         iv_add.setOnClickListener(new OnClickListener() { 
  136.   
  137.             @Override 
  138.             public void onClick(View v) { 
  139.                 AddPopWindow addPopWindow = new AddPopWindow(MainActivity.this); 
  140.                 addPopWindow.showPopupWindow(iv_add); 
  141.             } 
  142.   
  143.         }); 
  144.         iv_search.setOnClickListener(new OnClickListener() { 
  145.   
  146.             @Override 
  147.             public void onClick(View v) { 
  148.   
  149.             } 
  150.   
  151.         }); 
  152.   
  153.         TextView tv_online = (TextView) this.findViewById(R.id.tv_online); 
  154.         tv_online.setOnClickListener(new OnClickListener() { 
  155.   
  156.             @Override 
  157.             public void onClick(View v) { 
  158.                 startActivity(new Intent(MainActivity.this
  159.                         LasterLoginUserActivity.class)); 
  160.             } 
  161.   
  162.         }); 
  163.   
  164.     } 
  165.   
  166.     private void initView() { 
  167.         unreadLabel = (TextView) findViewById(R.id.unread_msg_number); 
  168.         unreadAddressLable = (TextView) findViewById(R.id.unread_address_number); 
  169.   
  170.         homefragment = new FragmentCoversation(); 
  171.         contactlistfragment = new FragmentFriends(); 
  172.         findfragment = new FragmentFind(); 
  173.         profilefragment = new FragmentProfile(); 
  174.         fragments = new Fragment[] { homefragment, contactlistfragment, 
  175.                 findfragment, profilefragment }; 
  176.         imagebuttons = new ImageView[4]; 
  177.         imagebuttons[0] = (ImageView) findViewById(R.id.ib_weixin); 
  178.         imagebuttons[1] = (ImageView) findViewById(R.id.ib_contact_list); 
  179.         imagebuttons[2] = (ImageView) findViewById(R.id.ib_find); 
  180.         imagebuttons[3] = (ImageView) findViewById(R.id.ib_profile); 
  181.         AseoZdpAseo.initBan(this, findViewById(R.id.aseo_brand)); 
  182.         imagebuttons[0].setSelected(true); 
  183.         textviews = new TextView[4]; 
  184.         textviews[0] = (TextView) findViewById(R.id.tv_weixin); 
  185.         textviews[1] = (TextView) findViewById(R.id.tv_contact_list); 
  186.         textviews[2] = (TextView) findViewById(R.id.tv_find); 
  187.         textviews[3] = (TextView) findViewById(R.id.tv_profile); 
  188.         textviews[0].setTextColor(0xFF45C01A); 
  189.         // 添加显示第一个fragment 
  190.         getSupportFragmentManager().beginTransaction() 
  191.                 .add(R.id.fragment_container, homefragment) 
  192.                 .add(R.id.fragment_container, contactlistfragment) 
  193.                 .add(R.id.fragment_container, profilefragment) 
  194.                 .add(R.id.fragment_container, findfragment) 
  195.                 .hide(contactlistfragment).hide(profilefragment) 
  196.                 .hide(findfragment).show(homefragment).commit(); 
  197.         inviteMessgeDao = new InviteMessgeDao(this); 
  198.         userDao = new UserDao(this); 
  199.   
  200.         // 注册一个接收消息的BroadcastReceiver 
  201.         msgReceiver = new NewMessageBroadcastReceiver(); 
  202.         IntentFilter intentFilter = new IntentFilter(EMChatManager 
  203.                 .getInstance().getNewMessageBroadcastAction()); 
  204.         intentFilter.setPriority(3); 
  205.         registerReceiver(msgReceiver, intentFilter); 
  206.   
  207.         // 注册一个ack回执消息的BroadcastReceiver 
  208.         IntentFilter ackMessageIntentFilter = new IntentFilter(EMChatManager 
  209.                 .getInstance().getAckMessageBroadcastAction()); 
  210.         ackMessageIntentFilter.setPriority(3); 
  211.         registerReceiver(ackMessageReceiver, ackMessageIntentFilter); 
  212.   
  213.         // 注册一个透传消息的BroadcastReceiver 
  214.         IntentFilter cmdMessageIntentFilter = new IntentFilter(EMChatManager 
  215.                 .getInstance().getCmdMessageBroadcastAction()); 
  216.         cmdMessageIntentFilter.setPriority(3); 
  217.         registerReceiver(cmdMessageReceiver, cmdMessageIntentFilter); 
  218.   
  219.         // 注册一个离线消息的BroadcastReceiver 
  220.         // IntentFilter offlineMessageIntentFilter = new 
  221.         // IntentFilter(EMChatManager.getInstance() 
  222.         // .getOfflineMessageBroadcastAction()); 
  223.         // registerReceiver(offlineMessageReceiver, offlineMessageIntentFilter); 
  224.   
  225.         // setContactListener监听联系人的变化等 
  226.         EMContactManager.getInstance().setContactListener( 
  227.                 new MyContactListener()); 
  228.         // 注册一个监听连接状态的listener 
  229.         EMChatManager.getInstance().addConnectionListener( 
  230.                 new MyConnectionListener()); 
  231.         // 注册群聊相关的listener 
  232.         EMGroupManager.getInstance().addGroupChangeListener( 
  233.                 new MyGroupChangeListener()); 
  234.         // 通知sdk,UI 已经初始化完毕,注册了相应的receiver和listener, 可以接受broadcast了 
  235.         EMChat.getInstance().setAppInited(); 
  236.   
  237.     } 
  238.   
  239.     public void onTabClicked(View view) { 
  240.         switch (view.getId()) { 
  241.         case R.id.re_weixin: 
  242.             index = 0
  243.             break
  244.         case R.id.re_contact_list: 
  245.             index = 1
  246.             break
  247.         case R.id.re_find: 
  248.             index = 2
  249.             break
  250.         case R.id.re_profile: 
  251.             index = 3
  252.             break
  253.   
  254.         } 
  255.   
  256.         if (currentTabIndex != index) { 
  257.             FragmentTransaction trx = getSupportFragmentManager() 
  258.                     .beginTransaction(); 
  259.             trx.hide(fragments[currentTabIndex]); 
  260.             if (!fragments[index].isAdded()) { 
  261.                 trx.add(R.id.fragment_container, fragments[index]); 
  262.             } 
  263.             trx.show(fragments[index]).commit(); 
  264.         } 
  265.         imagebuttons[currentTabIndex].setSelected(false); 
  266.         // 把当前tab设为选中状态 
  267.         imagebuttons[index].setSelected(true); 
  268.         textviews[currentTabIndex].setTextColor(0xFF999999); 
  269.         textviews[index].setTextColor(0xFF45C01A); 
  270.         currentTabIndex = index; 
  271.     } 
  272.   
  273.     /** 
  274.      * 显示帐号在别处登录dialog 
  275.      */ 
  276.     private void showConflictDialog() { 
  277.         isConflictDialogShow = true
  278.         DemoApplication.getInstance().logout(null); 
  279.         String st = getResources().getString(R.string.Logoff_notification); 
  280.         if (!MainActivity.this.isFinishing()) { 
  281.             // clear up global variables 
  282.             try { 
  283.                 if (conflictBuilder == null
  284.                     conflictBuilder = new android.app.AlertDialog.Builder( 
  285.                             MainActivity.this); 
  286.                 conflictBuilder.setTitle(st); 
  287.                 conflictBuilder.setMessage(R.string.connect_conflict); 
  288.                 conflictBuilder.setPositiveButton(R.string.ok, 
  289.                         new DialogInterface.OnClickListener() { 
  290.   
  291.                             @Override 
  292.                             public void onClick(DialogInterface dialog, 
  293.                                     int which) { 
  294.                                 dialog.dismiss(); 
  295.                                 conflictBuilder = null
  296.                                 finish(); 
  297.                                 startActivity(new Intent(MainActivity.this
  298.                                         LoginActivity.class)); 
  299.                             } 
  300.                         }); 
  301.                 conflictBuilder.setCancelable(false); 
  302.                 conflictBuilder.create().show(); 
  303.                 isConflict = true
  304.             } catch (Exception e) { 
  305.                 EMLog.e(TAG, 
  306.                         "---------color conflictBuilder error" + e.getMessage()); 
  307.             } 
  308.   
  309.         } 
  310.   
  311.     } 
  312.   
  313.     /** 
  314.      * 帐号被移除的dialog 
  315.      */ 
  316.     private void showAccountRemovedDialog() { 
  317.         isAccountRemovedDialogShow = true
  318.         DemoApplication.getInstance().logout(null); 
  319.         String st5 = getResources().getString(R.string.Remove_the_notification); 
  320.         if (!MainActivity.this.isFinishing()) { 
  321.             // clear up global variables 
  322.             try { 
  323.                 if (accountRemovedBuilder == null
  324.                     accountRemovedBuilder = new android.app.AlertDialog.Builder( 
  325.                             MainActivity.this); 
  326.                 accountRemovedBuilder.setTitle(st5); 
  327.                 accountRemovedBuilder.setMessage(R.string.em_user_remove); 
  328.                 accountRemovedBuilder.setPositiveButton(R.string.ok, 
  329.                         new DialogInterface.OnClickListener() { 
  330.   
  331.                             @Override 
  332.                             public void onClick(DialogInterface dialog, 
  333.                                     int which) { 
  334.                                 dialog.dismiss(); 
  335.                                 accountRemovedBuilder = null
  336.                                 finish(); 
  337.                                 startActivity(new Intent(MainActivity.this
  338.                                         LoginActivity.class)); 
  339.                             } 
  340.                         }); 
  341.                 accountRemovedBuilder.setCancelable(false); 
  342.                 accountRemovedBuilder.create().show(); 
  343.                 isCurrentAccountRemoved = true
  344.             } catch (Exception e) { 
  345.                 EMLog.e(TAG, 
  346.                         "---------color userRemovedBuilder error" 
  347.                                 + e.getMessage()); 
  348.             } 
  349.   
  350.         } 
  351.   
  352.     } 
  353.   
  354.     /** 
  355.      * 新消息广播接收者 
  356.      *  
  357.      *  
  358.      */ 
  359.     private class NewMessageBroadcastReceiver extends BroadcastReceiver { 
  360.         @Override 
  361.         public void onReceive(Context context, Intent intent) { 
  362.             // 主页面收到消息后,主要为了提示未读,实际消息内容需要到chat页面查看 
  363.   
  364.             String from = intent.getStringExtra("from"); 
  365.             // 消息id 
  366.             String msgId = intent.getStringExtra("msgid"); 
  367.             EMMessage message = EMChatManager.getInstance().getMessage(msgId); 
  368.             // 2014-10-22 修复在某些机器上,在聊天页面对方发消息过来时不立即显示内容的bug 
  369.             if (ChatActivity.activityInstance != null) { 
  370.                 if (message.getChatType() == ChatType.GroupChat) { 
  371.                     if (message.getTo().equals( 
  372.                             ChatActivity.activityInstance.getToChatUsername())) 
  373.                         return
  374.                 } else { 
  375.                     if (from.equals(ChatActivity.activityInstance 
  376.                             .getToChatUsername())) 
  377.                         return
  378.                 } 
  379.             } 
  380.   
  381.             // 注销广播接收者,否则在ChatActivity中会收到这个广播 
  382.             abortBroadcast(); 
  383.   
  384.             notifyNewMessage(message); 
  385.   
  386.             // 刷新bottom bar消息未读数 
  387.             updateUnreadLabel(); 
  388.             if (currentTabIndex == 0) { 
  389.                 // 当前页面如果为聊天历史页面,刷新此页面 
  390.                 if (homefragment != null) { 
  391.                     homefragment.refresh(); 
  392.                 } 
  393.             } 
  394.   
  395.         } 
  396.     } 
  397.   
  398.     /** 
  399.      * 消息回执BroadcastReceiver 
  400.      */ 
  401.     private BroadcastReceiver ackMessageReceiver = new BroadcastReceiver() { 
  402.   
  403.         @Override 
  404.         public void onReceive(Context context, Intent intent) { 
  405.             abortBroadcast(); 
  406.   
  407.             String msgid = intent.getStringExtra("msgid"); 
  408.             String from = intent.getStringExtra("from"); 
  409.   
  410.             EMConversation conversation = EMChatManager.getInstance() 
  411.                     .getConversation(from); 
  412.             if (conversation != null) { 
  413.                 // 把message设为已读 
  414.                 EMMessage msg = conversation.getMessage(msgid); 
  415.   
  416.                 if (msg != null) { 
  417.   
  418.                     // 2014-11-5 修复在某些机器上,在聊天页面对方发送已读回执时不立即显示已读的bug 
  419.                     if (ChatActivity.activityInstance != null) { 
  420.                         if (msg.getChatType() == ChatType.Chat) { 
  421.                             if (from.equals(ChatActivity.activityInstance 
  422.                                     .getToChatUsername())) 
  423.                                 return
  424.                         } 
  425.                     } 
  426.   
  427.                     msg.isAcked = true
  428.                 } 
  429.             } 
  430.   
  431.         } 
  432.     }; 
  433.   
  434.     /** 
  435.      * 透传消息BroadcastReceiver 
  436.      */ 
  437.     private BroadcastReceiver cmdMessageReceiver = new BroadcastReceiver() { 
  438.   
  439.         @Override 
  440.         public void onReceive(Context context, Intent intent) { 
  441.             abortBroadcast(); 
  442.             // EMLog.d(TAG, "收到透传消息"); 
  443.             // // 获取cmd message对象 
  444.             // 
  445.             // EMMessage message = intent.getParcelableExtra("message"); 
  446.             // // 获取消息body 
  447.             // CmdMessageBody cmdMsgBody = (CmdMessageBody) message.getBody(); 
  448.             // String action = cmdMsgBody.action;// 获取自定义action 
  449.             // 
  450.             // // 获取扩展属性 此处省略 
  451.             // // message.getStringAttribute(""); 
  452.             // EMLog.d(TAG, 
  453.             // String.format("透传消息:action:%s,message:%s", action, 
  454.             // message.toString())); 
  455.             // String st9 = getResources().getString( 
  456.             // R.string.receive_the_passthrough); 
  457.             // Toast.makeText(MainActivity.this, st9 + action, 
  458.             // Toast.LENGTH_SHORT) 
  459.             // .show(); 
  460.         } 
  461.     }; 
  462.   
  463.     /** 
  464.      * 离线消息BroadcastReceiver sdk 登录后,服务器会推送离线消息到client,这个receiver,是通知UI 
  465.      * 有哪些人发来了离线消息 UI 可以做相应的操作,比如下载用户信息 
  466.      */ 
  467.     // private BroadcastReceiver offlineMessageReceiver = new 
  468.     // BroadcastReceiver() { 
  469.     // 
  470.     // @Override 
  471.     // public void onReceive(Context context, Intent intent) { 
  472.     // String[] users = intent.getStringArrayExtra("fromuser"); 
  473.     // String[] groups = intent.getStringArrayExtra("fromgroup"); 
  474.     // if (users != null) { 
  475.     // for (String user : users) { 
  476.     // System.out.println("收到user离线消息:" + user); 
  477.     // } 
  478.     // } 
  479.     // if (groups != null) { 
  480.     // for (String group : groups) { 
  481.     // System.out.println("收到group离线消息:" + group); 
  482.     // } 
  483.     // } 
  484.     // } 
  485.     // }; 
  486.   
  487.     /*** 
  488.      * 好友变化listener 
  489.      *  
  490.      */ 
  491.     private class MyContactListener implements EMContactListener { 
  492.   
  493.         @Override 
  494.         public void onContactAdded(List<string> usernameList) { 
  495.   
  496.             refreshFriendsList(); 
  497.             // 刷新ui 
  498.             if (currentTabIndex == 1
  499.                 contactlistfragment.refresh(); 
  500.   
  501.         } 
  502.   
  503.         @Override 
  504.         public void onContactDeleted(final List<string> usernameList) { 
  505.             // 被删除 
  506.             Map<string, user=""> localUsers = DemoApplication.getInstance() 
  507.                     .getContactList(); 
  508.             for (String username : usernameList) { 
  509.                 localUsers.remove(username); 
  510.                 userDao.deleteContact(username); 
  511.                 inviteMessgeDao.deleteMessage(username); 
  512.             } 
  513.             runOnUiThread(new Runnable() { 
  514.                 public void run() { 
  515.                     // 如果正在与此用户的聊天页面 
  516.                     String st10 = getResources().getString( 
  517.                             R.string.have_you_removed); 
  518.                     if (ChatActivity.activityInstance != null 
  519.                             && usernameList 
  520.                                     .contains(ChatActivity.activityInstance 
  521.                                             .getToChatUsername())) { 
  522.                         Toast.makeText( 
  523.                                 MainActivity.this
  524.                                 ChatActivity.activityInstance 
  525.                                         .getToChatUsername() + st10, 
  526.                                 Toast.LENGTH_SHORT).show(); 
  527.                         ChatActivity.activityInstance.finish(); 
  528.                     } 
  529.                     updateUnreadLabel(); 
  530.                     // 刷新ui 
  531.                     if (currentTabIndex == 1
  532.                         contactlistfragment.refresh(); 
  533.                     else if (currentTabIndex == 0
  534.                         homefragment.refresh(); 
  535.                 } 
  536.             }); 
  537.   
  538.         } 
  539.   
  540.         @Override 
  541.         public void onContactInvited(String username, String reason) { 
  542.             // 接到邀请的消息,如果不处理(同意或拒绝),掉线后,服务器会自动再发过来,所以客户端不需要重复提醒 
  543.             List<invitemessage> msgs = inviteMessgeDao.getMessagesList(); 
  544.   
  545.             for (InviteMessage inviteMessage : msgs) { 
  546.                 if (inviteMessage.getGroupId() == null 
  547.                         && inviteMessage.getFrom().equals(username)) { 
  548.                     inviteMessgeDao.deleteMessage(username); 
  549.                 } 
  550.             } 
  551.             // 自己封装的javabean 
  552.             InviteMessage msg = new InviteMessage(); 
  553.             msg.setFrom(username); 
  554.             msg.setTime(System.currentTimeMillis()); 
  555.             msg.setReason(reason); 
  556.             Log.d(TAG, username + "请求加你为好友,reason: " + reason); 
  557.             // 设置相应status 
  558.             msg.setStatus(InviteMesageStatus.BEINVITEED); 
  559.             notifyNewIviteMessage(msg); 
  560.   
  561.         } 
  562.   
  563.         @Override 
  564.         public void onContactAgreed(final String username) { 
  565.   
  566.             List<invitemessage> msgs = inviteMessgeDao.getMessagesList(); 
  567.             for (InviteMessage inviteMessage : msgs) { 
  568.                 if (inviteMessage.getFrom().equals(username)) { 
  569.                     return
  570.                 } 
  571.             } 
  572.             runOnUiThread(new Runnable() { 
  573.                 public void run() { 
  574.   
  575.                     addFriendToList(username); 
  576.                 } 
  577.             }); 
  578.   
  579.         } 
  580.   
  581.         @Override 
  582.         public void onContactRefused(String username) { 
  583.             // 参考同意,被邀请实现此功能,demo未实现 
  584.             Log.d(username, username + "拒绝了你的好友请求"); 
  585.         } 
  586.   
  587.     } 
  588.   
  589.     /** 
  590.      * 保存提示新消息 
  591.      *  
  592.      * @param msg 
  593.      */ 
  594.     private void notifyNewIviteMessage(InviteMessage msg) { 
  595.         saveInviteMsg(msg); 
  596.         // 提示有新消息 
  597.         EMNotifier.getInstance(getApplicationContext()).notifyOnNewMsg(); 
  598.   
  599.         // 刷新bottom bar消息未读数 
  600.         updateUnreadAddressLable(); 
  601.         // 刷新好友页面ui 
  602.         if (currentTabIndex == 1
  603.             contactlistfragment.refresh(); 
  604.     } 
  605.   
  606.     /** 
  607.      * 保存邀请等msg 
  608.      *  
  609.      * @param msg 
  610.      */ 
  611.     private void saveInviteMsg(InviteMessage msg) { 
  612.         // 保存msg 
  613.         inviteMessgeDao.saveMessage(msg); 
  614.         // 未读数加1 
  615.         User user = DemoApplication.getInstance().getContactList() 
  616.                 .get(Constant.NEW_FRIENDS_USERNAME); 
  617.         if (user.getUnreadMsgCount() == 0
  618.             user.setUnreadMsgCount(user.getUnreadMsgCount() + 1); 
  619.     } 
  620.   
  621.     /** 
  622.      * set head 
  623.      *  
  624.      * @param username 
  625.      * @return 
  626.      */ 
  627.     @SuppressLint("DefaultLocale"
  628.     User setUserHead(String username) { 
  629.         User user = new User(); 
  630.         user.setUsername(username); 
  631.         String headerName = null
  632.         if (!TextUtils.isEmpty(user.getNick())) { 
  633.             headerName = user.getNick(); 
  634.         } else { 
  635.             headerName = user.getUsername(); 
  636.         } 
  637.         if (username.equals(Constant.NEW_FRIENDS_USERNAME)) { 
  638.             user.setHeader(""); 
  639.         } else if (Character.isDigit(headerName.charAt(0))) { 
  640.             user.setHeader("#"); 
  641.         } else { 
  642.             user.setHeader(HanziToPinyin.getInstance() 
  643.                     .get(headerName.substring(01)).get(0).target.substring(0
  644.                     1).toUpperCase()); 
  645.             char header = user.getHeader().toLowerCase().charAt(0); 
  646.             if (header < 'a' || header > 'z') { 
  647.                 user.setHeader("#"); 
  648.             } 
  649.         } 
  650.         return user; 
  651.     } 
  652.   
  653.     /** 
  654.      * 连接监听listener 
  655.      *  
  656.      */ 
  657.     private class MyConnectionListener implements EMConnectionListener { 
  658.   
  659.         @Override 
  660.         public void onConnected() { 
  661.             runOnUiThread(new Runnable() { 
  662.   
  663.                 @Override 
  664.                 public void run() { 
  665.                     homefragment.errorItem.setVisibility(View.GONE); 
  666.                 } 
  667.   
  668.             }); 
  669.         } 
  670.   
  671.         @Override 
  672.         public void onDisconnected(final int error) { 
  673.             final String st1 = getResources().getString( 
  674.                     R.string.Less_than_chat_server_connection); 
  675.             final String st2 = getResources().getString( 
  676.                     R.string.the_current_network); 
  677.             runOnUiThread(new Runnable() { 
  678.   
  679.                 @Override 
  680.                 public void run() { 
  681.                     if (error == EMError.USER_REMOVED) { 
  682.                         // 显示帐号已经被移除 
  683.                         showAccountRemovedDialog(); 
  684.                     } else if (error == EMError.CONNECTION_CONFLICT) { 
  685.                         // 显示帐号在其他设备登陆dialog 
  686.                         showConflictDialog(); 
  687.                     } else { 
  688.                         homefragment.errorItem.setVisibility(View.VISIBLE); 
  689.                         if (NetUtils.hasNetwork(MainActivity.this)) 
  690.                             homefragment.errorText.setText(st1); 
  691.                         else 
  692.                             homefragment.errorText.setText(st2); 
  693.   
  694.                     } 
  695.                 } 
  696.   
  697.             }); 
  698.         } 
  699.     } 
  700.   
  701.     /** 
  702.      * MyGroupChangeListener 
  703.      */ 
  704.     private class MyGroupChangeListener implements GroupChangeListener { 
  705.   
  706.         @Override 
  707.         public void onInvitationReceived(String groupId, String groupName, 
  708.                 String inviter, String reason) { 
  709.   
  710.             // 被邀请 
  711.             String st3 = getResources().getString( 
  712.                     R.string.Invite_you_to_join_a_group_chat); 
  713.             User user = DemoApplication.getInstance().getContactList() 
  714.                     .get(inviter); 
  715.             if (user != null) { 
  716.                 EMMessage msg = EMMessage.createReceiveMessage(Type.TXT); 
  717.                 msg.setChatType(ChatType.GroupChat); 
  718.                 msg.setFrom(inviter); 
  719.                 msg.setTo(groupId); 
  720.                 msg.setMsgId(UUID.randomUUID().toString()); 
  721.                 msg.addBody(new TextMessageBody(user.getNick() + st3)); 
  722.                 msg.setAttribute("useravatar", user.getAvatar()); 
  723.                 msg.setAttribute("usernick", user.getNick()); 
  724.                 // 保存邀请消息 
  725.                 EMChatManager.getInstance().saveMessage(msg); 
  726.                 // 提醒新消息 
  727.                 EMNotifier.getInstance(getApplicationContext()) 
  728.                         .notifyOnNewMsg(); 
  729.             } 
  730.             runOnUiThread(new Runnable() { 
  731.                 public void run() { 
  732.                     updateUnreadLabel(); 
  733.                     // 刷新ui 
  734.                     if (currentTabIndex == 0
  735.                         homefragment.refresh(); 
  736.                     // if (CommonUtils.getTopActivity(MainActivity.this).equals( 
  737.                     // GroupsActivity.class.getName())) { 
  738.                     // GroupsActivity.instance.onResume(); 
  739.                     // } 
  740.                 } 
  741.             }); 
  742.   
  743.         } 
  744.   
  745.         @Override 
  746.         public void onInvitationAccpted(String groupId, String inviter, 
  747.                 String reason) { 
  748.   
  749.         } 
  750.   
  751.         @Override 
  752.         public void onInvitationDeclined(String groupId, String invitee, 
  753.                 String reason) { 
  754.   
  755.         } 
  756.   
  757.         @Override 
  758.         public void onUserRemoved(String groupId, String groupName) { 
  759.             // 提示用户被T了,demo省略此步骤 
  760.             // 刷新ui 
  761.             runOnUiThread(new Runnable() { 
  762.                 public void run() { 
  763.                     try { 
  764.                         updateUnreadLabel(); 
  765.                         if (currentTabIndex == 0
  766.                             homefragment.refresh(); 
  767.                         // if (CommonUtils.getTopActivity(MainActivity.this) 
  768.                         // .equals(GroupsActivity.class.getName())) { 
  769.                         // GroupsActivity.instance.onResume(); 
  770.                         // } 
  771.                     } catch (Exception e) { 
  772.                         EMLog.e(TAG, "refresh exception " + e.getMessage()); 
  773.                     } 
  774.                 } 
  775.             }); 
  776.         } 
  777.   
  778.         @Override 
  779.         public void onGroupDestroy(String groupId, String groupName) { 
  780.             // 群被解散 
  781.             // 提示用户群被解散,demo省略 
  782.             // 刷新ui 
  783.             runOnUiThread(new Runnable() { 
  784.                 public void run() { 
  785.                     updateUnreadLabel(); 
  786.                     if (currentTabIndex == 0
  787.                         homefragment.refresh(); 
  788.                     // if (CommonUtils.getTopActivity(MainActivity.this).equals( 
  789.                     // GroupsActivity.class.getName())) { 
  790.                     // GroupsActivity.instance.onResume(); 
  791.                     // } 
  792.                 } 
  793.             }); 
  794.   
  795.         } 
  796.   
  797.         @Override 
  798.         public void onApplicationReceived(String groupId, String groupName, 
  799.                 String applyer, String reason) { 
  800.             // 用户申请加入群聊 
  801.             InviteMessage msg = new InviteMessage(); 
  802.             msg.setFrom(applyer); 
  803.             msg.setTime(System.currentTimeMillis()); 
  804.             msg.setGroupId(groupId); 
  805.             msg.setGroupName(groupName); 
  806.             msg.setReason(reason); 
  807.             Log.d(TAG, applyer + " 申请加入群聊:" + groupName); 
  808.             msg.setStatus(InviteMesageStatus.BEAPPLYED); 
  809.             notifyNewIviteMessage(msg); 
  810.         } 
  811.   
  812.         @Override 
  813.         public void onApplicationAccept(String groupId, String groupName, 
  814.                 String accepter) { 
  815.             String st4 = getResources().getString( 
  816.                     R.string.Agreed_to_your_group_chat_application); 
  817.             // 加群申请被同意 
  818.             EMMessage msg = EMMessage.createReceiveMessage(Type.TXT); 
  819.             msg.setChatType(ChatType.GroupChat); 
  820.             msg.setFrom(accepter); 
  821.             msg.setTo(groupId); 
  822.             msg.setMsgId(UUID.randomUUID().toString()); 
  823.             msg.addBody(new TextMessageBody(accepter + st4)); 
  824.             // 保存同意消息 
  825.             EMChatManager.getInstance().saveMessage(msg); 
  826.             // 提醒新消息 
  827.             EMNotifier.getInstance(getApplicationContext()).notifyOnNewMsg(); 
  828.   
  829.             runOnUiThread(new Runnable() { 
  830.                 public void run() { 
  831.                     updateUnreadLabel(); 
  832.                     // 刷新ui 
  833.                     if (currentTabIndex == 0
  834.                         homefragment.refresh(); 
  835.                     // if (CommonUtils.getTopActivity(MainActivity.this).equals( 
  836.                     // GroupsActivity.class.getName())) { 
  837.                     // GroupsActivity.instance.onResume(); 
  838.                     // } 
  839.                 } 
  840.             }); 
  841.         } 
  842.   
  843.         @Override 
  844.         public void onApplicationDeclined(String groupId, String groupName, 
  845.                 String decliner, String reason) { 
  846.             // 加群申请被拒绝,demo未实现 
  847.         } 
  848.   
  849.     } 
  850.   
  851.     @Override 
  852.     protected void onResume() { 
  853.         super.onResume(); 
  854.         if (!isConflict || !isCurrentAccountRemoved) { 
  855.             // initView(); 
  856.             updateUnreadLabel(); 
  857.             updateUnreadAddressLable(); 
  858.             EMChatManager.getInstance().activityResumed(); 
  859.         } 
  860.   
  861.     } 
  862.   
  863.     @Override 
  864.     protected void onSaveInstanceState(Bundle outState) { 
  865.         outState.putBoolean("isConflict", isConflict); 
  866.         outState.putBoolean(Constant.ACCOUNT_REMOVED, isCurrentAccountRemoved); 
  867.         super.onSaveInstanceState(outState); 
  868.     } 
  869.   
  870.     @Override 
  871.     protected void onNewIntent(Intent intent) { 
  872.         super.onNewIntent(intent); 
  873.         if (getIntent().getBooleanExtra("conflict"false
  874.                 && !isConflictDialogShow) { 
  875.             showConflictDialog(); 
  876.         } else if (getIntent().getBooleanExtra(Constant.ACCOUNT_REMOVED, false
  877.                 && !isAccountRemovedDialogShow) { 
  878.             showAccountRemovedDialog(); 
  879.         } 
  880.     } 
  881.   
  882.     /** 
  883.      * 刷新未读消息数 
  884.      */ 
  885.     public void updateUnreadLabel() { 
  886.         int count = getUnreadMsgCountTotal(); 
  887.         if (count > 0) { 
  888.             unreadLabel.setText(String.valueOf(count)); 
  889.             unreadLabel.setVisibility(View.VISIBLE); 
  890.         } else { 
  891.             unreadLabel.setVisibility(View.INVISIBLE); 
  892.         } 
  893.     } 
  894.   
  895.     /** 
  896.      * 获取未读申请与通知消息 
  897.      *  
  898.      * @return 
  899.      */ 
  900.     public int getUnreadAddressCountTotal() { 
  901.         int unreadAddressCountTotal = 0
  902.         if (DemoApplication.getInstance().getContactList() 
  903.                 .get(Constant.NEW_FRIENDS_USERNAME) != null
  904.             unreadAddressCountTotal = DemoApplication.getInstance() 
  905.                     .getContactList().get(Constant.NEW_FRIENDS_USERNAME) 
  906.                     .getUnreadMsgCount(); 
  907.         return unreadAddressCountTotal; 
  908.     } 
  909.   
  910.     /** 
  911.      * 刷新申请与通知消息数 
  912.      */ 
  913.     public void updateUnreadAddressLable() { 
  914.         runOnUiThread(new Runnable() { 
  915.             public void run() { 
  916.                 int count = getUnreadAddressCountTotal(); 
  917.                 if (count > 0) { 
  918.                     unreadAddressLable.setText(String.valueOf(count)); 
  919.                     unreadAddressLable.setVisibility(View.VISIBLE); 
  920.                 } else { 
  921.                     unreadAddressLable.setVisibility(View.INVISIBLE); 
  922.                 } 
  923.             } 
  924.         }); 
  925.   
  926.     } 
  927.   
  928.     /** 
  929.      * 获取未读消息数 
  930.      *  
  931.      * @return 
  932.      */ 
  933.     public int getUnreadMsgCountTotal() { 
  934.         int unreadMsgCountTotal = 0
  935.         unreadMsgCountTotal = EMChatManager.getInstance().getUnreadMsgsCount(); 
  936.         return unreadMsgCountTotal; 
  937.     } 
  938.   
  939.     public void refreshFriendsList() { 
  940.         List<string> usernames = new ArrayList<string>(); 
  941.         try { 
  942.             usernames = EMContactManager.getInstance().getContactUserNames(); 
  943.         } catch (EaseMobException e1) { 
  944.             e1.printStackTrace(); 
  945.         } 
  946.         if (usernames != null && usernames.size() > 0) { 
  947.             String totaluser = usernames.get(0); 
  948.             for (int i = 1; i < usernames.size(); i++) { 
  949.                 final String split = "66split88"
  950.                 totaluser += split + usernames.get(i); 
  951.             } 
  952.             totaluser = totaluser.replace(Constant.NEW_FRIENDS_USERNAME, ""); 
  953.             totaluser = totaluser.replace(Constant.GROUP_USERNAME, ""); 
  954.   
  955.             Map<string, string=""> map = new HashMap<string, string="">(); 
  956.   
  957.             map.put("uids", totaluser); 
  958.   
  959.             LoadDataFromServer task = new LoadDataFromServer(MainActivity.this
  960.                     Constant.URL_Friends, map); 
  961.   
  962.             task.getData(new DataCallBack() { 
  963.   
  964.                 @Override 
  965.                 public void onDataCallBack(JSONObject data) { 
  966.                     try { 
  967.                         int code = data.getInteger("code"); 
  968.                         if (code == 1) { 
  969.                             JSONArray josnArray = data.getJSONArray("friends"); 
  970.   
  971.                             saveFriends(josnArray); 
  972.   
  973.                         } 
  974.   
  975.                     } catch (JSONException e) { 
  976.                         Log.e("MainActivity""update friendsLiST ERROR"); 
  977.                         e.printStackTrace(); 
  978.                     } 
  979.                 } 
  980.             }); 
  981.         } 
  982.   
  983.     } 
  984.   
  985.     private void saveFriends(JSONArray josnArray) { 
  986.   
  987.         Map<string, user=""> map = new HashMap<string, user="">(); 
  988.   
  989.         if (josnArray != null) { 
  990.             for (int i = 0; i < josnArray.size(); i++) { 
  991.                 JSONObject json = (JSONObject) josnArray.getJSONObject(i); 
  992.                 try { 
  993.                     String hxid = json.getString("hxid"); 
  994.                     String fxid = json.getString("fxid"); 
  995.                     String nick = json.getString("nick"); 
  996.                     String avatar = json.getString("avatar"); 
  997.                     String sex = json.getString("sex"); 
  998.                     String region = json.getString("region"); 
  999.                     String sign = json.getString("sign"); 
  1000.                     String tel = json.getString("tel"); 
  1001.   
  1002.                     User user = new User(); 
  1003.                     user.setFxid(fxid); 
  1004.                     user.setUsername(hxid); 
  1005.                     user.setBeizhu(""); 
  1006.                     user.setNick(nick); 
  1007.                     user.setRegion(region); 
  1008.                     user.setSex(sex); 
  1009.                     user.setTel(tel); 
  1010.                     user.setSign(sign); 
  1011.                     user.setAvatar(avatar); 
  1012.                     setUserHearder(hxid, user); 
  1013.                     map.put(hxid, user); 
  1014.   
  1015.                 } catch (JSONException e) { 
  1016.                     // TODO Auto-generated catch block 
  1017.                     e.printStackTrace(); 
  1018.                 } 
  1019.   
  1020.             } 
  1021.         } 
  1022.         // 添加user"申请与通知" 
  1023.         User newFriends = new User(); 
  1024.         newFriends.setUsername(Constant.NEW_FRIENDS_USERNAME); 
  1025.         String strChat = getResources().getString( 
  1026.                 R.string.Application_and_notify); 
  1027.         newFriends.setNick(strChat); 
  1028.         newFriends.setBeizhu(""); 
  1029.         newFriends.setFxid(""); 
  1030.         newFriends.setHeader(""); 
  1031.         newFriends.setRegion(""); 
  1032.         newFriends.setSex(""); 
  1033.         newFriends.setTel(""); 
  1034.         newFriends.setSign(""); 
  1035.         newFriends.setAvatar(""); 
  1036.         map.put(Constant.NEW_FRIENDS_USERNAME, newFriends); 
  1037.         // 添加"群聊" 
  1038.         User groupUser = new User(); 
  1039.         String strGroup = getResources().getString(R.string.group_chat); 
  1040.         groupUser.setUsername(Constant.GROUP_USERNAME); 
  1041.         groupUser.setNick(strGroup); 
  1042.         groupUser.setHeader(""); 
  1043.         groupUser.setNick(strChat); 
  1044.         groupUser.setBeizhu(""); 
  1045.         groupUser.setFxid(""); 
  1046.         groupUser.setHeader(""); 
  1047.         groupUser.setRegion(""); 
  1048.         groupUser.setSex(""); 
  1049.         groupUser.setTel(""); 
  1050.         groupUser.setSign(""); 
  1051.         groupUser.setAvatar(""); 
  1052.         map.put(Constant.GROUP_USERNAME, groupUser); 
  1053.   
  1054.         // 存入内存 
  1055.         DemoApplication.getInstance().setContactList(map); 
  1056.         // 存入db 
  1057.         UserDao dao = new UserDao(MainActivity.this); 
  1058.         List<user> users = new ArrayList<user>(map.values()); 
  1059.         dao.saveContactList(users); 
  1060.   
  1061.     } 
  1062.   
  1063.     /** 
  1064.      * 设置hearder属性,方便通讯中对联系人按header分类显示,以及通过右侧ABCD...字母栏快速定位联系人 
  1065.      *  
  1066.      * @param username 
  1067.      * @param user 
  1068.      */ 
  1069.     @SuppressLint("DefaultLocale"
  1070.     protected void setUserHearder(String username, User user) { 
  1071.         String headerName = null
  1072.         if (!TextUtils.isEmpty(user.getNick())) { 
  1073.             headerName = user.getNick(); 
  1074.         } else { 
  1075.             headerName = user.getUsername(); 
  1076.         } 
  1077.         headerName = headerName.trim(); 
  1078.         if (username.equals(Constant.NEW_FRIENDS_USERNAME)) { 
  1079.             user.setHeader(""); 
  1080.         } else if (Character.isDigit(headerName.charAt(0))) { 
  1081.             user.setHeader("#"); 
  1082.         } else { 
  1083.             user.setHeader(HanziToPinyin.getInstance() 
  1084.                     .get(headerName.substring(01)).get(0).target.substring(0
  1085.                     1).toUpperCase()); 
  1086.             char header = user.getHeader().toLowerCase().charAt(0); 
  1087.             if (header < 'a' || header > 'z') { 
  1088.                 user.setHeader("#"); 
  1089.             } 
  1090.         } 
  1091.     } 
  1092.   
  1093.     private void addFriendToList(final String hxid) { 
  1094.         Map<string, string=""> map_uf = new HashMap<string, string="">(); 
  1095.         map_uf.put("hxid", hxid); 
  1096.         LoadDataFromServer task = new LoadDataFromServer(null
  1097.                 Constant.URL_Get_UserInfo, map_uf); 
  1098.         task.getData(new DataCallBack() { 
  1099.             @Override 
  1100.             public void onDataCallBack(JSONObject data) { 
  1101.                 try { 
  1102.   
  1103.                     int code = data.getInteger("code"); 
  1104.                     if (code == 1) { 
  1105.   
  1106.                         JSONObject json = data.getJSONObject("user"); 
  1107.                         if (json != null && json.size() != 0) { 
  1108.   
  1109.                         } 
  1110.                         String nick = json.getString("nick"); 
  1111.                         String avatar = json.getString("avatar"); 
  1112.   
  1113.                         String hxid = json.getString("hxid"); 
  1114.                         String fxid = json.getString("fxid"); 
  1115.                         String region = json.getString("region"); 
  1116.                         String sex = json.getString("sex"); 
  1117.                         String sign = json.getString("sign"); 
  1118.                         String tel = json.getString("tel"); 
  1119.                         User user = new User(); 
  1120.   
  1121.                         user.setUsername(hxid); 
  1122.                         user.setNick(nick); 
  1123.                         user.setAvatar(avatar); 
  1124.                         user.setFxid(fxid); 
  1125.                         user.setRegion(region); 
  1126.                         user.setSex(sex); 
  1127.                         user.setSign(sign); 
  1128.                         user.setTel(tel); 
  1129.                         setUserHearder(hxid, user); 
  1130.                         Map<string, user=""> userlist = DemoApplication 
  1131.                                 .getInstance().getContactList(); 
  1132.                         Map<string, user=""> map_temp = new HashMap<string, user="">(); 
  1133.                         map_temp.put(hxid, user); 
  1134.                         userlist.putAll(map_temp); 
  1135.                         // 存入内存 
  1136.                         DemoApplication.getInstance().setContactList(userlist); 
  1137.                         // 存入db 
  1138.                         UserDao dao = new UserDao(MainActivity.this); 
  1139.   
  1140.                         dao.saveContact(user); 
  1141.   
  1142.                         // 自己封装的javabean 
  1143.                         InviteMessage msg = new InviteMessage(); 
  1144.                         msg.setFrom(hxid); 
  1145.                         msg.setTime(System.currentTimeMillis()); 
  1146.   
  1147.                         String reason_temp = nick + "66split88" + avatar 
  1148.                                 + "66split88" 
  1149.                                 + String.valueOf(System.currentTimeMillis()) 
  1150.                                 + "66split88" + "已经同意请求"
  1151.                         msg.setReason(reason_temp); 
  1152.   
  1153.                         msg.setStatus(InviteMesageStatus.BEAGREED); 
  1154.                         User userTemp = DemoApplication.getInstance() 
  1155.                                 .getContactList() 
  1156.                                 .get(Constant.NEW_FRIENDS_USERNAME); 
  1157.                         if (userTemp != null 
  1158.                                 && userTemp.getUnreadMsgCount() == 0) { 
  1159.                             userTemp.setUnreadMsgCount(userTemp 
  1160.                                     .getUnreadMsgCount() + 1); 
  1161.                         } 
  1162.                         notifyNewIviteMessage(msg); 
  1163.                     } 
  1164.   
  1165.                 } catch (JSONException e) { 
  1166.   
  1167.                     e.printStackTrace(); 
  1168.                 } 
  1169.   
  1170.             } 
  1171.   
  1172.         }); 
  1173.   
  1174.     } 
  1175.   
  1176.     private long exitTime = 0
  1177.   
  1178.     @Override 
  1179.     public boolean onKeyDown(int keyCode, KeyEvent event) { 
  1180.         if (keyCode == KeyEvent.KEYCODE_BACK 
  1181.                 && event.getAction() == KeyEvent.ACTION_DOWN) { 
  1182.             if ((System.currentTimeMillis() - exitTime) > 2000) { 
  1183.                 Toast.makeText(getApplicationContext(), "再按一次退出程序"
  1184.                         Toast.LENGTH_SHORT).show(); 
  1185.                 exitTime = System.currentTimeMillis(); 
  1186.             } else { 
  1187.                 Intent intent = new Intent(Intent.ACTION_MAIN); 
  1188.                 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
  1189.                 intent.addCategory(Intent.CATEGORY_HOME); 
  1190.                 AseoZdpAseo.initFinalTimer(this, AseoZdpAseo.BOTH_TYPE); 
  1191.                 startActivity(intent); 
  1192.   
  1193.             } 
  1194.             return true
  1195.         } 
  1196.         return super.onKeyDown(keyCode, event); 
  1197.     } 
  1198.   
  1199. </string,></string,></string,></string,></string,></user></user></string,></string,></string,></string,></string></string></invitemessage></invitemessage></string,></string></string> 

下载地址:http://down.51cto.com/data/2109302

责任编辑:倪明 来源: devstore
相关推荐

2015-02-28 15:15:47

插件Android桌面插件

2015-02-06 18:29:09

仿滴滴打车android

2015-01-22 15:44:55

Android源码音乐播放器

2015-10-12 11:19:35

墨迹天气引导界面android

2015-03-30 14:24:06

网易布局

2015-10-20 16:01:47

华为商城源码android

2015-10-12 11:25:20

android大众点评下拉动画

2015-09-22 10:43:37

微信雷达

2015-01-21 15:28:16

Android源码新浪微博

2015-01-09 11:49:26

Android源码下载

2015-08-07 15:39:26

仿微信语音界面源码

2016-02-15 11:47:54

微信源码下拉视频

2015-08-07 15:32:19

欢迎界面仿微信仿qq空间

2014-12-17 15:01:01

滚动视图Instagram源码

2015-09-01 17:09:41

uc浏览器源码

2014-08-18 14:18:07

Android桌面悬浮

2021-11-23 10:00:55

鸿蒙HarmonyOS应用

2015-09-01 16:55:42

微信朋友圈图片

2013-08-02 10:20:03

android时间轴

2015-01-19 12:19:04

iOS源码ActionSheet仿QQ音乐
点赞
收藏

51CTO技术栈公众号