Making calls directly from java to javascript using Phonegap

I am writing my own plugin to raise an event in JavaScript when the keyboard is displayed. I'm doing it:

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

In my JavaScript, I then do something like:

 window.addEventListener('show_keyboard', handler); 

However, this was noted as a great absence by PhoneGap expert on PhoneGap in the team. What is wrong with this approach?

+6
source share
2 answers

Look for a response drawing from reliable and / or official sources.

Well, I'm not an expert on PhoneGap either, but Apache Cordova , the engine that supports PhoneGap , is its source on GitHub .

What does the source say?

Well, as an example, let's see how Cordoba dispatches a volumedownbutton event. I present lines 613 and 621 from 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 Cordoba uses a similar approach to send events to JavaScript.

What happened to what you have?

I don’t know exactly what the problem was that your colleague raised, but it looks like sendJavascript deprecated . So here it is. But if your appView is a CordovaWebView , you can simply call loadUrl in the same way that Cordoba herself does (as shown above).

+2
source

I'm not an expert on telephone calls, but why don't you just call the method directly and not use listeners?

eg

 function keyboardShown() { alert("test"); //or other code } 

and

 appView.sendJavascript("keyboardShown();") 

In this way, you delete the service data that the listener has.

If this does not yet satisfy your telephone expert, ask him what he will do to improve this code.

0
source

Source: https://habr.com/ru/post/971649/


All Articles