Crosswalk - 如何在XWalkExtension类中获取android应用程序上下文?

时间:2014-12-03 05:00:20

标签: java android crosswalk-runtime

所以我按照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消息,我可以传入上下文。

1 个答案:

答案 0 :(得分:1)

没有公开记录的界面可以从XWalkExtension获取活动。您可以修改构造函数以存储对Activity的私有引用,然后只是从实例化扩展的位置传入它。

public class ExtensionEcho extends XWalkExtension {
    private final Activity activity;

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

    ...
}