容联云通讯---进微信会话界面,主动同步服务器至本地数据库

进入微信,点击发消息,进入会话语聊界面接收Hook组发来的广播

package com.godinsec.seland.updatecontacts;

import com.godinsec.seland.immsUtils.LogUtils;
import com.godinsec.seland.sqlite.VChatSipInfoStorage;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

@SuppressWarnings("deprecation")
public class SipConfigWeiXinSyncServer extends BroadcastReceiver {
	//自己的广播接收器action值---接受对方发来的广播!!!!!!  
    private final static String SIPCONFIG_WEIXIN_SYNC_SERVER_REQUEST = "com.godinec.seland.vertu.SipConfigWeiXinSyncServer"; 
    private final static String REFRESH_VCHAT_CONTACT_INFO = "com.godinsec.seland.action.ACTION_BIND_THREE_ID";
    private Context context;
    private Handler mHandler = new Handler(){
    	public void handleMessage(android.os.Message msg) {
    		switch (msg.what) {
			case 0:
				String[] weixinArray = (String[]) msg.obj;
	    		if (weixinArray==null) {
	    			return;
				}
	    		ContactController contactController = new ContactController(context);
	    		contactController.doRefreshPullDownListView(weixinArray);
				break;
			case 1:
				String[] vchatIds = VChatSipInfoStorage.getInstance().queryAllVChatIdFromWxInfo();
				if(vchatIds != null){
					ContactController controller = new ContactController(context);
					controller.doRefreshPullDownListView(vchatIds);
				}else{
					LogUtils.d("SipConfigWeiXinSyncServer", "------->vchatIds is null");
				}
				break;
			}
    	};
    };
	@Override
	public void onReceive(Context context, Intent intent) {
		this.context = context;
		if(intent != null){ 
			//接受广播,进行实时更新,同步服务器weixin
			if(SIPCONFIG_WEIXIN_SYNC_SERVER_REQUEST.equals(intent.getAction())){
				String[] weixinArray = intent.getStringArrayExtra("weixinArray");
				Log.e("TAG", "weixinArray getted");
				Log.e("TAG", ""+weixinArray.length+"---"+weixinArray[0]+"---"+weixinArray[1]);
				Message msg = mHandler.obtainMessage();
				msg.what = 0;
				msg.obj = weixinArray;
				mHandler.sendMessage(msg);
			}
        }  
	}

}

配置文件如下

    <!-- 即时同步服务器sip_config表的微信状态 -->
        <receiver android:name="com.godinsec.seland.updatecontacts.SipConfigWeiXinSyncServer" >
            <intent-filter>
                <action android:name="com.godinec.seland.vertu.SipConfigWeiXinSyncServer" />
            </intent-filter>
        </receiver>

contactController.doRefreshPullDownListView(weixinArray);方法如下

	/**
	 * 主动刷新功能--同步服务器
	 */
	public void doRefreshPullDownListView(String[] getWeixinIds) {
		if (!ProtocolUtil.isConnected()) {
			mContactUIHandler.sendEmptyMessage(codeConnectFailed);
			return;
		}
		if (Global.IsActive) {
			String[] weixinIds = getWeixinIds;

			ArrayList<String> weixinIdsDB = (ArrayList<String>) VChatSipInfoStorage
					.getInstance().queryAllVChatIdsFromVChatSipConfig();
			if (weixinIdsDB == null) {
				weixinIdsDB = new ArrayList<String>();
			}
			if (weixinIds != null) {
				for (String weixinId : weixinIds) {
					if (weixinIdsDB.contains(weixinId)) {
						continue;
					}
					weixinIdsDB.add(weixinId);
				}
			}

			String[] array = weixinIdsDB.toArray(new String[] {});
			if (array == null || array.length <= 0) {
				mContactUIHandler.sendEmptyMessage(codeNocontactRefreshfailed);
				return;
			}
			Log.e("TAG",
					"===============doRefreshPullDownListView==============");
			for (int i = 0; i < array.length; i++) {
				Log.e("TAG", "contactcontroller array[i]:" + array[i]);
			}
			mContactUIController.doRequestUserWeiXinIdVoIPinfo(array);
		} else {
			mContactUIHandler.sendEmptyMessage(codeNoactiveRefreshfailed);
		}
	}

