因此,我遵循在https://crosswalk-project.org/documentation/embedding_crosswalk/extensions.html上创建Crosswalk扩展的说明。我的问题与扩展XWalkExtension类有关。

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;
    }

}


扩展XWalkExtension类时,如何获取android应用程序上下文?因此,如果我想创建Toast消息,则可以在上下文中传递。

最佳答案

没有公开记录的接口可从XWalkExtension获取活动。您可以修改构造函数以存储对Activity的私有引用,然后只需从实例化扩展的地方将其传递即可。

public class ExtensionEcho extends XWalkExtension {
    private final Activity activity;

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

    ...
}

关于java - Crosswalk-如何在XWalkExtension类中获取android应用程序上下文?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27263972/

10-10 06:18