B4A Library eventbus now departing gate 2.

here's an interesting thing called an event library (for android). a forum member was having
a problem raising an event in an activity that was in the background (or even killed). there are
some workarounds (service, intent, startactivityforresult, sharedpreferences, shared resource,
etc), which - for whatever reasons - he couldn't use in the project.

here is another option he probably couldn't use: the eventbus. there are a few implementations
out there, but i latched onto greenrobot's version and, after experimenting a bit in inline java, i
cobbled a simple library together. the additional jars required are very small. after downloading
and unarchiving the attached zip archive, copy any jars, aars or xml files to your additional libraries
folder. the remaining bus2 folder holds an example project which you can build.

to get started checking out the eventbus library (for android), navegate here:
https://greenrobot.github.io/EventBus/. this is a typical graphic representation of what the
event bus does:
ebus.png


in the attached example, the main activity posts an event on the bus and then runs a second
activity. when that activity launches, it retrieves the event automagically. if you read the
abovementioned link, you'll see that the eventbus handles communications between
activities, services, fragments, etc. and to read all the information out there, you'd think it was
not possible to do anything serious without an eventbus. whatever.

the so-called event can be any type of object. for purposes of simplicity, i chose a string
"event". anyone on the eventbus will see the event automatically as it is posted (except for
activities that are not in the foreground). i have chosen to employ "sticky" events so that dormant
activities will receive them when they are launched or moved to the foreground. push notifications
and named pipes/ipc come to mind.
 

Attachments

  • bus2.zip
    52.5 KB · Views: 196
Hi drgottjr, Is possible to send events across applications? I need to send an receive events with an app developed with android studio which send events with LiveEventBus:
B4X:
 LiveEventBus
                        .get("ahd_notice")
                        .postAcrossApp(new NoticeEvent(simp));
Is possible to do it with B4A
 

drgottjr

Expert
Licensed User
Longtime User
it's been a while, but i think "my" eventbus is based on greenrobot's implementation (one of many out there). even though they might all handle the ipc in a similar fashion under the hood, the methods used by each for communication at the user level are different. your ".postAcrossApp()" is not the same as my "send()", so they wouldn't work together without some kind of middleman (or wrapper). if you can't change the app developed with android studio, then any app created with b4a that uses an eventbus would have to use LiveEventBus. if LiveEventBus is available as a library (.jar/.aar), it should work with b4a with javaobject. if you have to build it from source, things could get a little trickier. i saw the github page, but my chinese is a little rusty, so i missed the good bits.
 
it's been a while, but i think "my" eventbus is based on greenrobot's implementation (one of many out there). even though they might all handle the ipc in a similar fashion under the hood, the methods used by each for communication at the user level are different. your ".postAcrossApp()" is not the same as my "send()", so they wouldn't work together without some kind of middleman (or wrapper). if you can't change the app developed with android studio, then any app created with b4a that uses an eventbus would have to use LiveEventBus. if LiveEventBus is available as a library (.jar/.aar), it should work with b4a with javaobject. if you have to build it from source, things could get a little trickier. i saw the github page, but my chinese is a little rusty, so i missed the good bits.
Thanks for your answer, I will try to make the other company that made the android studio app to use other method to send messages, for example intents, because I can't find LiveEventBus library and I dont't know how to do it for myself.
 
Do you think it would be possible use this java code in B4A?:
B4X:
LiveEventBus
    .get("some_key", String.class)
    .observe(this, new Observer<String>() {
        @Override
        public void onChanged(@Nullable String s) {
        }
    });
 
Top