Crosswalk在java上从java调用js函数

gok*_*kce 4 javascript java android crosswalk-runtime

我试图在我的Android应用程序中使用crosswalk运行时.我试过android 4+.

我有一些js和html代码,它对我来说很完美.但它不像android webview那样工作.在webview中,我可以从java代码调用javascript函数.但我在人行横道上找不到任何选择.任何的想法?

谢谢

Rub*_*uck 10

在Android Studio里面的app/module/lib level build.gradle将它添加到依赖项中:

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:21.0.3'
compile 'org.xwalk:xwalk_core_library:12.41.296.5'}
Run Code Online (Sandbox Code Playgroud)

同步项目

创建xml布局资源

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<org.xwalk.core.XWalkView
    android:id="@+id/xwalkWebView"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000"
    />

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

在活动onCreate或由它调用

//        Don't know how this helps if the preferences are connected?
XWalkPreferences.setValue("enable-javascript", true);
XWalkPreferences.setValue(XWalkPreferences.REMOTE_DEBUGGING, true);

xWalkView=(XWalkView)findViewById(R.id.xwalkWebView);
xWalkView.addJavascriptInterface(new JS_Bind(this, xWalkView),"Android");

xWalkView.clearCache(true);
xWalkView.load(COM_URL, null);
Run Code Online (Sandbox Code Playgroud)

创建类JS_Bind:

public class JS_Bind {
    private static final String TAG = "JS_Bind";
    private Context context;
    private XWalkView xWalkWebView;

    public JS_Bind(Context c, XWalkView xWalkWebView) {
        context = c;
        this.xWalkWebView = xWalkWebView;
    }

    @JavascriptInterface
    public void showToast(String toast) {
        Log.d(TAG, "showToast(String toast)");
        Toast.makeText(context, toast, Toast.LENGTH_SHORT).show();
    }
}
Run Code Online (Sandbox Code Playgroud)

在Web Java脚本中调用函数:

Android.showToast(toast);
Run Code Online (Sandbox Code Playgroud)

确保导入正确的xwalk @JavaScripInterface装饰器而不是标准的Web视图装饰器.

  • 所以我发现默认情况下启用了javascript.然而,一个关键的魔法是......改变"import android.webkit.JavascriptInterface;" "导入org.xwalk.core.JavascriptInterface;" (5认同)

Sho*_*qun 7

Crosswalk支持与evaluateJavascriptAndroid WebView 类似的API接口(),用于从Java调用JavaScript函数:

https://crosswalk-project.org/apis/embeddingapidocs/reference/org/xwalk/core/XWalkView.html#evaluateJavascript(java.lang.String ,)

您还可以load直接使用调用javascript函数,如: xwalkView.load("javascript:" + jsCode, null).

你是否遇到过Crosswalk API的任何问题?