Flutter Plugin: How to set a programatic Receiver with IntentFilter

我只是一个虾纸丫 提交于 2021-01-29 07:24:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!