概述
何为h5,js与Android通信?其实说白了就是js调用Android本地方法和Android本地调用js方法之间的一个过程。这篇文章主要是讲述一个简单的通信方式,至于通信间数据的协议我用的是jsonRpc,在这里不做过多的描述,不懂的可以自行查阅,因为大多数人通信时不会用这个。
webview设置
这个应该不是问题,大概的设置就是一些基本项,这里贴一下代码,没有什么特殊的。
privatevoidinitWebView(){WebSettingswebSettings=webView.getSettings();webSettings.setJavaScriptEnabled(true);webSettings.setDatabaseEnabled(true);// webSettings.setDatabasePath("/data/data/" + Envi.appContext.getPackageName() + "/localstorage/");webSettings.setDomStorageEnabled(true);webSettings.setSavePassword(false);webSettings.setSaveFormData(true);webSettings.setSupportZoom(false);webSettings.setGeolocationEnabled(true);webSettings.setDomStorageEnabled(true);webSettings.setAppCacheEnabled(true);webSettings.setAllowFileAccess(true);// webSettings.setAppCacheMaxSize(100 * 1024 * 1024);// webSettings.setAppCachePath("/data/data/" + Envi.appContext.getPackageName() + "/cache/");// webSettings.setCacheMode(WebSettings.LOAD_DEFAULT);// String userAgent = webSettings.getUserAgentString();// userAgent = userAgent + " ganji_" + Envi.customerId + "_" + Envi.versionName;// webSettings.setUserAgentString(userAgent);}
这里可以根据需求注释/打开某些设置。
webView.setWebViewClient(newWebViewClient(){@OverridepublicvoidonLoadResource(WebViewview,Stringurl){super.onLoadResource(view,url);}@OverridepublicbooleanshouldOverrideUrlLoading(WebViewview,Stringurl){returnsuper.shouldOverrideUrlLoading(view,url);}@OverridepublicvoidonReceivedError(WebViewview,interrorCode,Stringdescription,StringfailingUrl){Log.e("html5","webview receive error: "+description);super.onReceivedError(view,errorCode,description,failingUrl);mWebViewReceivedError=true;}@OverridepublicvoidonPageFinished(WebViewview,Stringurl){mPageLoadFinished=true;if(mWebViewReceivedError){//TODO network is ok?webView.post(newRunnable(){@Overridepublicvoidrun(){webView.setVisibility(View.GONE);// mLoadFailContainer.setVisibility(View.VISIBLE);}});}}@OverridepublicvoidonPageStarted(WebViewview,Stringurl,Bitmapfavicon){super.onPageStarted(view,url,favicon);}});webView.setWebChromeClient(newWebChromeClient(){@OverridepublicvoidonGeolocationPermissionsShowPrompt(Stringorigin,GeolocationPermissions.Callbackcallback){callback.invoke(origin,true,false);super.onGeolocationPermissionsShowPrompt(origin,callback);}@OverridepublicbooleanonJsAlert(WebViewview,Stringurl,Stringmessage,JsResultresult){returntrue;}@OverridepublicbooleanonJsConfirm(WebViewview,Stringurl,Stringmessage,JsResultresult){returntrue;}@OverridepublicbooleanonJsPrompt(WebViewview,Stringurl,Stringmessage,StringdefaultValue,JsPromptResultresult){returntrue;}@OverridepublicvoidonExceededDatabaseQuota(Stringurl,StringdatabaseIdentifier,longquota,longestimatedDatabaseSize,longtotalQuota,WebStorage.QuotaUpdaterquotaUpdater){quotaUpdater.updateQuota(estimatedDatabaseSize*2);}@OverridepublicvoidonReachedMaxAppCacheSize(longrequiredStorage,longquota,WebStorage.QuotaUpdaterquotaUpdater){quotaUpdater.updateQuota(requiredStorage*2);}});
这里没什么参考的必要,主要是对两个Client的设置,根据你的需求来。
native 调用js方法
下面说重点,js调用native方法,首先,我贴下我js的代码:
从Android传来的值是:call Android from javascript