Java Question Tip about BA.raiseEventFromDifferentThread

Erel

Administrator
Staff member
Licensed User
BA.raiseEventFromDifferentThread is usually used in a multithreaded library to raise an event. It works by sending a message to the main thread messages queue.

However it can also be useful in the case of a single threaded library (any library that doesn't start a new thread or uses the internal thread pool).

Regular events are only handled when the activity is active (or service).

In most cases code running in the main thread is not expected to raise events while the activity is paused. However sometimes there are special events that are expected to be raised when the activity is paused.
For example, the code of AdMob wrapper:
B4X:
   public void Initialize2(final BA ba, String EventName, String PublisherId, Object Size) {
      setObject(new AdView(ba.activity, (AdSize)Size, PublisherId));
      super.Initialize(ba, EventName);
      final String eventName = EventName.toLowerCase(BA.cul);
      getObject().setAdListener(new AdListener() {

         @Override
         public void onFailedToReceiveAd(Ad ad, AdRequest.ErrorCode e){
            ba.raiseEvent(getObject(), eventName + "_failedtoreceivead", e.toString());
         }
         @Override
         public void onReceiveAd(Ad ad) {
            ba.raiseEvent(getObject(), eventName + "_receivead");
         }
         @Override
         public void onDismissScreen(Ad arg0) {
            ba.raiseEventFromDifferentThread(getObject(), null, 0, eventName + "_adscreendismissed", false, null);
         }
         @Override
         public void onLeaveApplication(Ad arg0) {
            //
         }
         @Override
         public void onPresentScreen(Ad arg0) {
            ba.raiseEventFromDifferentThread(getObject(), null, 0, eventName + "_presentscreen", false, null);

         }
      });
   }

As you can see AdScreenDismissed event is raised with raiseEventFromDifferentThread. This event is raised just before the ad screen is dismissed. Our activity will be paused at that time. So by using raiseEventFromDifferentThread the event will wait in the message queue and be handled just before Activity_Resume. You can also see a message in the logs saying: Running waiting messages (x) where x is the number of waiting events.
 

walterf25

Expert
Licensed User
ba.RaiseEvent question

Hi Erel, I'm working on a ad library wrapper, so far everything works great, i'm now trying to add the events to catch when the ads load, close, or fail to load.
I have that part working on the adview part, but i would also like to implement this when an interstitial Ad loads or a video ad.

when I try to add the callback method for the interstitial ads like this
B4X:
getObject().setOnInterstitialAdDownload(new UserAdDownloadAd(){

i know this gives me an error because i'm only extending the ViewWrapper<Adview> class, and not FullscreenView, therefore i can not use the
B4X:
ba.raiseEvent(getObject(), eventName + "_adloaded"
code to raise this event, do you have any suggestions on how to accomplish this Erel, your help as usual will be greatly appreciated.

Thanks,
Walter
 
Top