Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

Convenience for anonymous BroadcastReceivers #484

Description

@dhleong

It would be excellent to be able to do something like this inside a fragment or activity:

@EReceiver({ACTION_ONE, ACTION_TWO})
BroadcastReceiver mReceiver = new BroadcastReceiver() {

  @Override
  public void onReceive(Context context, Intent intent) {
    if (ACTION_ONE.equals(intent.getAction()) {
      // ...
    } else if (ACTION_TWO.equals(intent.getAction()) {
      // ...
    }
  }
}

And have AndroidAnnotations fill in the registerReceiver() with IntentFilter in onResume() (or onCreate()) and unregisterReceiver() in onPause() (or onDestroy()).

Or, even better, just annotate methods:

@EReceiverMethod(ACTION_ONE)
void onReceiveOne(Context context, Intent intent, BroadcastReceiver receiver) {}

@EReceiverMethod({ACTION_TWO, ACTION_THREE)
void onReceiveTwoOrThree(Context context, Intent intent, BroadcastReceiver receiver) {}

and have the receiver arg be optional, in case you're using sticky broadcasts, or something.

I'd love to work on this when I find time, just looking for input on the best way to approach it, IE: when to register/unregister, should that be an argument to the annotation, do we want to annotate methods, or just the object, etc.

Activity

  1. pyricau commented on Feb 27, 2013

    @pyricau
    Contributor

    AndroidAnnotations can only generate subclasses. Sorry but this doesn't look like anything AndroidAnnotations can do.

  2. dhleong commented on Feb 27, 2013

    @dhleong
    ContributorAuthor

    Why not? For the @EReceiverMethod approach, in an activity or fragment where some method is annotated as suggested above, AndroidAnnotations could generate the anonymous BroadcastReceiver in the subclass with a series of if statements to call the methods in the parent class. We could even attempt to extract extras from the intent based on the variable name and type in the method signature, if we want to get really fancy.

  3. pyricau commented on Feb 27, 2013

    @pyricau
    Contributor

    Well, the problem is here : BroadcastReceiver mReceiver = new BroadcastReceiver() . This is an anonymous class. One cannot generate a subclass of an anonymous class. And even if we could, there's no way to replace the new BroadcastReceiver() with a new BroadcastReceiver_(). Furthermore, we cannot access the variable name.

  4. dhleong commented on Feb 27, 2013

    @dhleong
    ContributorAuthor

    Do you need to generate a subclass of the Receiver? The first proposal
    would simply generate code in the onPause/onResume to register/unregister
    the created receiver. The second proposal would generate the anonymous
    receiver itself, transparent to the parent class.

    On Wed, Feb 27, 2013 at 11:59 AM, Pierre-Yves Ricau <
    notifications@github.com> wrote:

    Well, the problem is here : BroadcastReceiver mReceiver = new
    BroadcastReceiver() . This is an anonymous class. One cannot generate a
    subclass of an anonymous class. And even if we could, there's no way to
    replace the new BroadcastReceiver() with a new BroadcastReceiver_().
    Furthermore, we cannot access the variable name.

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/484#issuecomment-14185229
    .

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions