onPause()
里面调用 pauseChannel()
onResume()
里面调用 resumeChannel()
iOS
只要App在 Capabilities->Background Modes设置成OFF就可以,无需特殊处理。在退后台时通话会自动暂停,回到前台时通话会恢复。
Android
需要处理电话事件,否则电话和我们的语音会同时进行,处理步骤如下:
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);
}
<uses-permission android:name="android.permission.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(); }
}
iOS
只要App在 Capabilities->Background Modes设置成ON就可以,无需特殊处理。在退后台时通话不会中断。并且有电话来时,通话会自动暂停。当电话挂断时,通话会自动恢复。