0

So I'm following the instructions for creating a Crosswalk extension on https://crosswalk-project.org/documentation/embedding_crosswalk/extensions.html. My question relates to extending the XWalkExtension class.

package org.crosswalkproject.sample;

import org.xwalk.core.XWalkExtension;

public class ExtensionEcho extends XWalkExtension {
    private static String name = "echo";

    private static String jsapi = "var echoListener = null;" +
         "extension.setMessageListener(function(msg) {" +
         "  if (echoListener instanceof Function) {" +
         "    echoListener(msg);" + "  };" + "});" +
         "exports.echo = function (msg, callback) {" +
         "  echoListener = callback;" + "  extension.postMessage(msg);" +
         "};" + "exports.echoSync = function (msg) {" +
         "  return extension.internal.sendSyncMessage(msg);" + "};";

    public ExtensionEcho() {
        super(name, jsapi);
    }

    @Override
    public void onMessage(int instanceID, String message) {
        postMessage(instanceID, "From java: " + message);
    }

    @Override
    public String onSyncMessage(int instanceID, String message) {
        return "From java sync: " + message;
    }

}

When extending the XWalkExtension class, how can you get the android application context? Hence, if I wanted to create a Toast message, I can pass in the context.

1 Answers1

1

There isn't a publicly documented interface to get the Activity from XWalkExtension. You can modify the constructor to store a private reference to an Activity, then simply pass it in from where you instantiate the extension.

public class ExtensionEcho extends XWalkExtension {
    private final Activity activity;

    public ExtensionEcho(Activity activity) {
        this.activity = activity;
    }

    ...
}
Mike Kwan
  • 24,123
  • 12
  • 63
  • 96