人行横道在android上从java调用js函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24601087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 13:23:49  来源:igfitidea点击:

Crosswalk call js function from java on android

javajavascriptandroidcrosswalk-runtime

提问by gokhangokce

I m trying to use crosswalkruntime in my android app. I tried this on android 4+.

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

I got some js & html codes and it worked perfect for me. But it is not working like android webview. In the webview i can call javascript functions from java code. But i couldnt find any option in crosswalk. Any idea?

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

Thanks

谢谢

采纳答案by Shouqun

Crosswalk support the similar API interface (evaluateJavascript) as Android WebView for calling JavaScript function from Java:

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

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

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

You can also use loadfor calling javascript function directly like: xwalkView.load("javascript:" + jsCode, null).

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

did you meet any issue with the Crosswalk API?

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

回答by Rubber Duck

In Android Studio inside app/module/lib level build.gradle add this to dependencies:

在 app/module/lib 级别 build.gradle 中的 Android Studio 中,将其添加到依赖项中:

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

Sync Project

同步项目

create xml layout resource

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

In activity onCreate or called by it

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

create class JS_Bind:

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

in web Java script call the function thus:

在 web Java 脚本中调用该函数:

Android.showToast(toast);

make sure you import the correct xwalk @JavaScripInterface decorator instead of the standard web view decorator.

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

回答by bboydflo

The response above is almost complete. as @Someone Somewhere commented it is needed to add these to lines in order to call native functionality from Crosswalk Webview.

上面的回复基本完成了。正如@Someone Somewhere 评论的那样,需要将这些添加到行中,以便从 Crosswalk Webview 调用本机功能。

  1. add this import statement import org.xwalk.core.XWalkView;
  2. before any exposed method to javascript put this @org.xwalk.core.JavascriptInterfaceinstead of the default annotation @JavascriptInterface
  1. 添加此导入语句 import org.xwalk.core.XWalkView;
  2. 在任何向 javascript 公开的方法之前,将它@org.xwalk.core.JavascriptInterface而不是默认注释@JavascriptInterface

回答by mehdi

For calling js method and return back result to java . you have to mix Javascript interface and evaluateJavascript.

用于调用 js 方法并将结果返回给 java 。你必须混合Javascript接口和评估Javascript。

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

    public String GetFullname(String FirstName , String LastName){
        xWalkWebView.evaluateJavascript(String.format(
        "Android.ReturnGetFullName(getFullName('%s','%s'))" , FirstName , LastName));
    }

    @JavascriptInterface
    public void ReturnGetFullName(String Fullname) {
        Log.d(TAG, "showToast(String toast)");
        Toast.makeText(context, Fullname, Toast.LENGTH_SHORT).show();
    }

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

and in javascript :

在 javascript 中:

<script>
    function getFullName(FirstName , LastName){
        return FirstName +  " " + LastName;
    }
</script>