1.WebBrowserActivity extends BaseActivity
2.setContentView(R.layout.activity_web_html);
<WebView android:id="@+id/wv_web_browser" android:layout_width="match_parent" android:layout_height="match_parent"/>
3.wv_web_browser = (WebView)findViewById(R.id.wv_web_browser);
WebSettings webSet = wv_web_browser.getSettings(); webSet.setJavaScriptEnabled(true); webSet.setJavaScriptCanOpenWindowsAutomatically(true); webSet.setLoadsImagesAutomatically(true); webSet.setSupportZoom(true); webSet.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS); webSet.setUseWideViewPort(true); webSet.setLoadWithOverviewMode(true); webSet.setMixedContentMode(0); // 设置出现缩放工具 webSet.setBuiltInZoomControls(true); int sysVersion = VERSION.SDK_INT; if (sysVersion >= 11) { webSet.setDisplayZoomControls(false); } else { ZoomButtonsController zbc = new ZoomButtonsController(wv_web_browser); zbc.getZoomControls().setVisibility(View.GONE); } // 设置可以访问文件 webSet.setAllowFileAccess(true); webSet.setCacheMode(WebSettings.LOAD_NO_CACHE); webSet.setAppCacheEnabled(true); webSet.setDomStorageEnabled(true); webSet.setDatabaseEnabled(true);
//通知h5应用状态 通知刷新html informHtmlReceiver = new InformHtmlReceiver(); IntentFilter informHtmlFilter = new IntentFilter(AppConstants.ACTION_APP_PACKAGENAME_INFORM_HTML); LocalBroadcastManager.getInstance(WebBrowserActivity.this).registerReceiver(informHtmlReceiver, informHtmlFilter);
// 增加h5与本地交互方法 wv_web_browser.addJavascriptInterface(new HtmlJavascriptInterface(),"jsObj"); wv_web_browser.addJavascriptInterface(new HtmlJavaAlarmInterface(), "mstore");
WebChromeClient wvcc = new WebChromeClient() { @Override public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title); if (!TextUtils.isEmpty(title)) { tv_classify_title.setText(title); } } }; wv_web_browser.setWebChromeClient(wvcc);
/** * 调用js更新H5,js方法 */ private class InformHtmlReceiver extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { final String packageName = intent.getStringExtra("packagename"); String state = intent.getStringExtra("state"); wv_web_browser.loadUrl("javascript:callDownState('" + packageName +"','"+state+ "')"); } }
public class HtmlJavaAlarmInterface { /** * 点击添加 * * @param apps_json */ @JavascriptInterface public void setAlarm(final String endTime, final String title, final String content) { if (CalendarUtil.tranceLong(endTime) != 0) { CalendarUtil.setAlarm(WebBrowserActivity.this, CalendarUtil.tranceLong(endTime), title, content); } } }