Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中创建接口并调用android方法?_Javascript_Android_Webview_Interface - Fatal编程技术网

如何在javascript中创建接口并调用android方法?

如何在javascript中创建接口并调用android方法?,javascript,android,webview,interface,Javascript,Android,Webview,Interface,我正在构建一个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>&l

我正在构建一个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>
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中不受欢迎,或者有其他解决方法?

这是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):


公共无效日志(字符串msg)
之前添加
@JavascriptInterface

谢谢你能告诉我如何在android中添加jQuery吗
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");

}
 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {

        webView.evaluateJavascript("someJQFunction();", null);

    } 
else {

        webView.loadUrl("javascript: someJQFunction();");

    }