简体   繁体   English

JavaScriptInterface仅适用于本地HTML文件

[英]JavaScriptInterface only working with local HTML file

I have created an android application which uses a webview to access an HTML file. 我创建了一个使用Web视图访问HTML文件的android应用程序。 This was done to map 2 javascript functions to 2 native android functions using a JavaScriptInterface. 这样做是使用JavaScriptInterface将2个javascript函数映射到2个本机android函数。 These 2 functions bring up and dismiss the android soft keyboard. 这2个功能可启动和关闭android软键盘。

When I originally tested the file, I was running it locally and it worked. 当我最初测试文件时,我在本地运行它并且可以正常工作。 Since then, I have placed the file on a server and changed the webview to access the HTML file through the server. 从那时起,我将文件放置在服务器上,并更改了Webview以通过服务器访问HTML文件。 Since placing it on the server, the javascriptinterface functions are no longer being called. 由于将其放置在服务器上,因此不再调用javascriptinterface函数。

Is there a flag that needs to be set or a permission I am missing? 是否需要设置标志或缺少我的权限? I have searched to no avail. 我搜索无济于事。

here is the HTML code 这是HTML代码

<head id="Head1" runat="server">
<title>Login Page</title>
<script language="javascript" type="text/javascript">
    function capturePassword(event)
    {
        event = (event) ? event : window.event
        var charCode = (event.which) ? event.which : event.keyCode
        if (charCode == 13) {
        document.getElementById("password").focus();
        document.getElementById("password").click();

        }
    }
    function myFunction()
    {
        jsi.showKeyboard(); //this is a function in the webview android app which does not get called when the file is placed on the server
    }
    function hideKeyboardMaybe()
    {
        event = (event) ? event : window.event
        var charCode = (event.which) ? event.which : event.keyCode
        if (charCode == 13) {
            jsi.hideKeyboard(); //same here
        }
    }
</script>

</head>
<body>
      User name: <input type="number" id="fname" autofocus      onkeyup="capturePassword(event)"><br>
      Password: <input type="number" id="password" onfocus="myFunction()"     onkeyup="hideKeyboardMaybe(event)"><br>
</body>
</html>

here is the webview code 这是webview代码

public class MainActivity extends Activity {

@SuppressLint("JavascriptInterface")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final WebView mWebView=(WebView)findViewById(R.id.webView1);

mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setSaveFormData(true);
mWebView.getSettings().setBuiltInZoomControls(true);
JavaScriptInterface jsi = new JavaScriptInterface(this, mWebView);
mWebView.addJavascriptInterface(jsi, "jsi"); 

//setContentView(mWebView);
mWebView.loadUrl("file://"+ Environment.getExternalStorageDirectory() +     "/sippopup.html");
mWebView.requestFocus(View.FOCUS_DOWN);

}


public class JavaScriptInterface {
Context mContext;
WebView v;

/** Instantiate the interface and set the context */
JavaScriptInterface(Context c, WebView v) {
mContext = c;
this.v=v;

}


@JavascriptInterface
public void showKeyboard()
{

InputMethodManager inputMethodManager=    (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.showSoftInput(v, 0);

}

@JavascriptInterface
public void hideKeyboard()
{

InputMethodManager inputMethodManager=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);

}
}



}

JavaScript is disabled in WebViews by default, you will need to enable it in code. 默认情况下,WebView中的JavaScript是禁用的,您需要在代码中启用它。

WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScriptInterface()在JellyBean中不起作用 - JavaScriptInterface() not working in JellyBean Android JavascriptInterface无法与minifyEnabled一起使用 - Android JavascriptInterface not working with minifyEnabled JavascriptInterface在Android 5上不起作用 - JavascriptInterface not working on Android 5 JavaScriptInterface()在android中不起作用 - JavaScriptInterface() is not working in android JavascriptInterface在版本&gt; 2.1上不起作用 - JavascriptInterface not working on versions > 2.1 HTML5 + 2 JS 脚本仅单独工作 - 相机和本地文件输入 - HTML5 + 2 JS Scripts only working separately - camera and local file input 嵌入 Twitch Player 正在使用小提琴,但不在本地 html 文件中 - Embedding Twitch Player is working in fiddle but not in local html-file HTML-SVG文件动画在主机上不起作用,但在本地上起作用? - HTML - SVG File animation not working on host, but works on local? JavaScript文件的本地路径在加载到WebBrowser控件本地HTML页面下无法正常工作 - Local path to JavaScript file are not working under loaded to WebBrowser control local HTML page 从不调用JavaScriptInterface类在WebView android上加载html - JavaScriptInterface class is never called to load html on webview android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM