gpt4 book ai didi

java - Crosswalk 在 android 上从 java 调用 js 函数

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:35:28 25 4
gpt4 key购买 nike

我正在尝试使用 crosswalk在我的 android 应用程序中运行。我在 android 4+ 上试过这个。

我得到了一些 js 和 html 代码,它非常适合我。但它不像 android webview 那样工作。在 webview 中,我可以从 java 代码调用 javascript 函数。但是我在人行横道上找不到任何选择。任何的想法?

谢谢

最佳答案

在 Android Studio 中的 app/module/lib 级别的 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'}

同步项目

创建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>

在 onCreate Activity 中或被其调用

//        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);

创建类 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();
}
}

在 web Java 脚本中这样调用函数:

Android.showToast(toast);

确保导入正确的 xwalk @JavaScripInterface 装饰器而不是标准的 web View 装饰器。

关于java - Crosswalk 在 android 上从 java 调用 js 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24601087/

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