Пример #1
0
  /**
   * this function will initialize the HuanXin SDK
   *
   * @return boolean true if caller can continue to call HuanXin related APIs after calling onInit,
   *     otherwise false.
   *     <p>环信初始化SDK帮助函数 返回true如果正确初始化,否则false,如果返回为false,请在后续的调用中不要调用任何和环信相关的代码
   *     <p>for example: 例子:
   *     <p>public class DemoHXSDKHelper extends HXSDKHelper
   *     <p>HXHelper = new DemoHXSDKHelper(); if(HXHelper.onInit(context)){ // do HuanXin related
   *     work }
   */
  public synchronized boolean onInit(Context context) {
    if (sdkInited) {
      return true;
    }

    appContext = context;

    // create HX SDK model
    hxModel = createModel();

    // create a defalut HX SDK model in case subclass did not provide the model
    if (hxModel == null) {
      hxModel = new DefaultHXSDKModel(appContext);
    }

    int pid = android.os.Process.myPid();
    String processAppName = getAppName(pid);

    Log.d(TAG, "process app name : " + processAppName);

    // 如果app启用了远程的service,此application:onCreate会被调用2次
    // 为了防止环信SDK被初始化2次,加此判断会保证SDK被初始化1次
    // 默认的app会在以包名为默认的process name下运行,如果查到的process name不是app的process name就立即返回
    if (processAppName == null || !processAppName.equalsIgnoreCase(hxModel.getAppProcessName())) {
      Log.e(TAG, "enter the service process!");

      // 则此application::onCreate 是被service 调用的,直接返回
      return false;
    }

    // 初始化环信SDK,一定要先调用init()
    EMChat.getInstance().init(context);

    // 设置sandbox测试环境
    if (hxModel.isSandboxMode()) {
      EMChat.getInstance().setEnv(EMEnvMode.EMSandboxMode);
    }

    if (hxModel.isDebugMode()) {
      // set debug mode in development process
      EMChat.getInstance().setDebugMode(true);
    }

    Log.d(TAG, "initialize EMChat SDK");

    initHXOptions();
    initListener();

    syncGroupsListeners = new ArrayList<HXSyncListener>();
    syncContactsListeners = new ArrayList<HXSyncListener>();
    syncBlackListListeners = new ArrayList<HXSyncListener>();

    isGroupsSyncedWithServer = hxModel.isGroupsSynced();
    isContactsSyncedWithServer = hxModel.isContactSynced();
    isBlackListSyncedWithServer = hxModel.isBacklistSynced();

    sdkInited = true;
    return true;
  }
Пример #2
0
  @Override
  protected void initHXOptions() {
    super.initHXOptions();

    // you can also get EMChatOptions to set related SDK options
    EMChatOptions options = EMChatManager.getInstance().getChatOptions();
    options.allowChatroomOwnerLeave(getModel().isChatroomOwnerLeaveAllowed());
  }