Cordova Sending Data From Android Java to Javascript

假如想象 提交于 2021-01-29 15:09:51

问题


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.

来源:https://stackoverflow.com/questions/65799899/cordova-sending-data-from-android-java-to-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!