Two Peas in a Pod: Android and EGL

Now Go the Other Way

We’ve just looked at two examples of direct interaction between EGL and Android where EGL initiates the interaction. You’ll likely encounter scenarios where something happens in Android that you want to let EGL know about – where Android needs to initiate the interaction.

In the example app, I illustrate this with a timer in Android that increments a value. The Android app initiates interaction with the EGL app to pass it this value every time the timer fires.

On the app display, it’s just a simple counter as shown in Figure 7.

To make this type of interaction work, start by defining a function in your EGL handler:

function timerCountInterface( timerCountFromAndroid string in )
	timerCountField.text = timerCountFromAndroid;

Nothing complicated there.

On the Android side, we must be a bit tricky. Every time the timer fires, after we increment our “timerTick” variable, we directly call our EGL function using JavaScript injection:

webView.loadUrl("javascript:egl.rootHandler.timerCountInterface('" + timerTick + "');");

If we dissect this code, we see that we're acting like we're going to load a new page into our WebView container ("loadURL") but, instead of passing an HTTP URL, we're passing in some JavaScript to be executed in the WebView container. Our already-running EGL app is exposed to us by the "egl.rootHandler" variable that's a part of the generated code for our EGL app.

A good question may come to mind at this point: What if IBM changes the JavaScript generation implementation such that "egl.rootHandler" goes away or doesn't behave the same in the future? I don't have an answer. I hope the implementation doesn't change because I'm counting on it to keep working. Or, if it does change, I hope that IBM recognizes the need for this functionality and exposes it in another easily accessible way.

One Last Thing

With what you've already seen, the sky is the limit for Android and EGL interaction. You can load an EGL program and pass it parameters. You can directly call Android methods from EGL--passing parameters to Android and getting data back. (That right there lets you tap into everything Android has to offer.) You can also code Android to directly invoke EGL functions based on events that get triggered on the native Android side.

This last example is included in the interest of completeness. I don't know if you'll use it or not.

Android has the capability to monitor a browser's log and respond to log entries as they arrive. In the example application, we can write a browser log entry and, when Android picks it up, the log entry is displayed as a Toast. It looks like Figure 8 in action.

One of our external type functions on the EGL side is a simple write to the browser log. Here's the EGL code that responds to a button press on the "Write Log Entry" button:

function onClick_writeLogEntry( event Event in )

On the Android side, we establish a listener for browser console messages. Anything that comes in gets displayed as a Toast.

webView.setWebChromeClient(new WebChromeClient() {
   public boolean onConsoleMessage(android.webkit.ConsoleMessage cm) {
"EGL wrote this browser log entry: " + cm.message(),
return true;

If you wanted, you could do something in the Android app when a particular message is received. You'd just need to look at the "cm.message()" contents and react accordingly. I don't personally think this approach is as clean as the direct JavaScript interface approach, but you may have a use for it.

I'll make the ADT and RBD projects available for download from my website at Go to the “Software” tab and look for “Android and EGL.” Import these projects into the appropriate tools to access the full source code and configuration details. No warranty is expressed or implied for any of this code. Use it at your own risk but feel free to use it any way you want, without attribution.

Dan Darnell is an IBM consultant and author.

Like what you just read? To receive technical tips and articles directly in your inbox twice per month, sign up for the EXTRA e-newsletter here.

comments powered by Disqus



2019 Solutions Edition

A Comprehensive Online Buyer's Guide to Solutions, Services and Education.

Are You Multilingual?

Rational enables development in multiplatform environments

IBM Systems Magazine Subscribe Box Read Now Link Subscribe Now Link iPad App Google Play Store
IBMi News Sign Up Today! Past News Letters