Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

Cordova Sending Data From Android Java to Javascript

I am working with Cordova and the inAppBrowser plugin for Android. I am trying to expand upon a previous question,

https://stackoverflow.com/questions/65534821/cordova-inappbrowser-hide-show-in-java-android/65627954?noredirect=1#comment116337002_65627954

where the inAppBrowser hardware back button was linked to the Javascript .hide function, to hide the browser but not exit it. This problem was solved, however created a new problem, inAppBrowser does not have a visibility status to say if it is either shown or hidden to the user.

I would like to send a message from Java to Cordova Javascript with the hide update.

Here is were we left off

I edited InAppBrowserDialog.java

I added

import org.apache.cordova.CordovaArgs;
import org.json.JSONArray;

And added inside the

public class InAppBrowserDialog extends Dialog {

the following

public void hideDialog() {
  CordovaArgs args = new CordovaArgs(new JSONArray());
    try {
      this.inAppBrowser.execute("hide", args, new FakeCallbackContext());
    } catch (JSONException e) {
      e.printStackTrace();
    }
 }

public void showDialog() {
  CordovaArgs args = new CordovaArgs(new JSONArray());
  try {
      this.inAppBrowser.execute("show", args, new FakeCallbackContext());
  } catch (JSONException e) {
      e.printStackTrace();
  }
} 

I then created a new java file in the same directory, called

FakeCallbackContext.java

And placed the following code inside

package org.apache.cordova.inappbrowser;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;

class FakeCallbackContext extends CallbackContext {

public FakeCallbackContext() {
  super(null, null);

}

@Override
public void sendPluginResult(PluginResult pluginResult) {
  // Do not send an actual result to the webview
  // Perhaps just log the result

  //<<<< This is where I need to send an update back to a Cordova listener 
  //<<<<   so I can let Cordova know that the inAppBrowser is now hidden

}
}

I have tried to incorporate several different solutions, such as

webView.loadUrlNow("javascript:" + js);

But from what I have read you have to be inside the main Cordova class, and not a plugin.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神解答

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...