EventChannel不是每次都被调用



我对flutter+dart框架很陌生。我想了解EventChannel是如何工作的。我已经设置了EventChannel来捕获一个来电的号码。

android侧,我设置了一个BroadcastReceiver,如下所示。

public class CallEventHandler extends BroadcastReceiver implements EventChannel.StreamHandler {
private static final String TAG = "[SAMPLE]";
private static final int NUMBER_LEN = 10;
private EventChannel.EventSink eventSink = null;
private Activity activity = null;
public CallEventHandler(Activity activity) {
this.activity = activity;
}
@Override
public void onListen(Object arguments, EventChannel.EventSink events) {
Log.i(TAG, "[onListen] setting up events");
eventSink = events;
}
@Override
public void onCancel(Object arguments) {
Log.i(TAG, "[onCancel] cancel events");
eventSink = null;
activity = null;
}
@Override
public void onReceive(Context context, Intent intent) {
try {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
if(state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
if(incomingNumber != null) {
Log.i(TAG, "[CallEventHandler] Incoming number : " + incomingNumber);
if(incomingNumber.length() > NUMBER_LEN) {
incomingNumber = incomingNumber.substring(incomingNumber.length() - NUMBER_LEN, incomingNumber.length());
Log.i(TAG, "[CallEventHandler] Incoming number after : " + incomingNumber);
if(activity != null) {
String finalIncomingNumber = incomingNumber;
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if(eventSink != null) {
Log.i(TAG, "[CallEventHandler] HERESSSSS : " + finalIncomingNumber);
eventSink.success(finalIncomingNumber);
}
}
});
}
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}

}

onReceive方法中,我正在获取入局号码并将其发送给EventSink

在我的MainActivity中,我设置CallEventHandler如下:

private final String eventId = "SAMPLE_ID";
private CallEventHandler handler = new CallEventHandler(this);
@Override
public void onStart() {
super.onStart();
...
registerReceiver(handler, filter);
}
@Override
public void onStop() {
super.onStop();
unregisterReceiver(handler);
}
@Override
public void configureFlutterEngine(@NonNull  FlutterEngine flutterEngine) {
super.configureFlutterEngine(flutterEngine);
new EventChannel(flutterEngine.getDartExecutor().getBinaryMessenger(), eventId)
.setStreamHandler(handler);
}

Flutter端,代码如下:

class EventHandler {
static const String TAG = "[SAMPLE]";
final String _eventId = "SAMPLE_ID";
EventChannel? _evtChannel;
Stream<String>? _evtStream;
EventHandler() {
debugPrint(TAG + " Setting up EventHandler");
_evtChannel = EventChannel(_eventId);
_evtStream = _evtChannel?.receiveBroadcastStream().distinct().map((dynamic
event) => getString(event as String));
}
void startListening(void Function(String data)? onData) {
debugPrint(TAG + " starting listening");
_evtStream?.listen((data) {
debugPrint(TAG + " In listening");
onData!(data);
});
}
}

在我的UI代码中,我有一个StatefulWidget(MySamplePage),当收到调用时,我正在注册回调

void initState() {
widget.handler.startListening((incomingNumber) {
debugPrint(_tag + " data : $incomingNumber");
...
}); 
}

在我的有状态主页构建方法中,我初始化了initState中的处理程序,并在build方法中添加了route

class _HomeScreenState extends State<HomeScreen> {
@override
void initState() {
super.initState();
debugPrint(_tag + "initState");
_handler = EventHandler();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
routes: {
'/caller': (context) => MySamplePage(
handler: _handler
),
},
...
);
}
}

我面临的问题是,当小部件打开时,我正在接收第一个来电,正如预期的那样。但是如果我进行另一个调用,那么第二个调用不会被流捕获。如果我按下后退按钮并重新打开Widget,一切都如预期的那样工作,第一个传入呼叫将在控制台中打印出来。我知道Android代码正在从onReceive方法发送事件(每次打印' HERESSSSS'行),但颤动流没有获得值。我不确定我做错了什么。有人能帮忙吗?

我的日志是

I/flutter (11836): [SAMPLE][HomeScreen]initState
I/flutter (11836): [SAMPLE][EventHandler] Setting up EventHandler
V/AutofillManager(11836): requestHideFillUi(null): anchor = null
I/flutter (11836): [SAMPLE][EventHandler] starting listening
I/[SAMPLE] (11836): [onListen] setting up events
I/[SAMPLE] (11836): [CallEventHandler] Receiver start
I/[SAMPLE] (11836): [CallEventHandler] Receiver start
I/[SAMPLE] (11836): [CallEventHandler] Incoming number : +91XXXXXXXXXX
I/[SAMPLE] (11836): [CallEventHandler] Incoming number after : XXXXXXXXXX
I/[SAMPLE] (11836): [CallEventHandler] HERESSSSS : XXXXXXXXXX
I/flutter (11836): [SAMPLE][EventHandler] In listening
I/flutter (11836): [SAMPLE] data : XXXXXXXXXX

在随后的呼入中,最后一行不打印

谢谢

好的,我已经设法解决了它,但不知道这是否是正确的方法。问题是MySamplePageStatefulWidget,我在其State对象中调用setState。这可能是它无法再收听流的原因。我已经调用startListeningsetState方法,并相应地更改了代码(删除以前的订阅并重新收听流)

void startListening(void Function(String data)? onData) {
debugPrint(TAG + " starting listening");
if(_subscription != null) {
_subscription?.cancel();
_subscription = null;
}
_subscription ??= _evtStream?.listen((data) {
debugPrint(TAG + " In listening");
onData!(data);
});
}

这里_subscriptionStreamSubscription<String>?类型的变量。希望这个答案对大家有所帮助。我应该早点把完整的代码贴出来。

相关内容

  • 没有找到相关文章

最新更新