【问题标题】:How to make interface in javascript and call android method?如何在javascript中制作界面并调用android方法?
【发布时间】:2018-01-02 18:25:18
【问题描述】:

我正在构建一个 android 应用程序,其中在 webView 上加载了一个失败的 HTTP 文件,该文件位于 assets 文件夹中。下面是失败HTTP文件的代码。

<html>
<head>
</head>
<body>
<a href="javascript:void(0);" onclick="init();">Click on this link for a log 
message</a>
<br><br><br><br><br>

 <script>
function init(){
    android.log('Data from JS');
    android.log(android.data);
}

function showMessage(str){
    android.log(str);
}
</script>

</body>
</html>

创建代码

 //add interface
    wv.addJavascriptInterface(jsInterface, "android");//android is the 
    keyword that will be exposed in js

OnClick方法和接口方法

 public void click(View view){
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        wv.evaluateJavascript("javascript:showMessage('Hello World!')", null);
    }
}

//javascript interface
private class JsInterface {
    @JavascriptInterface
    //function that will be called from assets/test.js
    //js example: android.log('my message');
    public String data() {
        return "DATA FROM ANDROID";
    }

    public void log(String msg) {
        Log.d("MSG FROM JAVASCRIPT", msg);
    }
}

以上是我如何在 JavaScript 中使用接口的完整代码。很久以前,我曾经使用过在旧项目中使用的相同方法,但现在无法正常工作了,任何人都可以回答我是这种方法在 android 中已被弃用,还是有其他解决方法?

【问题讨论】:

    标签: javascript android webview interface


    【解决方案1】:

    这是js接口类:

    public class JS_INTERFACE {
    
        Context mContext;
    
        /** Instantiate the interface and set the context */
        JS_INTERFACE(Context c) {
            mContext = c;
        }
    
        /** Show a toast from the web page */
        @JavascriptInterface
        public void native_showToast(String toast) {
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
        }
    
        @JavascriptInterface
        public void native_ShowNotifyTest1(String toast , String BigOrSmall ) {
    
            Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    
        }
    
    }
    
    
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    
        WebSettings settings = webView1.getSettings();
    
        // Enable Javascript
        settings.setJavaScriptEnabled(true);
    
        webView1.addJavascriptInterface(new JS_INTERFACE(getContext()), "android");
    
    }
    

    我使用 onCreateView 进行初始化。

    更新答案:

    在你的应用中使用 JQ(简单使用 evaluateJavascript):

     if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
    
            webView.evaluateJavascript("someJQFunction();", null);
    
        } 
    else {
    
            webView.loadUrl("javascript: someJQFunction();");
    
        }
    

    【讨论】:

    • 谢谢你能告诉我如何在android中添加jQuery
    【解决方案2】:

    public void log(String msg) 之前添加@JavascriptInterface

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 2013-07-21
      • 1970-01-01
      • 2015-04-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多