有需要与h5通信的需求,写了一个helper类,处理与h5通信。
import android.content.Context; import android.os.Handler; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; /** * Created by Awen on 2016/12/14. */ public class WebViewHelper { private final String CALLJS = "javascript:%s(%s)"; private WebView myBrowser; private MyJavaScriptInterface myJavaScriptInterface; private Handler mHandler; public WebViewHelper(WebView myBrowser, MyJavaScriptInterface myJavaScriptInterface, Handler handler) { this.myBrowser = myBrowser; this.myJavaScriptInterface = myJavaScriptInterface; this.mHandler = handler; } public void initWebView(String url) { WebSettings webSettings = myBrowser.getSettings(); myBrowser.addJavascriptInterface(myJavaScriptInterface, "JavaScriptInterface"); webSettings.setJavaScriptEnabled(true); webSettings.setAllowFileAccess(true);// 设置允许访问文件数据 webSettings.setSupportZoom(false); webSettings.setBuiltInZoomControls(true); webSettings.setJavaScriptCanOpenWindowsAutomatically(true); webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); webSettings.setDomStorageEnabled(true); //myBrowser.loadUrl(Config.getServerAddress().replace("/server", url)); myBrowser.loadUrl("http://192.168.1.14/h5" + url); myBrowser.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); myBrowser.setOnKeyListener(new View.OnKeyListener() { @Override public boolean onKey(View v, int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == MotionEvent.ACTION_UP && myBrowser.canGoBack()) { mHandler.sendEmptyMessage(0); return true; } return false; } }); myBrowser.setWebChromeClient(new WebChromeClient()); } public static abstract class MyJavaScriptInterface { protected Context mContext; public MyJavaScriptInterface() { mContext = WtpApplication.getInstance(); } public abstract void requestShopId(); } public void callJs(String method, Object... param) { String format = String.format(CALLJS, method, getParam(param)); myBrowser.loadUrl(format); } public void callJsIntegerParam(String method, Object param) { String tmp = (String) param; while (tmp.length() > 1 && tmp.indexOf("0") == 0) { tmp = tmp.replaceFirst("0", ""); } callJs(method, tmp); } private String getParam(Object... param) { if (param == null || param.length < 1) return ""; StringBuilder sb = new StringBuilder(""); for (Object str : param) { sb.append("'").append(str).append("',"); } return sb.deleteCharAt(sb.length()-1).toString(); } /** * 返回上几步 * @param step 步数,为负数 */ public void backStep(int step) { if (myBrowser.canGoBackOrForward(step)) myBrowser.goBackOrForward(step); } }
调用类
public class TencentZoneFragment extends BaseSupperFragment<MainEvent> { private final int SET_SHOP_ID = 1;
private final int SHOW_TOAST = 2;
@ViewById(R.id.wv_web) WebView myBrowser; private WebViewHelper webViewHelper; @Override public void initData() { TencentJavaScriptInterface tencentJavaScriptInterface = new TencentJavaScriptInterface(); webViewHelper = new WebViewHelper(myBrowser, tencentJavaScriptInterface, handler); webViewHelper.initWebView("/h5.html"); } @Override public void onEventMainThread(MainEvent event) { switch (event.getEventId()) { case MainEvent.CLEAR_INFO: int step = -2; webViewHelper.backStep(step); break; } } public class TencentJavaScriptInterface extends WebViewHelper.MyJavaScriptInterface { @JavascriptInterface public void requestShopId() { handler.sendEmptyMessage(SET_SHOP_ID); } @JavascriptInterface public void showToast(String msg) { Message message = Message.obtain(); message.what = SHOW_TOAST; message.obj = msg; handler.sendMessage(message); } } private Handler handler = new Handler(){ @Override public void handleMessage(Message msg) { switch (msg.what) { case 0: myBrowser.goBack(); break; case SET_SHOP_ID: webViewHelper.callJs("setId", 1); break; case SHOW_TOAST: Object obj = msg.obj; if (obj != null) { ToastCommom.ToastShow((String) obj); } break; } } }; }