gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:11:17 25 4
gpt4 key购买 nike

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

最佳答案

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

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com