doRequestUserWeiXinIdVoIPinfo方法如下

ContactUIController代码中有如下的中转方法

@Override
	public void doRequestUserWeiXinIdVoIPinfo(String[] weixinIds) {
		ContactService.getInstance().doRequestUserWeiXinVoIPinfo(this, weixinIds);
	}

	/**
	 * 同步服务器微信id到本地数据库
	 */
	public void doRequestUserWeiXinVoIPinfo(UICallback callback, String... wixinids) {
		Resource res = new Resource(callback);
		String myWeixinId = SQLiteManager.getInstance().queryWeixinIdOfMyself();
		String godinIdByVChatId = SQLiteManager.getInstance().queryGodinId();
		String userid = CasApplication.getInstance().getClientAuthInfo()
				.getUserId();
		res.setRequestKey(Global.RequestKey.KEY_USERVOIP_NET);
		res.setUrl(Global.getRequestUrl(Global.RequestUri.URI_GET_VOIP_USER_INFO, null));
		String requestJson = ProtocolUtil.buildJSONPacketBody(
				new String[] { "useragent", "network", "reqtime" ,"wechat_id","godin_id"},
				new Object[] { Global.getUser_Agent(), ProtocolUtil.getNetworkName() + "", DateUtil.getDefaultFormat() ,myWeixinId,godinIdByVChatId}, 
						
						new String[]{"wechat_id"}, new Object[] {wixinids});
		res.setRequestBody(requestJson);
		
		Log.e("TAG","contactService doRequestUserVoIP inforequestJson+"+requestJson);
		Log.e("TAG","contactService doRequestUserVoIP inforequestJson");
		addTask(res ,Resource.RESOURCE_UPLOAD_MOBILE);
	}

在回到ContactUIController代码中

else if (RequestKey.KEY_USERVOIP_NET.equals(document.getRequestKey())) {

			Log.e("TAG", "ContactUIController RequestKey.KEY_USERVOIP_NET");
			
			if (document instanceof ContactState) {
				ContactState doc = (ContactState) document;

				final ArrayList<Entry> entryList = doc.getEntryList();

				if (entryList != null && entryList.size() > 0) {
					Log.e("TAG", ""+entryList.size());
					
					//创建集合存放微信id
					ArrayList<String> list = new ArrayList<String>();
					for (Entry obj : entryList) {
						list.add(obj.getVchatId());
					}
					mWeixinIds = list.toArray(new String[] {});


					VChatSipInfoStorage.getInstance().insertVChatContacts(entryList);
				}
				
			}
			Message msg = mActivityHandler.obtainMessage();
			msg.what = Integer.parseInt(document.getRequestKey());
			Bundle bundle = new Bundle();
			bundle.putStringArray(VoipUtils.WEIXIN,
					mWeixinIds == null ? new String[] {} : mWeixinIds);
			msg.setData(bundle);
			
			mActivityHandler.sendMessage(msg);

		} 

回到ContactController代码中

case RequestCode.KEY_USERVOIP_NET:
				// doRefreshOnlinePullDown();
				Log.e("TAG", "ContactController  RequestCode.KEY_USERVOIP_NET");
				/**
				 * 获取对方的tek
				 */
				Bundle data = msg.getData();
				String[] weixinIds = (String[]) data.get(VoipUtils.WEIXIN);

				String godinIdString = VChatSipInfoStorage.getInstance()
						.queryLocalGodinId();
				if (TextUtils.isEmpty(godinIdString)) {
					Log.e("TAG", "godinIdString==null");
					return;
				}
				for (int i = 0; i < weixinIds.length; i++) {
					String queryGodinIdByWeixinId = VChatSipInfoStorage
							.getInstance().queryGodinIdByVChatId(weixinIds[i]);
					if (TextUtils.isEmpty(godinIdString)
							|| godinIdString.equals(queryGodinIdByWeixinId)) {
						continue;
					}
					// 检查、下载tek
					KeyCheckUtil
							.checkTEK(context, queryGodinIdByWeixinId, null);
					Log.e("TAG", "checkTEK method");
				}

				break;


你可能感兴趣的:(容联云通讯---进微信会话界面,主动同步服务器至本地数据库)