Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Making the calls directly from java to javascript with Phonegap

I'm writing a native plugin to raise an event in JavaScript when the keyboard is showing. I do this:

appView.sendJavascript("cordova.fireWindowEvent('show_keyboard')")

In my JavaScript I then do something like:

window.addEventListener('show_keyboard', handler);

However, this has been flagged as a big no no in PhoneGap by a PhoneGap expert on the team. What is wrong with this approach?

like image 490
Mark Avatar asked Jul 02 '14 10:07

Mark


1 Answers

Looking for an answer drawing from credible and/or official sources.

Well I too am no PhoneGap expert, but Apache Cordova, the engine that powers PhoneGap has its source on GitHub.

What does the source say?

Well, as an example, let's look at how Cordova sends its volumedownbutton event. I present lines 613–621 of CordovaWebView.java:

if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
    this.loadUrl("javascript:cordova.fireDocumentEvent('volumedownbutton');");
    return true;
}
// If volumeup key
else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
    this.loadUrl("javascript:cordova.fireDocumentEvent('volumeupbutton');");
    return true;
}

It would seem that Cordova employs a similar approach to sending events to JavaScript.

What's wrong with what you have?

I am not sure what the exact issue was that your coworker raised, but it does seem that sendJavascript is deprecated. So, there's that. But if your appView is a CordovaWebView, the you can just call loadUrl in the same way the that Cordova itself does it (as shown above).

like image 85
Whymarrh Avatar answered Oct 21 '22 16:10

Whymarrh