异常处理

App退到后台时,当前通话暂停

  1. Android
    • onPause() 里面调用 pauseChannel()
    • onResume() 里面调用 resumeChannel()
  2. iOS

    只要App在 Capabilities->Background Modes设置成OFF就可以,无需特殊处理。在退后台时通话会自动暂停,回到前台时通话会恢复。

App退到后台时,当前通话继续

  1. Android

    需要处理电话事件,否则电话和我们的语音会同时进行,处理步骤如下:

    • Step 1: 把下面代码加到你们的MainActivity里面,并在onCreate()里面调用initPhoneStateReceiver
        private static BroadcastReceiver mReceiver = null;
        public static void initPhoneStateReceiver(Context env)
        {
            mReceiver = new BroadcastReceiver()
            {
                @Override
                public void onReceive(Context context, Intent intent)
                {
                    try {
                        String action = intent.getAction();
                        if (action.equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
                            String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
                            if(stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)){
                                api.resumeChannel();
                            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)){
                            }
                            else if(stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)){
                                api.pauseChannel();
                        }
                    }catch (Throwable e) {e.printStackTrace();}
                }
            };
            IntentFilter intenFilter = new IntentFilter();  
        intenFilter.addAction(TelephonyManager.ACTION_PHONE_STATE_CHANGED);
            env.registerReceiver(mReceiver, mFilter);
        }
    • Step 2: 在AndroidManifest.xml里面,添加如下的权限申请:
      <uses-permission android:name="android.permission.READ_PHONE_STATE" />
    • Step 3: 当App的TARGET_LEVEL >= 23时,需要增加代码动态获取 READ_PHONE_STATE 权限。增加如下函数并调用它即可:
        public static void requestReadPhoneStatePermission(Context context)
        {
            try {
                if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) && (context != null)
                && (context instanceof Activity) && (context.getApplicationInfo().targetSdkVersion >= 23)) {
                    int permission = ContextCompat.checkSelfPermission((Activity)context, Manifest.permission.READ_PHONE_STATE);
                    if (permission != PackageManager.PERMISSION_GRANTED) {
                        ActivityCompat.requestPermissions((Activity)mContext,
                        new String[]{Manifest.permission.READ_PHONE_STATE},
                        1);
                    }
                }
            } catch (Throwable e) { e.printStackTrace(); }
        }
  2. iOS

    只要App在 Capabilities->Background Modes设置成ON就可以,无需特殊处理。在退后台时通话不会中断。并且有电话来时,通话会自动暂停。当电话挂断时,通话会自动恢复。