New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

broadcastReceiver function for String argument inconsistent with the function taking IntentFilter #139

Closed
MaciejG opened this Issue Sep 30, 2015 · 2 comments

Comments

Projects
None yet
3 participants
@MaciejG

MaciejG commented Sep 30, 2015

in the readme there is an example of using broadcastReceiver function:

broadcastReceiver(ConnectivityManager.CONNECTIVITY_ACTION) { (context, intent) =>
  doSomething()
}

the problem is that it is impossible to use this version of the function this way because the passed onReceiveBody function is parameterless and than it is invoked in onReceive without any parameters:

  def broadcastReceiver(filterString: String)(onReceiveBody: => Any)(implicit ctx: Context, reg: Registerable) {
    val receiver = new BroadcastReceiver {
      def onReceive(context: Context, intent: Intent) {
        onReceiveBody
      }
    }
    val filter = new IntentFilter()
    filter.addAction(filterString)
    reg.onRegister(ctx.registerReceiver(receiver, filter))
    reg.onUnregister(ctx.unregisterReceiver(receiver))
  }

so the only version that takes context and intent parameters is the one which takes filter: IntentFilter as parameter:

  def broadcastReceiver(filter: IntentFilter)(onReceiveBody: (Context, Intent) => Any)(implicit ctx: Context, reg: Registerable) {
    val receiver = new BroadcastReceiver {
      def onReceive(context: Context, intent: Intent) {
        onReceiveBody(context, intent)
      }
    }
    reg.onRegister(ctx.registerReceiver(receiver, filter))
    reg.onUnregister(ctx.unregisterReceiver(receiver))
  }

so this is e.g. possible:

broadcastReceiver(new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)) { (context, intent) =>
  doSomething()
}

but the version with String as param may be used only in this way:

broadcastReceiver(ConnectivityManager.CONNECTIVITY_ACTION) {
  doSomething()
}
@liskin

This comment has been minimized.

Show comment
Hide comment
@liskin

liskin Feb 10, 2016

I just lost 3 hours fighting this crap. Even the very example from the README doesn't work! broadcastReceiver(ConnectivityManager.CONNECTIVITY_ACTION) { (context, intent) => ... } calls the String/Any version of the function, since ((Context, Intent) => Unit) <: Any, but just evaluating the function does nothing at all. :-(

liskin commented Feb 10, 2016

I just lost 3 hours fighting this crap. Even the very example from the README doesn't work! broadcastReceiver(ConnectivityManager.CONNECTIVITY_ACTION) { (context, intent) => ... } calls the String/Any version of the function, since ((Context, Intent) => Unit) <: Any, but just evaluating the function does nothing at all. :-(

@pocorall pocorall closed this in 4a18a5d Feb 12, 2016

@pocorall

This comment has been minimized.

Show comment
Hide comment
@pocorall

pocorall Feb 12, 2016

Owner

Sorry for the inconsistence, I've just updated the document.

Owner

pocorall commented Feb 12, 2016

Sorry for the inconsistence, I've just updated the document.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment