[英]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.