问题
Problem
I have been working on a Flutter Plugin for the Radar SDK, however, I am unable to programmatically set my BroadcastReceiver.
This works, but I cannot access my EventChannel
When I set my Receiver in the android Manifest of the plugin, the receiver is called properly. The manifest can be seen below:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.yourorg.appname">
<application
android:name="io.flutter.app.FlutterApplication">
<receiver
android:name=".MyRadarReceiver"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="io.radar.sdk.RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
What I Have Tried
However, when I attempt to set the receiver programmatically none of the events are received. I register the receiver in the onAttachedToEngine
which I believe is proper, however, none of the events are caught.
override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
this.context = flutterPluginBinding.getApplicationContext()
channel = MethodChannel(flutterPluginBinding.getFlutterEngine().getDartExecutor(), "flutter_radar_io")
channel.setMethodCallHandler(this)
receiver = MyRadarReceiver(this)
flutterPluginBinding.getApplicationContext().registerReceiver(receiver, IntentFilter("io.radar.sdk.RECEIVED"))
}
Why
The reason I need to set the receiver programmatically is that I need to pass the context to my receiver so that when an event is received I can add the new event to my EventChannel which can be subscribed to as a stream in my flutter application. There may be another way to accomplish this but this is as close as I've come.
If you need me to share more code such as the receiver class I would be more than happy to.
来源:https://stackoverflow.com/questions/64246589/flutter-plugin-how-to-set-a-programatic-receiver-with-intentfilter