在Android中,可以通过实现BroadcastReceiver来监听来电事件,代码示例如下:
public class IncomingCallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(TelephonyManager.ACTION_PHONE_STATE_CHANGED)) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
// 在此处处理来电通知
}
}
}
}
而在iOS中,可以通过实现CXCallObserverDelegate来监听来电事件,代码示例如下:
class CallObserver: NSObject, CXCallObserverDelegate {
private let callObserver = CXCallObserver()
func startObserving() {
callObserver.setDelegate(self, queue: DispatchQueue.main)
}
func callObserver(_ callObserver: CXCallObserver, callChanged call: CXCall) {
if call.hasEnded {
// 处理来电结束通知
} else if call.isOutgoing {
// 处理去电通知
} else if call.hasConnected {
// 处理来电接通通知
} else if call.hasStartedConnecting {
// 处理来电连接中通知
} else {
// 处理来电中断通知
}
}
}