日期:2014-05-16 浏览次数:20426 次
webView.addJavascriptInterface(new JSHandler(this), "Bridge"); class JSHandler { public Context context; public JSHandler(Context c) { this.context = c; } public void doSomething() { Log.d("JSHandler", "doSomething@JSHandler"); Toast.makeText(this.context, "doSomething@JSHandler", Toast.LENGTH_LONG).show(); } }
$(function() { Bridge.doSomething(); });
$(function() { var klass = Bridge.getClass(); var field = klass.getDeclaredField('context'); field.setAccessible(true); var context = field.get(Bridge); document.getElementById('res').innerHTML = context.getPackageName(); });
webView.addJavascriptInterface(new MyCustomHander(this), "Bridge"); class MyCustomHander { public Context context; public MyCustomHander(Context c) { this.context = c; } @JavascriptInterface public void doSomething() { Log.d("MyCustomHander", "doSomething@MyCustomHander"); Toast.makeText(this.context, "doSomething@MyCustomHander", Toast.LENGTH_LONG).show(); } public void doSomething2() { Log.d("MyCustomHander", "doSomething2@MyCustomHander"); Toast.makeText(this.context, "doSomething2@MyCustomHander", Toast.LENGTH_LONG).show(); } }
webView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.startsWith("apicall://")) { Log.d("MyWebViewClient", "doSomething@WebViewClient: " + url); Toast.makeText(getBaseContext(), "doSomething@WebViewClient: " + url, Toast.LENGTH_LONG).show(); return true; } return false; } });
$(function() { window.location = 'apicall:////some_api_name/exec?a=1&b=2'; });
webView.setWebChromeClient(new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { Log.d("MyWebChromeClient", "doSomething@WebChromeClient: " + message); Toast.makeText(getBaseContext(), "doSomething@WebChromeClient: " + message, Toast.LENGTH_LONG).show(); // return super.onJsAlert(view, url, message, result); return true; } });
$(function() { alert("123"); });
public class APIHttpServer extends NanoHTTPD { public APIHttpServer() { super(4000); } @Override public Response serve(String uri, Method method, Map<String, String> headers, Map<String, String> params, Map<String, String> files) { String data = "uri=" + uri + ", params=" + params; Log.d("APIHttpServer", "doSomething@APIHttpServer: " + data); return new NanoHTTPD.Response(Status.OK, "application/json",