javascript - JavaScriptInterface only working with local HTML file -
i have created android application uses webview access html file. done map 2 javascript functions 2 native android functions using javascriptinterface. these 2 functions bring , dismiss android soft keyboard.
when tested file, running locally , worked. since then, have placed file on server , changed webview access html file through server. since placing on server, javascriptinterface functions no longer being called.
is there flag needs set or permission missing? have searched no avail.
here html code
<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 function in webview android app not called when file placed on 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 webview code
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 interface , set 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 disabled in webviews default, need enable in code.
webview mywebview = (webview) findviewbyid(r.id.webview); websettings websettings = mywebview.getsettings(); websettings.setjavascriptenabled(true);
Comments
Post a Comment