Webview in Android which works in 4.2+ and not in 2.3.3 -
application works in android 4.2.2 using webview javascriptinterface not working in android 2.3.3.
it has targetsdkversion="19"
and build property 4.2
i have created emulator 2.3.3 , device 3.7" wvga.
oncreate here -
@javascriptinterface @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); vwebview = (webview) findviewbyid(r.id.vwebview); btnview = (button) findviewbyid(r.id.btnview); txtview = (textview) findviewbyid(r.id.txtview); vwebview.setwebchromeclient(new webchromeclient()); vwebview.setwebviewclient(new webviewclient()); try { if ("2.3".equals(build.version.release)) { javascriptinterfacebroken = true; } } catch (exception e) { // ignore, , assume user javascript interface working correctly. } // add javascript interface if it's not broken if (!javascriptinterfacebroken) { vwebview.addjavascriptinterface(new jscriptinterface(), "androidfunction"); } websettings websetting = vwebview.getsettings(); websetting.setjavascriptenabled(true); log.d("main", "called"); vwebview.loadurl("file:///android_asset/sample.html"); btnview.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub //vwebview.loadurl("javascript:showandroidtoast('hello')"); try{ runonuithread(new runnable() { @override public void run() { vwebview.loadurl("javascript:showandroidtoast('hello')"); } }); } catch(exception ee){ log.d("exception", "called" + ee.tostring()); } } }); }
jsinterface -
public class jscriptinterface { public jscriptinterface() { // todo auto-generated constructor stub } public void showtoast(string toast) { try{ log.d("js", "called" + toast); // txtview.settext(toast); toast.maketext(mainactivity.this, toast, toast.length_long).show(); } catch(exception ee){ toast.maketext(mainactivity.this, ee.tostring(), toast.length_long).show(); } } }
i think might have bumped this known issue. , here's how work around it: handling android 2.3 webview’s broken addjavascriptinterface
credits original poster here
in future, consider describing issues in more details. "which not working" not give idea of problem, googled "javascriptinterface 2.3.3" -- again, have done yourself.
upd need put methods workaround overrides webviewclient
. code, should this:
vwebview.setwebchromeclient(new webchromeclient()); try { if ("2.3".equals(build.version.release)) { javascriptinterfacebroken = true; } } catch (exception e) { // ignore, , assume user javascript interface working correctly. } // add javascript interface if it's not broken if (!javascriptinterfacebroken) { vwebview.addjavascriptinterface(new jscriptinterface(), "androidfunction"); } if (!javascriptinterfacebroken) vwebview.setwebviewclient(new webviewclient()); else { vwebview.setwebviewclient(new webviewclient() { // , here put code article @override public void onpagefinished(webview view, string url) { ... } @override public boolean shouldoverrideurlloading(webview view, string url) { ... } }); }
Comments
Post a Comment