ips
鐢变簬WebView鐨勭敤娉曞疄鍦ㄥお澶氾紝濡傛灉鎮ㄥ彧鏄兂鏌ヨ鏌愪釜鍔熻兘鐨勪娇鐢ㄢ�斺�斿缓璁瓹trl+F锛圕ommad+F锛夊湪鏈〉闈㈡悳绱㈠叧閿瓧鏌ユ壘銆�
鏂囩珷缁欏墠鍗婇儴鍒嗗ぇ澶氭槸鏂规硶鐨勪粙缁嶏紝鑻ュ珜鐞愮鍙洿鎺ユ嫋鍒版渶鍚庣湅浠g爜婕旂ず銆�
Thanks for reading~锛�侃(鈾∥碘櫋 )鄱
鍓嶈█
鍠濋厭涓嶅紑杞︼紝寮�杞︿笉鍠濋厭銆�
鐩綍
WebView绠�浠�
WebView鍩烘湰浣跨敤
WebView甯哥敤鏂规硶
WebSettings
WebViewClient
WebChromeClient
JavaScript涓嶹ebView浜や簰
WebView鍔犺浇浼樺寲
椹剧収鑰冭瘯
涓婅矾
WebView绠�浠�
涓轰簡鏂逛究寮�鍙戣�呭疄鐜板湪app鍐呭睍绀虹綉椤靛苟涓庣綉椤典氦浜掔殑闇�姹傦紝Android SDK鎻愪緵浜哤ebView缁勪欢銆�
瀹冪户鎵胯嚜AbsoluteLayout锛屽睍绀虹綉椤电殑鍚屾椂锛屼篃鍙互鍦ㄥ叾涓斁鍏ュ叾浠栫殑瀛怴iew銆�
鐜板浠婏紝Hybrid搴旂敤浼间箮鍗犳嵁鐨凙PP鐨勪富娴佺被鍨嬶紝閭d箞鍏充簬WebView鐨勪娇鐢ㄥ氨鍙樺緱瓒婂彂鐨勯噸瑕併��
浠嶢ndroid
4.4锛圞itKat锛夊紑濮嬶紝鍘熸湰鍩轰簬WebKit鐨刉ebView寮�濮嬪熀浜嶤hromium鍐呮牳锛岃繖涓�鏀瑰姩澶уぇ鎻愬崌浜哤ebView缁勪欢鐨勬�ц兘浠ュ強瀵笻TML5,CSS3,JavaScript鐨勬敮鎸併�備笉杩囧畠鐨凙PI鍗存病鏈夊緢澶х殑鏀瑰姩锛屽湪鍏煎浣庣増鏈殑鍚屾椂鍙紩杩涗簡灏戦儴鍒嗘柊鐨凙PI锛屽苟涓嶉渶瑕佷綘鍋氬緢澶х殑鏀瑰姩銆�
涓嶈繃鏈夊嚑鐐规敼鍙橀渶瑕佹敞鎰忥紝浣嗘垜灏濊瘯鐫�缈昏瘧浜嗕笅锛屽彂鐜拌繕鏄嫳鏂囧師鏂囪寰楀ソ锛屾墍浠ユ垜璐撮摼鎺ュ惂~~~
Migrating to WebView in Android 4.4
鍦╓ebView涓紝鏈夊嚑涓湴鏂规槸鎴戜滑鍙互浣跨敤鏉ュ畾鍒舵垜浠殑WebView鍚勭琛屼负鐨勶紝鍒嗗埆鏄細WebSettings銆�JavaScriptInterface銆�WebViewClient浠ュ強WebChromeClient銆傝繖浜涙垜閮戒細鍦ㄦ帴涓嬫潵鐨勬枃绔犱腑涓�涓�浠嬬粛銆�
WebView鍩烘湰浣跨敤
涓嬮潰绠�鍗曚粙缁嶄笅WebView鐨勫熀鏈娇鐢細
棣栧厛鏂板缓涓�涓伐绋嬶紝鍦╨ayout鏂囦欢閲屾斁鍏ヤ竴涓猈ebView鎺т欢锛堝綋鐒朵篃鍙互閫氳繃Java浠g爜鍔ㄦ�佹斁鍏ワ紝杩欓噷涓嶆紨绀轰簡锛�
鐒跺悗鍦ˋctivity鐨刼nCreate鏂规硶閲屽啓鍏ュ涓嬩唬鐮侊細
Stringurl="https://www.google.com";WebViewwebView=(WebView)findViewById(R.id.web_view);webView.loadUrl(url);
鎺ョ潃鍦ˋndroidManifest澹版槑璁块棶缃戠粶鐨勬潈闄愶細
灏憋紝瀹屼簨浜唦
杩欐椂杩愯app锛屽畠宸茬粡鍙互璁块棶鎸囧畾鍦板潃鐨勭綉椤典簡銆�
涓婇潰鎻愬埌浜哤ebView缁ф壙鑷狝bsoluteLayout锛屽彲浠ュ湪鍏朵腑鏀惧叆涓�浜涘瓙View锛岄偅涔熼『鎵嬫潵涓�涓嬨��
Layout鏂囦欢鏀逛负锛�
Activity鐨刼nCreate閲屽姞涓婏細
Buttonbutton=(Button)findViewById(R.id.button);button.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){Toast.makeText(getApplicationContext(),"绯诲ソ瀹夊叏甯�!",Toast.LENGTH_SHORT).show();}});
杩欐椂锛岃繍琛宎pp锛岄噷闈㈠氨浼氬鍑轰竴涓狟utton~
浣嗗鏋滀綘鐪熺殑杩愯鐨勮瘽锛屼綘灏变細鍙戠幇锛宎pp浼氳嚜鍔ㄨ烦鍒版祻瑙堝櫒骞舵墦寮�鎸囧畾鐨勭綉椤碉紝鑰屽苟闈炲湪app鍐呭睍绀虹綉椤碉紝閭h繖灏变笌鎴戜滑鐨勫垵琛疯儗閬撹�岄┌浜嗭紝閭d箞瑕佸浣曞疄鐜扮綉椤靛湪App鍐呮墦寮�鍛�?杩欏氨寮曞嚭浜嗕笅闈㈢殑绔犺妭浼氭彁鍒扮殑涓滆タ锛歐ebViewClient銆傛垜鍏堝皢浠g爜璐村嚭锛屽叿浣撳疄鐜板師鐞嗙暀鍒颁笅鑺傝鏄庛��
鏈�缁圶ML甯冨眬灏卞涓婇潰閭f牱锛孞ava浠g爜锛堟渶缁堬級濡備笅锛�
publicclassMainActivityextendsAppCompatActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);Stringurl="https://www.google.com";WebViewwebView=(WebView)findViewById(R.id.web_view);webView.loadUrl(url);webView.setWebViewClient(newWebViewClient(){@OverridepublicbooleanshouldOverrideUrlLoading(WebViewview,WebResourceRequestrequest){view.loadUrl(request.toString());returntrue;}});Buttonbutton=(Button)findViewById(R.id.button);button.setOnClickListener(newView.OnClickListener(){@OverridepublicvoidonClick(Viewview){Toast.makeText(getApplicationContext(),"绯诲ソ瀹夊叏甯�!",Toast.LENGTH_SHORT).show();}});}}
鏁堟灉鍥撅細鏁堟灉瑙嗛
WebView绠�浠�
鎺ヤ笅鏉ュ啀浠嬬粛涓�浜沇ebView鐨勫父鐢ㄦ柟娉曪紝鍏蜂綋婕旂ず浼氬湪鍚庨潰绔犺妭鐨勪唬鐮侀噷缁熶竴灞曠ず銆�
String getUrl()锛氳幏鍙栧綋鍓嶉〉闈㈢殑URL銆�
reload()锛氶噸鏂皉eload褰撳墠鐨刄RL锛屽嵆鍒锋柊銆�
boolean canGoBack()锛氱敤鏉ョ‘璁ebView閲屾槸鍚﹁繕鏈夊彲鍥為��鐨勫巻鍙茶褰曘�傞�氬父鎴戜滑浼氬湪WebView閲岄噸鍐欒繑鍥為敭鐨勭偣鍑讳簨浠讹紝閫氳繃璇ユ柟娉曞垽鏂璚ebView閲屾槸鍚﹁繕鏈夊巻鍙茶褰曪紝鑻ユ湁鍒欒繑鍥炰笂涓�椤点��
boolean canGoForward()锛氱敤鏉ョ‘璁ebView鏄惁杩樻湁鍙悜鍓嶇殑鍘嗗彶璁板綍銆�
boolean canGoBackOrForward(int steps)锛氫互褰撳墠鐨勯〉闈负璧峰鐐癸紝鐢ㄦ潵纭WebView鐨勫巻鍙茶褰曟槸鍚﹁冻浠ュ悗閫�鎴栧墠杩涚粰瀹氱殑姝ユ暟锛屾鏁颁负鍓嶈繘锛岃礋鏁颁负鍚庨��銆�
goBack()锛氬湪WebView鍘嗗彶璁板綍鍚庨��鍒颁笂涓�椤广��
goForward()锛氬湪WebView鍘嗗彶璁板綍閲屽墠杩涘埌涓嬩竴椤广��
goBackOrForward(int steps)锛氫互褰撳墠椤甸潰涓鸿捣濮嬬偣锛屽墠杩涙垨鍚庨��鍘嗗彶璁板綍涓寚瀹氱殑姝ユ暟锛屾鏁颁负鍓嶈繘锛岃礋鏁颁负鍚庨��銆�
clearCache(boolean includeDiskFiles)锛氭竻绌虹綉椤佃闂暀涓嬬殑缂撳瓨鏁版嵁銆傞渶瑕佹敞鎰忕殑鏃讹紝鐢变簬缂撳瓨鏄叏灞�鐨勶紝鎵�浠ュ彧瑕佹槸WebView鐢ㄥ埌鐨勭紦瀛橀兘浼氳娓呯┖锛屽嵆渚垮叾浠栧湴鏂逛篃浼氫娇鐢ㄥ埌銆傝鏂规硶鎺ュ彈涓�涓弬鏁帮紝浠庡懡鍚嶅嵆鍙湅鍑轰綔鐢ㄣ�傝嫢璁句负false锛屽垯鍙竻绌哄唴瀛橀噷鐨勮祫婧愮紦瀛橈紝鑰屼笉娓呯┖纾佺洏閲岀殑銆�
clearHistory()锛氭竻闄ゅ綋鍓峸ebview璁块棶鐨勫巻鍙茶褰曘��
clearFormData()锛氭竻闄よ嚜鍔ㄥ畬鎴愬~鍏呯殑琛ㄥ崟鏁版嵁銆傞渶瑕佹敞鎰忕殑鏄紝璇ユ柟娉曚粎浠呮竻闄�褰撳墠琛ㄥ崟鍩熻嚜鍔ㄥ畬鎴愬~鍏呯殑琛ㄥ崟鏁版嵁锛屽苟涓嶄細娓呴櫎WebView瀛樺偍鍒版湰鍦扮殑鏁版嵁銆�
onPause()锛氬綋椤甸潰琚け鍘荤劍鐐硅鍒囨崲鍒板悗鍙颁笉鍙鐘舵�侊紝闇�瑕佹墽琛宱nPause鎿嶄綔锛岃鎿嶄綔浼氶�氱煡鍐呮牳瀹夊叏鍦版殏鍋滄墍鏈夊姩浣滐紝姣斿鍔ㄧ敾鐨勬墽琛屾垨瀹氫綅鐨勮幏鍙栫瓑銆傞渶瑕佹敞鎰忕殑鏄鏂规硶骞朵笉浼氭殏鍋淛avaScript鐨勬墽琛岋紝鑻ヨ鏆傚仠JavaScript鐨勬墽琛岃浣跨敤鎺ヤ笅鏉ョ殑杩欎釜鏂规硶銆�
onResume()锛氬湪鍏堝墠璋冪敤onPause()鍚庯紝鎴戜滑鍙互璋冪敤璇ユ柟娉曟潵鎭㈠WebView鐨勮繍琛屻��
pauseTimers()锛氳鏂规硶闈㈠悜鍏ㄥ眬鏁翠釜搴旂敤绋嬪簭鐨剋ebview锛屽畠浼氭殏鍋滄墍鏈墂ebview鐨刲ayout锛宲arsing锛孞avaScript Timer銆傚綋绋嬪簭杩涘叆鍚庡彴鏃讹紝璇ユ柟娉曠殑璋冪敤鍙互闄嶄綆CPU鍔熻�椼��
resumeTimers()锛氭仮澶峱auseTimers鏃剁殑鎵�鏈夋搷浣溿��
destroy()锛氶攢姣乄ebView銆傞渶瑕佹敞鎰忕殑鏄細杩欎釜鏂规硶鐨勮皟鐢ㄥ簲鍦╓ebView浠庣埗瀹瑰櫒涓remove鎺変箣鍚庛�傛垜浠彲浠ユ墜鍔ㄥ湴璋冪敤
rootLayout.removeView(webView);webView.destroy();
getScrollY()锛氳鏂规硶杩斿洖鐨勫綋鍓嶅彲瑙佸尯鍩熺殑椤剁璺濇暣涓〉闈㈤《绔殑璺濈锛屼篃灏辨槸褰撳墠鍐呭婊氬姩鐨勮窛绂汇��
getHeight()锛氭柟娉曢兘杩斿洖褰撳墠WebView杩欎釜瀹瑰櫒鐨勯珮搴︺�傚叾瀹炰互涓婁袱涓柟娉曢兘灞炰簬View銆�
getContentHeight()锛氳鏂规硶杩斿洖鏁翠釜HTML椤甸潰鐨勯珮搴︼紝浣嗚楂樺害鍊煎苟涓嶇瓑鍚屼簬褰撳墠鏁翠釜椤甸潰鐨勯珮搴︼紝鍥犱负WebView鏈夌缉鏀惧姛鑳斤紝 鎵�浠ュ綋鍓嶆暣涓〉闈㈢殑楂樺害瀹為檯涓婂簲璇ユ槸鍘熷HTML鐨勯珮搴﹀啀涔樹笂缂╂斁姣斾緥銆傚洜姝わ紝鍑嗙‘鐨勫垽鏂柟娉曞簲璇ユ槸
if(webView.getContentHeight()*webView.getScale()==(webView.getHeight()+webView.getScrollY())){//宸茬粡澶勪簬搴曠}if(webView.getScrollY()==0){//澶勪簬椤剁}
pageUp(boolean top)锛氬皢WebView灞曠ず鐨勯〉闈㈡粦鍔ㄨ嚦椤堕儴銆�
pageDown(boolean bottom)锛氬皢WebView灞曠ず鐨勯〉闈㈡粦鍔ㄨ嚦搴曢儴銆�
WebSettings
WebSettings鏄敤鏉ョ鐞哤ebView閰嶇疆鐨勭被銆傚綋WebView绗竴娆″垱寤烘椂锛屽唴閮ㄤ細鍖呭惈涓�涓粯璁ら厤缃殑闆嗗悎銆傝嫢鎴戜滑鎯虫洿鏀硅繖浜涢厤缃紝渚垮彲浠ラ�氳繃WebSettings閲岀殑鏂规硶鏉ヨ繘琛岃缃��
WebSettings瀵硅薄鍙互閫氳繃WebView.getSettings()鑾峰緱锛屽畠鐨勭敓鍛藉懆鏈熸槸涓庡畠鐨刉ebView鏈韩鎭伅鐩稿叧鐨勶紝濡傛灉WebView琚攢姣佷簡锛岄偅涔堜换浣曠敱WebSettings璋冪敤鐨勬柟娉曚篃鍚屾牱涓嶈兘浣跨敤銆�
鑾峰彇WebSettings瀵硅薄
WebSettingswebSettings=webView.getSettings();
WebSettings甯哥敤鏂规硶
锛堝嚑涔庢墍鏈夌殑set鏂规硶閮芥湁鐩稿簲鐨刧et鏂规硶锛岃繖閲屽氨鍙粙缁峴et浜嗐��鍙︼紝鎵�鏈夋湭鍐欐柟娉曡繑鍥炲�肩被鍨嬬殑鐨嗕负绌虹被鍨�锛�
setJavaScriptEnabled(boolean flag)锛氳缃甒ebView鏄惁鍙互杩愯JavaScript銆�
setJavaScriptCanOpenWindowsAutomatically(boolean flag)锛氳缃甒ebView鏄惁鍙互鐢盝avaScript鑷姩鎵撳紑绐楀彛锛岄粯璁や负false锛岄�氬父涓嶫avaScript鐨�window.open()閰嶅悎浣跨敤銆�
setAllowFileAccess(boolean allow)锛氬惎鐢ㄦ垨绂佺敤WebView璁块棶鏂囦欢鏁版嵁銆�
setBlockNetworkImage(boolean flag)锛氱姝㈡垨鍏佽WebView浠庣綉缁滀笂鍔犺浇鍥剧墖銆傞渶瑕佹敞鎰忕殑鏄紝濡傛灉璁剧疆鏄粠绂佹鍒板厑璁哥殑杞彉鐨勮瘽锛屽浘鐗囨暟鎹苟涓嶄細鍦ㄨ缃敼鍙樺悗绔嬪埢鍘昏幏鍙栵紝鑰屾槸鍦╓ebView璋冪敤reload()鐨勬椂鍊欐墠浼氱敓鏁堛��
杩欎釜鏃跺�欙紝闇�瑕佺‘淇濊繖涓猘pp鎷ユ湁璁块棶Internet鐨勬潈闄愶紝鍚﹀垯浼氭姏鍑哄畨鍏ㄥ紓甯搞��
閫氬父娌℃湁绂佹鍥剧墖鍔犺浇鐨勯渶姹傜殑鏃跺�欙紝瀹屽叏涓嶇敤绠¤繖涓柟娉曪紝鍥犱负褰撴垜浠殑app鎷ユ湁璁块棶Internet鐨勬潈闄愭椂锛岃繖涓猣lag鐨勯粯璁ゅ�煎氨鏄痜alse銆�
setSupportZoom(boolean support)锛氳缃槸鍚︽敮鎸佺缉鏀俱��
setBuiltInZoomControls(boolean enabled)锛氭樉绀烘垨涓嶆樉绀虹缉鏀炬寜閽紙wap缃戦〉涓嶆敮鎸侊級銆�
setSupportMultipleWindows(boolean support)锛氳缃甒ebView鏄惁鏀寔澶氱獥鍙c��
setLayoutAlgorithm(WebSettings.LayoutAlgorithm l)锛氭寚瀹歐ebView鐨勯〉闈㈠竷灞�鏄剧ず褰㈠紡锛岃皟鐢ㄨ鏂规硶浼氬紩璧烽〉闈㈤噸缁樸�傞粯璁ゅ�间负LayoutAlgorithm#NARROW_COLUMNS銆�
setNeedInitialFocus(boolean flag)锛氶�氱煡WebView鏄惁闇�瑕佽缃竴涓妭鐐硅幏鍙栫劍鐐瑰綋WebView#requestFocus(int,android.graphics.Rect)琚皟鐢ㄦ椂锛岄粯璁や负true銆�
setAppCacheEnabled(boolean flag)锛氬惎鐢ㄦ垨绂佺敤搴旂敤缂撳瓨銆�
setAppCachePath(String appCachePath)锛氳缃簲鐢ㄧ紦瀛樿矾寰勶紝杩欎釜璺緞蹇呴』鏄彲浠ヨapp鍐欏叆鏂囦欢鐨勩�傝鏂规硶搴旇鍙璋冪敤涓�娆★紝閲嶅璋冪敤浼氳鏃犺~
setCacheMode(int mode)锛�鐢ㄦ潵璁剧疆WebView鐨勭紦瀛樻ā寮忋��褰撴垜浠姞杞介〉闈㈡垨浠庝笂涓�涓〉闈㈣繑鍥炵殑鏃跺�欙紝浼氭寜鐓ц缃殑缂撳瓨妯″紡鍘绘鏌ュ苟浣跨敤锛堟垨涓嶄娇鐢級缂撳瓨銆�
缂撳瓨妯″紡鏈夊洓绉嶏細
LOAD_DEFAULT锛氶粯璁ょ殑缂撳瓨浣跨敤妯″紡銆傚湪杩涜椤甸潰鍓嶈繘鎴栧悗閫�鐨勬搷浣滄椂锛屽鏋滅紦瀛樺彲鐢ㄥ苟鏈繃鏈熷氨浼樺厛鍔犺浇缂撳瓨锛屽惁鍒欎粠缃戠粶涓婂姞杞芥暟鎹�傝繖鏍峰彲浠ュ噺灏戦〉闈㈢殑缃戠粶璇锋眰娆℃暟銆�
LOAD_CACHE_ELSE_NETWORK锛氬彧瑕佺紦瀛樺彲鐢ㄥ氨鍔犺浇缂撳瓨锛屽摢鎬曞畠浠凡缁忚繃鏈熷け鏁堛�傚鏋滅紦瀛樹笉鍙敤灏变粠缃戠粶涓婂姞杞芥暟鎹��
LOAD_NO_CACHE锛氫笉鍔犺浇缂撳瓨锛屽彧浠庣綉缁滃姞杞芥暟鎹��
LOAD_CACHE_ONLY锛氫笉浠庣綉缁滃姞杞芥暟鎹紝鍙粠缂撳瓨鍔犺浇鏁版嵁銆�
閫氬父鎴戜滑鍙互鏍规嵁缃戠粶鎯呭喌灏嗚繖鍑犵妯″紡缁撳悎浣跨敤锛屾瘮濡傛湁缃戠殑鏃跺�欎娇鐢↙OAD_DEFAULT锛岀绾挎椂浣跨敤LOAD_CACHE_ONLY銆丩OAD_CACHE_ELSE_NETWORK锛岃鐢ㄦ埛涓嶈嚦浜庡湪绂荤嚎鏃跺暐閮界湅涓嶅埌銆�
setDatabaseEnabled(boolean flag)锛氬惎鐢ㄦ垨绂佺敤鏁版嵁搴撶紦瀛樸��
setDomStorageEnabled(boolean flag)锛氬惎鐢ㄦ垨绂佺敤DOM缂撳瓨銆�
setUserAgentString(String ua)锛氳缃甒ebView鐨刄serAgent鍊笺��
setDefaultEncodingName(String encoding)锛氳缃紪鐮佹牸寮忥紝閫氬父閮借涓衡�淯TF-8鈥濄��
setStandardFontFamily(String font)锛氳缃爣鍑嗙殑瀛椾綋鏃忥紝榛樿鈥渟ans-serif鈥濄��
setCursiveFontFamily锛氳缃崏涔﹀瓧浣撴棌锛岄粯璁も�渃ursive鈥濄��
setFantasyFontFamily锛氳缃瓹ursiveFont瀛椾綋鏃忥紝榛樿鈥渃ursive鈥濄��
setFixedFontFamily锛氳缃贩鍚堝瓧浣撴棌锛岄粯璁も�渕onospace鈥濄��
setSansSerifFontFamily锛氳缃⒌鏂囧瓧浣撴棌锛岄粯璁も�渟ans-serif鈥濄��
setSerifFontFamily锛氳缃‖绾垮瓧浣撴棌锛岄粯璁も�渟ans-serif鈥�
setDefaultFixedFontSize(int size)锛氳缃粯璁ゅ~鍏呭瓧浣撳ぇ灏忥紝榛樿16锛屽彇鍊煎尯闂翠负[1-72]锛岃秴杩囪寖鍥达紝浣跨敤鍏朵笂闄愬�笺��
setDefaultFontSize(int size)锛氳缃粯璁ゅ瓧浣撳ぇ灏忥紝榛樿16锛屽彇鍊煎尯闂碵1-72]锛岃秴杩囪寖鍥达紝浣跨敤鍏朵笂闄愬�笺��
setMinimumFontSize锛氳缃渶灏忓瓧浣擄紝榛樿8. 鍙栧�煎尯闂碵1-72]锛岃秴杩囪寖鍥达紝浣跨敤鍏朵笂闄愬�笺��
setMinimumLogicalFontSize锛氳缃渶灏忛�昏緫瀛椾綋锛岄粯璁�8. 鍙栧�煎尯闂碵1-72]锛岃秴杩囪寖鍥达紝浣跨敤鍏朵笂闄愬�笺��
浠ヤ笂灏辨槸涓�浜沇ebSettings鐨勫父鐢ㄦ柟娉曪紝鍏蜂綋鐨勪娇鐢ㄤ互鍙婁竴浜涚紦瀛樼殑闂浼氬湪鎺ヤ笅鏉ョ殑浠g爜浠ュ強鏂囩珷涓湁鏇村姞鐩磋鐨勮鏄庛��
WebViewClient
浠庡悕瀛椾笂涓嶉毦鐞嗚В锛岃繖涓被灏卞儚WebView鐨勫鎵樹汉涓�鏍凤紝鏄府鍔¦ebView澶勭悊鍚勭閫氱煡鍜岃姹備簨浠剁殑锛屾垜浠彲浠ョО浠栦负WebView鐨勨�滃唴鏀垮ぇ鑷b�濄��
onLoadResource(WebView view, String url)锛氳鏂规硶鍦ㄥ姞杞介〉闈㈣祫婧愭椂浼氬洖璋冿紝姣忎竴涓祫婧愶紙姣斿鍥剧墖锛夌殑鍔犺浇閮戒細璋冪敤涓�娆°��
onPageStarted(WebView view, String url, Bitmap favicon)锛氳鏂规硶鍦╓ebView寮�濮嬪姞杞介〉闈笖浠呭湪Main frame loading锛堝嵆鏁撮〉鍔犺浇锛夋椂鍥炶皟锛屼竴娆ain frame鐨勫姞杞藉彧浼氬洖璋冭鏂规硶涓�娆°�傛垜浠彲浠ュ湪杩欎釜鏂规硶閲岃瀹氬紑鍚竴涓姞杞界殑鍔ㄧ敾锛屽憡璇夌敤鎴风▼搴忓湪绛夊緟缃戠粶鐨勫搷搴斻��
onPageFinished(WebView view, String url)锛氳鏂规硶鍙湪WebView瀹屾垚涓�涓〉闈㈠姞杞芥椂璋冪敤涓�娆★紙鍚屾牱涔熷彧鍦�Main frame loading鏃惰皟鐢級锛屾垜浠彲浠ュ彲浠ュ湪姝ゆ椂鍏抽棴鍔犺浇鍔ㄧ敾锛岃繘琛屽叾浠栨搷浣溿��
onReceivedError(WebView view, WebResourceRequest request, WebResourceError error)锛氳鏂规硶鍦╳eb椤甸潰鍔犺浇閿欒鏃跺洖璋冿紝杩欎簺閿欒閫氬父閮芥槸鐢辨棤娉曚笌鏈嶅姟鍣ㄦ甯歌繛鎺ュ紩璧风殑锛屾渶甯歌鐨勫氨鏄綉缁滈棶棰樸�傝繖涓柟娉曟湁涓や釜鍦版柟闇�瑕佹敞鎰忥細
1.杩欎釜鏂规硶鍙湪涓庢湇鍔″櫒鏃犳硶姝e父杩炴帴鏃惰皟鐢紝绫讳技浜庢湇鍔″櫒杩斿洖閿欒鐮佺殑閭g閿欒锛堝嵆HTTP ERROR锛夛紝璇ユ柟娉曟槸涓嶄細鍥炶皟鐨勶紝鍥犱负浣犲凡缁忓拰鏈嶅姟鍣ㄦ甯歌繛鎺ヤ笂浜嗭紙鍏ㄦ�畼鏂规枃妗�(锔禴锔�)锛夛紱
2.杩欎釜鏂规硶鏄柊鐗堟湰鐨刼nReceivedError()鏂规硶锛屼粠API23寮�濮嬪紩杩涳紝涓庢棫鏂规硶onReceivedError(WebView view,int errorCode,String description,String failingUrl)涓嶅悓鐨勬槸锛屾柊鏂规硶鍦ㄩ〉闈㈠眬閮ㄥ姞杞藉彂鐢熼敊璇椂涔熶細琚皟鐢紙姣斿椤甸潰閲屼袱涓瓙Tab鎴栬�呬竴寮犲浘鐗囷級銆傝繖灏辨剰鍛崇潃璇ユ柟娉曠殑璋冪敤棰戠巼鍙兘浼氭洿鍔犻绻侊紝鎵�浠ユ垜浠簲璇ュ湪璇ユ柟娉曢噷鎵ц灏介噺灏戠殑鎿嶄綔銆�
onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse)锛氫笂涓�涓柟娉曟彁鍒皁nReceivedError骞朵笉浼氬湪鏈嶅姟鍣ㄨ繑鍥為敊璇爜鏃惰鍥炶皟锛岄偅涔堝綋鎴戜滑闇�瑕佹崟鎹塇TTP ERROR骞惰繘琛岀浉搴旀搷浣滄椂搴旇鎬庝箞鍔炲憿锛�API23渚垮紩鍏ヤ簡璇ユ柟娉曘�傚綋鏈嶅姟鍣ㄨ繑鍥炰竴涓狧TTP ERROR骞朵笖瀹冪殑status code>=400鏃讹紝璇ユ柟娉曚究浼氬洖璋冦�傝繖涓柟娉曠殑浣滅敤鍩熷苟涓嶅眬闄愪簬Main Frame锛屼换浣曡祫婧愮殑鍔犺浇寮曞彂HTTP ERROR閮戒細寮曡捣璇ユ柟娉曠殑鍥炶皟锛屾墍浠ユ垜浠篃搴旇鍦ㄨ鏂规硶閲屾墽琛屽敖閲忓皯鐨勬搷浣滐紝鍙繘琛岄潪甯稿繀瑕佺殑閿欒澶勭悊绛夈��
onReceivedSslError(WebView view, SslErrorHandler handler, SslError error)锛氬綋WebView鍔犺浇鏌愪釜璧勬簮寮曞彂SSL閿欒鏃朵細鍥炶皟璇ユ柟娉曪紝杩欐椂WebView瑕佷箞鎵цhandler.cancel()鍙栨秷鍔犺浇锛岃涔堟墽琛宧andler.proceed()鏂规硶缁х画鍔犺浇锛堥粯璁や负cancel锛夈�傞渶瑕佹敞鎰忕殑鏄紝杩欎釜鍐冲畾鍙兘浼氳淇濈暀骞跺湪灏嗘潵鍐嶆閬囧埌SSL閿欒鏃舵墽琛屽悓鏍风殑鎿嶄綔銆�
WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request)锛氬綋WebView闇�瑕佽姹傛煇涓暟鎹椂锛岃繖涓柟娉曞彲浠ユ嫤鎴璇锋眰鏉ュ憡鐭pp骞朵笖鍏佽app鏈韩杩斿洖涓�涓暟鎹潵鏇夸唬鎴戜滑鍘熸湰瑕佸姞杞界殑鏁版嵁銆�
姣斿浣犲web鐨勬煇涓猨s鍋氫簡鏈湴缂撳瓨锛屽笇鏈涘湪鍔犺浇璇s鏃朵笉鍐嶅幓璇锋眰鏈嶅姟鍣ㄨ�屾槸鍙互鐩存帴璇诲彇鏈湴缂撳瓨鐨刯s锛岃繖涓柟娉曞氨鍙互甯姪浣犲畬鎴愯繖涓渶姹傘�備綘鍙互鍐欎竴浜涢�昏緫妫�娴嬭繖涓猺equest锛屽苟杩斿洖鐩稿簲鐨勬暟鎹紝浣犺繑鍥炵殑鏁版嵁灏变細琚玏ebView浣跨敤锛屽鏋滀綘杩斿洖null锛學ebView浼氱户缁悜鏈嶅姟鍣ㄨ姹傘��
boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)锛氬搱~ 缁堜簬鍒颁簡杩欎釜鏂规硶锛屽湪鏈�寮�濮嬬殑鍩虹婕旂ず鏃舵垜浠敤鍒颁簡杩欎釜鏂规硶銆備粠瀹炶返涓垜浠煡閬擄紝褰撴垜浠病鏈夌粰WebView鎻愪緵WebViewClient鏃讹紝WebView濡傛灉瑕佸姞杞戒竴涓猽rl浼氬悜ActivityManager瀵绘眰涓�涓�傚悎鐨勫鐞嗚�呮潵鍔犺浇璇rl锛堟瘮濡傜郴缁熻嚜甯︾殑娴忚鍣級锛岃繖閫氬父鏄垜浠笉鎯崇湅鍒扮殑銆備簬鏄垜浠渶瑕佺粰WebView鎻愪緵涓�涓猈ebViewClient锛屽苟閲嶅啓璇ユ柟娉曡繑鍥瀟rue鏉ュ憡鐭ebView url鐨勫姞杞藉氨鍦╝pp涓繘琛屻�傝繖鏃朵究鍙互瀹炵幇鍦╝pp鍐呰闂綉椤点��
onScaleChanged(WebView view, float oldScale, float newScale)锛氬綋WebView寰楅〉闈cale鍊煎彂鐢熸敼鍙樻椂鍥炶皟銆�
boolean shouldOverrideKeyEvent(WebView view, KeyEvent event)锛氶粯璁ゅ�间负false锛岄噸鍐欐鏂规硶骞秗eturn true鍙互璁╂垜浠湪WebView鍐呭鐞嗘寜閿簨浠躲��
WebChromeClient
濡傛灉璇碬ebViewClient鏄府鍔¦ebView澶勭悊鍚勭閫氱煡銆佽姹備簨浠剁殑鈥滃唴鏀垮ぇ鑷b�濈殑璇濓紝閭d箞WebChromeClient灏辨槸杈呭姪WebView澶勭悊Javascript鐨勫璇濇锛岀綉绔欏浘鏍囷紝缃戠珯title锛屽姞杞借繘搴︾瓑鍋忓閮ㄤ簨浠剁殑鈥滃浜ゅぇ鑷b�濄��
onProgressChanged(WebView view, int newProgress)锛氬綋椤甸潰鍔犺浇鐨勮繘搴﹀彂鐢熸敼鍙樻椂鍥炶皟锛岀敤鏉ュ憡鐭ヤ富绋嬪簭褰撳墠椤甸潰鐨勫姞杞借繘搴︺��
onReceivedIcon(WebView view, Bitmap icon)锛氱敤鏉ユ帴鏀秝eb椤甸潰鐨刬con锛屾垜浠彲浠ュ湪杩欓噷灏嗚椤甸潰鐨刬con璁剧疆鍒癟oolbar銆�
onReceivedTitle(WebView view, String title)锛氱敤鏉ユ帴鏀秝eb椤甸潰鐨則itle锛屾垜浠彲浠ュ湪杩欓噷灏嗛〉闈㈢殑title璁剧疆鍒癟oolbar銆�
浠ヤ笅涓や釜鏂规硶鏄负浜嗘敮鎸亀eb椤甸潰杩涘叆鍏ㄥ睆妯″紡鑰屽瓨鍦ㄧ殑锛堟瘮濡傛挱鏀捐棰戯級锛屽鏋滀笉瀹炵幇杩欎袱涓柟娉曪紝璇eb涓婄殑鍐呭渚夸笉鑳借繘鍏ュ叏灞忔ā寮忋��
onShowCustomView(View view, WebChromeClient.CustomViewCallback callback)锛氳鏂规硶鍦ㄥ綋鍓嶉〉闈㈣繘鍏ュ叏灞忔ā寮忔椂鍥炶皟锛屼富绋嬪簭蹇呴』鎻愪緵涓�涓寘鍚綋鍓峸eb鍐呭锛堣棰� or Something锛夌殑鑷畾涔夌殑View銆�
onHideCustomView()锛氳鏂规硶鍦ㄥ綋鍓嶉〉闈㈤��鍑哄叏灞忔ā寮忔椂鍥炶皟锛屼富绋嬪簭搴斿湪杩欐椂闅愯棌涔嬪墠show鍑烘潵鐨刅iew銆�
Bitmap getDefaultVideoPoster()锛氬綋鎴戜滑鐨刉eb椤甸潰鍖呭惈瑙嗛鏃讹紝鎴戜滑鍙互鍦℉TML閲屼负瀹冭缃竴涓瑙堝浘锛學ebView浼氬湪缁樺埗椤甸潰鏃舵牴鎹畠鐨勫楂樹负瀹冨竷灞�銆傝�屽綋鎴戜滑澶勪簬寮辩綉鐘舵�佷笅鏃讹紝鎴戜滑娌℃湁姣旇緝蹇殑鑾峰彇璇ュ浘鐗囷紝閭ebView缁樺埗椤甸潰鏃剁殑gitWidth()鏂规硶灏变細鎶ュ嚭绌烘寚閽堝紓甯竳 浜庢槸app灏眂rash浜嗐�傘��
杩欐椂鎴戜滑灏遍渶瑕侀噸鍐欒鏂规硶锛屽湪鎴戜滑灏氭湭鑾峰彇web椤甸潰涓婄殑video棰勮鍥炬椂锛岀粰浜堝畠涓�涓湰鍦扮殑鍥剧墖锛岄伩鍏嶇┖鎸囬拡鐨勫彂鐢熴��
View getVideoLoadingProgressView()锛氶噸鍐欒鏂规硶鍙互鍦ㄨ棰憀oading鏃剁粰浜堜竴涓嚜瀹氫箟鐨刅iew锛屽彲浠ユ槸鍔犺浇鍦嗙幆 or something銆�
boolean onJsAlert(WebView view, String url, String message, JsResult result)锛氬鐞咼avascript涓殑Alert瀵硅瘽妗嗐��
boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result)锛氬鐞咼avascript涓殑Prompt瀵硅瘽妗嗐��
boolean onJsConfirm(WebView view, String url, String message, JsResult result)锛氬鐞咼avascript涓殑Confirm瀵硅瘽妗�
boolean onShowFileChooser(WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams)锛氳鏂规硶鍦ㄧ敤鎴疯繘琛屼簡web涓婃煇涓渶瑕佷笂浼犳枃浠剁殑鎿嶄綔鏃跺洖璋冦�傛垜浠簲璇ュ湪杩欓噷鎵撳紑涓�涓枃浠堕�夋嫨鍣紝濡傛灉瑕佸彇娑堣繖涓姹傛垜浠彲浠ヨ皟鐢�filePathCallback.onReceiveValue(null)骞惰繑鍥�true銆�
onPermissionRequest(PermissionRequest request)锛氳鏂规硶鍦╳eb椤甸潰璇锋眰鏌愪釜灏氭湭琚厑璁告垨鎷掔粷鐨勬潈闄愭椂鍥炶皟锛屼富绋嬪簭鍦ㄦ鏃惰皟鐢�grant(String [])鎴�deny()鏂规硶銆傚鏋滆鏂规硶娌℃湁琚噸鍐欙紝鍒欓粯璁ゆ嫆缁漺eb椤甸潰璇锋眰鐨勬潈闄愩��
onPermissionRequestCanceled(PermissionRequest request)锛氳鏂规硶鍦╳eb鏉冮檺鐢宠鏉冮檺琚彇娑堟椂鍥炶皟锛岃繖鏃跺簲璇ラ殣钘忎换浣曚笌涔嬬浉鍏崇殑UI鐣岄潰銆�
Js涓嶹ebView浜や簰
鏃㈢劧鍡ㄩ笩搴旂敤澶ц鍏堕亾锛岄偅涔堟鏃犵枒闂瓵ndroid涓嶫avaScript鐨勪氦浜掓垜浠篃蹇呴』浜嗚В娓呮锛屼笅闈㈡潵浠嬬粛涓�涓婮avaScript涓嶢ndroid鏄浣曚簰鐩歌皟鐢ㄧ殑銆�
鍒╃敤WebView璋冪敤缃戦〉涓婄殑JavaScript浠g爜
鍦╓ebView涓皟鐢↗s鐨勫熀鏈牸寮忎负webView.loadUrl("javascript:methodName(parameterValues)");
鐜版湁浠ヤ笅杩欐JavaScript浠g爜
functionreadyToGo(){alert("Hello")}functionalertMessage(message){alert(message)}functiongetYourCar(){return"Car";}
1. WebView璋冪敤JavaScript鏃犲弬鏃犺繑鍥炲�煎嚱鏁�
Stringcall="javascript:readyToGo()";webView.loadUrl(call);
2. WebView璋冪敤JavScript鏈夊弬鏃犺繑鍥炲�煎嚱鏁�
Stringcall="javascript:alertMessage(\""+"content"+"\")";webView.loadUrl(call);
3. WebView璋冪敤JavaScript鏈夊弬鏁版湁杩斿洖鍊肩殑鍑芥暟
@TargetApi(Build.VERSION_CODES.KITKAT)privatevoidevaluateJavaScript(WebViewwebView){webView.evaluateJavascript("getYourCar()",newValueCallback(){@OverridepublicvoidonReceiveValue(Strings){Log.d("findCar",s);}});}
JavaScript閫氳繃WebView璋冪敤Java浠g爜
浠嶢PI19寮�濮嬶紝Android鎻愪緵浜咢JavascriptInterface瀵硅薄娉ㄨВ鐨勬柟寮忔潵寤虹珛璧稪avascript瀵硅薄鍜孉ndroid鍘熺敓瀵硅薄鐨勭粦瀹氾紝鎻愪緵缁橨avScript璋冪敤鐨勫嚱鏁板繀椤诲甫鏈堾JavascriptInterface銆�
婕旂ず涓� JavaScript璋冪敤Android Toast鏂规硶
1. 缂栧啓Java鍘熺敓鏂规硶骞剁敤浣跨敤@JavascriptInterface娉ㄨВ
@JavascriptInterfacepublicvoidshow(Strings){Toast.makeText(getApplication(),s,Toast.LENGTH_SHORT).show();}
2.娉ㄥ唽JavaScriptInterface
webView.addJavascriptInterface(this,"android");
addJavascriptInterface鐨勪綔鐢ㄦ槸鎶妕his鎵�浠h〃鐨勭被鏄犲皠涓篔avaScript涓殑android瀵硅薄銆�
3.缂栧啓JavaScript浠g爜
functiontoastClick(){window.android.show("JavaScript called~!");}
婕旂ず浜� JavaScript璋冪敤鏈夎繑鍥炲�肩殑Java鏂规硶
1.瀹氫箟涓�涓甫杩斿洖鍊肩殑Java鏂规硶锛屽苟浣跨敤@JavaInterface锛�
@JavaInterfacepublicStringgetMessage(){return"Hello,boy~";}
2.娣诲姞JavaScript鐨勬槧灏�
webView.addJavaScriptInterface(this,"Android");
3.閫氳繃JavaScript璋冪敤Java鏂规硶
functionshowHello(){varstr=window.Android.getMessage();console.log(str);}
浠ヤ笂灏辨槸Js涓嶹ebView浜や簰鐨勪竴浜涗粙缁嶏紝甯屾湜鑳藉浣犳湁甯姪銆�
WebView鍔犺浇浼樺寲
褰揥ebView鐨勪娇鐢ㄩ鐜囧彉寰楅绻佺殑鏃跺�欙紝瀵逛簬鍏跺悇鏂归潰鐨勪紭鍖栧氨鍙樺緱閫愭笎閲嶈浜嗚捣鏉ャ�傚彲浠ョ煡閬撶殑鏄紝鎴戜滑姣忓姞杞戒竴涓�
H5椤甸潰锛岄兘浼氭湁寰堝鐨勮姹傘�傞櫎浜咹TML涓籙RL鑷韩鐨勮姹傚锛孒TML澶栭儴寮曠敤鐨� JS銆丆SS銆佸瓧浣撴枃浠躲�佸浘鐗囬兘鏄竴涓釜鐙珛鐨凥TTP
璇锋眰锛岃櫧鐒惰姹傛槸骞跺彂鐨勶紝浣嗗綋缃戦〉鏁翠綋鏁伴噺杈惧埌涓�瀹氱▼搴︾殑鏃跺�欙紝鍐嶅姞涓婃祻瑙堝櫒瑙f瀽銆佹覆鏌撶殑鏃堕棿锛學eb鏁翠綋鐨勫姞杞芥椂闂村彉寰楀緢闀裤�傚悓鏃惰姹傛枃浠惰秺澶氾紝娑堣�楃殑娴侀噺涔熶細瓒婂銆傞偅涔堝浜庡姞杞界殑浼樺寲灏卞彉寰楅潪甯搁噸瑕侊紝杩欐柟闈㈢殑缁忛獙鎴戜篃娌℃湁浠�涔堝埆鐨勶紝澶ф涓変釜鏂归潰锛�
涓�涓紝灏辨槸璧勬簮鏈湴鍖栫殑闂
棣栧厛鍙互鏄庣‘鐨勬槸锛屼互鐩墠鐨勭綉缁滄潯浠讹紝閫氳繃缃戠粶鍘绘湇鍔″櫒鑾峰彇璧勬簮鐨勯�熷害鏄繙杩滄瘮涓嶄笂浠庢湰鍦拌鍙栫殑銆�璋堣鍚勭浼樺寲绛栫暐鍏跺疄鎭版伆蹇界暐浜嗏�滈渶瑕佸姞杞解�濇墠鏄樆鎸¢�熷害鎻愬崌鐨勬渶澶х粖鑴氱煶銆�鎵�浠ユ垜浠殑鎬濊矾涓�锛屽氨鏄皢涓�浜涜緝閲嶇殑璧勬簮姣斿js銆乧ss銆佸浘鐗囩敋鑷矵TML鏈韩杩涜鏈湴鍖栧鐞嗭紝鍦ㄦ瘡娆″姞杞藉埌杩欎簺璧勬簮鐨勬椂鍊欙紝浠庢湰鍦拌鍙栬繘琛屽姞杞斤紝鍙互绠�鍗曡蹇嗕负鈥滃瓨路鍙柭锋洿鈥�銆�
鍏蜂綋瀹炵幇鎬濊矾涓猴細
鈥滃瓨鈥�鈥斺�斿皢涓婅堪閲嶉噺绾ц祫婧愭墦鍖呰繘apk鏂囦欢锛屾瘡娆″姞杞界浉搴旀枃浠舵椂鏃朵粠鏈湴鍙栧嵆鍙�備篃鍙笉鎵撳寘锛屽湪绗竴娆″姞杞芥椂浠ュ強鎺ヤ笅鏉ョ殑鑻ュ共闂撮殧鏃堕棿閲屽姩鎬佷笅杞藉瓨鍌紝灏嗘墍鏈夌殑璧勬簮鏂囦欢閮藉瓨鍦ˋndroid鐨刟sset鐩綍涓嬶紱
鈥滃彇鈥�鈥斺�旈噸鍐橶ebViewClient鐨�WebResourceResponse聽 shouldInterceptRequest(WebView view, WebResourceRequest request)鏂规硶锛岄�氳繃涓�瀹氱殑鍒ゅ埆鏂规硶锛堜緥濡傛鍒欒〃杈惧紡锛夋嫤鎴浉搴旂殑璇锋眰锛屼粠鏈湴璇诲彇鐩稿簲璧勬簮骞惰繑鍥烇紱
鈥滄洿鈥�鈥斺�斿缓绔嬭捣Cache Control鏈哄埗锛屽畾鏈熸垨浣跨敤API閫氱煡鐨勫舰寮忔帶鍒舵湰鍦拌祫婧愮殑鏇存柊锛屼繚璇佹湰鍦拌祫婧愭槸鏈�鏂板拰鍙敤鐨勩��
杩欓噷闄勪笂涓�绡囧崥瀹㈤摼鎺ワ紝闈炲父妫掑彲渚涘弬鑰冿細caching-web-resources-in-the-android-device
绗簩涓紝灏辨槸缂撳瓨鐨勯棶棰�
鍊樿嫢浣犱笉閲囩敤鎴栦笉瀹屽叏閲囩敤绗竴鏉¤祫婧愭湰鍦板寲鐨勬�濊矾锛岄偅涔堜綘鐨刉ebView缂撳瓨鏄繀椤昏寮�鍚殑锛堣櫧鐒惰繖涓�鎬濊矾鍜岀涓�鏉℃湁閲嶅悎鐨勫湴鏂癸級銆�
WebSettingssettings=webView.getSettings();settings.setAppCacheEnabled(true);settings.setDatabaseEnabled(true);settings.setDomStorageEnabled(true);//寮�鍚疍OM缂撳瓨settings.setCacheMode(WebSettings.LOAD_DEFAULT);
鍦ㄧ綉缁滄甯告椂锛岄噰鐢ㄩ粯璁ょ紦瀛樼瓥鐣ワ紝鍦ㄧ紦瀛樺彲鑾峰彇骞朵笖娌℃湁杩囨湡鐨勬儏鍐典笅鍔犺浇缂撳瓨锛屽惁鍒欓�氳繃缃戠粶鑾峰彇璧勬簮浠ュ噺灏戦〉闈㈢殑缃戠粶璇锋眰娆℃暟銆�
杩欓噷鍊煎緱鎻愯捣鐨勬槸锛屾垜浠粡甯稿湪app閲岀敤WebView灞曠ず椤甸潰鏃讹紝骞朵笉鎯宠鐢ㄦ埛瑙夊緱浠栨槸鍦ㄨ闂竴涓綉椤点�傚洜涓哄�樿嫢鎴戜滑鐨刟pp閲岀綉椤甸潪甯稿锛岃�屾垜浠粰鐢ㄦ埛鐨勬劅瑙夊張閮藉儚鍦ㄨ闂綉椤电殑璇濓紝鎴戜滑鐨刟pp渚垮け鍘讳簡鎰忎箟銆傦紙鎴戠殑鎰忔�濇槸涓轰粈涔堢敤鎴蜂笉鐩存帴浣跨敤娴忚鍣ㄥ憿锛燂級
鎵�浠ヨ繖鏃讹紝绂荤嚎缂撳瓨鐨勯棶棰樺氨鍊煎緱鎴戜滑娉ㄦ剰銆傛垜浠渶瑕佽鐢ㄦ埛鍦ㄦ病鏈夌綉鐨勬椂鍊欙紝渚濈劧鑳藉鎿嶄綔鎴戜滑鐨刟pp锛岃�屼笉鏄潰瀵逛竴涓拰娴忚鍣ㄩ噷鐨勭綉缁滈敊璇竴鏍风殑椤甸潰锛屽摢鎬曚粬鑳借繘琛岀殑鎿嶄綔鍗佸垎鏈夐檺銆�
杩欓噷鎴戠殑鎬濊矾鏄紝鍦ㄥ紑鍚紦瀛樼殑鍓嶆彁涓嬶紝WebView鍦ㄥ姞杞介〉闈㈡椂妫�娴嬬綉缁滃彉鍖栵紝鍊樿嫢鍦ㄥ姞杞介〉闈㈡椂鐢ㄦ埛鐨勭綉缁滅獊鐒舵柇鎺夛紝鎴戜滑搴斿綋鏇存敼WebView鐨勭紦瀛樼瓥鐣ャ��
ConnectivityManagerconnectivityManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfonetworkInfo=connectivityManager.getActiveNetworkInfo();if(networkInfo.isAvailable()){settings.setCacheMode(WebSettings.LOAD_DEFAULT);//缃戠粶姝e父鏃朵娇鐢ㄩ粯璁ょ紦瀛樼瓥鐣else{settings.setCacheMode(WebSettings.LOAD_CACHE_ONLY);//缃戠粶涓嶅彲鐢ㄦ椂鍙娇鐢ㄧ紦瀛榼
鏃㈢劧鏈夌紦瀛橈紝灏辫鏈夌紦瀛樻帶鍒讹紝涓庝竴鐩镐技鐨勬槸鎴戜滑涔熻寤虹珛缂撳瓨鎺у埗鏈哄埗锛屽畾鏈熸垨鎺ュ彈鏈嶅姟鍣ㄩ�氱煡鏉ヨ繘琛岀紦瀛樼殑娓呯┖鎴栨洿鏂般��
绗笁涓紝灏辨槸寤惰繜鍔犺浇鍜屾墽琛宩s
鍦╓ebView涓紝onPageFinished()鐨勫洖璋冩剰鍛崇潃椤甸潰鍔犺浇鐨勫畬鎴愩�備絾璇ユ柟娉曚細鍦↗avScript鑴氭湰鎵ц瀹屾垚鍚庢墠浼氳Е鍙戯紝鍊樿嫢鎴戜滑瑕佸姞杞界殑椤甸潰浣跨敤浜咼Query锛屼細鍦ㄥ鐞嗗畬DOM瀵硅薄锛屾墽琛屽畬$(document).ready(function()
{})鍚庢墠浼氭覆鏌撳苟鏄剧ず椤甸潰銆傝繖鏄笉鍙帴鍙楃殑锛屾墍浠ユ垜浠渶瑕佸Js杩涜寤惰繜鍔犺浇锛屽綋鐒惰繖閮ㄥ垎鏄疻eb鍓嶇鐨勫伐浣溿��
濡傛灉璇磋繕鏈変粈涔�
閭e氨鏄疛sBridge涓�寰嬩笉寰楁互鐢紝杩欎釜瀵归〉闈㈠姞杞界殑瀹屾垚閫熷害鏄湁寰堝ぇ褰卞搷鐨勶紝鍊樿嫢涓�涓〉闈㈠緢澶氭搷浣滈兘閫氳繃JSbridge鏉ユ帶鍒讹紝鍐嶆�庝箞浼樺寲涔熸棤娴庝簬浜嬶紙鍥犱负姣曠珶鏈夐偅涔堝鎿嶄綔瑕佸疄闄呮墽琛岋級銆傚悓鏃惰娉ㄦ剰鐨勬槸锛屼笉绠′綘鏄惁瀵硅祫婧愯繘琛岀紦瀛橈紝閮借灏嗚祫婧愬湪鏈嶅姟鍣ㄧ杩涜鍘嬬缉銆傚洜涓烘棤璁烘槸璧勬簮鐨勮幏鍙栧拰鏇存柊锛岄兘鏄浠庢湇鍔″櫒鑾峰彇鐨勶紝鎵�浠ュ浜庤祫婧愭枃浠剁殑鍘嬬缉鍏跺疄鏄�鏈�鐩存帴涔熸渶搴旇鍋�鐨勪簨鎯呬箣涓�锛屼絾鏄竴鑸湇鍔″櫒绔兘浼氬仛濂斤紝鎵�浠ヤ富瑕佸氨鏄笂闈㈣繖涓変欢浜嬨��
椹剧収鑰冭瘯
浠嬬粛浜嗚繖涔堝锛屽笇鏈涜兘瀵逛綘鏈夌偣甯姪銆傛帴涓嬫潵鏃剁函瀹炴垬鏃堕棿锛屾垜浼氬皢涓婇潰鎵�浠嬬粛鐨勫緢澶氱煡璇嗙偣鍦ㄦ帴涓嬫潵鐨勪唬鐮侀噷瀹為檯搴旂敤涓�閬嶏紝甯屾湜鑳藉甯︾粰浣犳洿鍔犵洿瑙傜殑浣跨敤鎰熷彈銆�
Java閮ㄥ垎
publicclassMainActivityextendsAppCompatActivity{privateWebViewmWebView;privateProgressBarmProgressbar;privateToolbarmToolbar;@OverrideprotectedvoidonCreate(BundlesavedInstanceState){supportRequestWindowFeature(Window.FEATURE_NO_TITLE);super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initAppBar();//鍒濆鍖朤oolbarinitWebView();//鍒濆鍖朩ebViewinitWebSettings();//鍒濆鍖朩ebSettingsinitWebViewClient();//鍒濆鍖朩ebViewClientinitWebChromeClient();//鍒濆鍖朩ebChromeClient}privatevoidinitAppBar(){mToolbar=(Toolbar)findViewById(R.id.toolbar);mToolbar.setTitle("杞藉叆涓�..");mToolbar.setTitleTextColor(getResources().getColor(R.color.colorWhite));setSupportActionBar(mToolbar);getSupportActionBar().setDisplayHomeAsUpEnabled(false);}privatevoidinitWebView(){mWebView=(WebView)findViewById(R.id.web_view);mProgressbar=(ProgressBar)findViewById(R.id.progress_bar);Stringurl="https://www.google.com";mWebView.loadUrl(url);}privatevoidinitWebSettings(){WebSettingssettings=mWebView.getSettings();//鏀寔鑾峰彇鎵嬪娍鐒︾偣mWebView.requestFocusFromTouch();//鏀寔JSsettings.setJavaScriptEnabled(true);//鏀寔鎻掍欢settings.setPluginState(WebSettings.PluginState.ON);//璁剧疆閫傚簲灞忓箷settings.setUseWideViewPort(true);settings.setLoadWithOverviewMode(true);//鏀寔缂╂斁settings.setSupportZoom(false);//闅愯棌鍘熺敓鐨勭缉鏀炬帶浠秙ettings.setDisplayZoomControls(false);//鏀寔鍐呭閲嶆柊甯冨眬settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);settings.supportMultipleWindows();settings.setSupportMultipleWindows(true);//璁剧疆缂撳瓨妯″紡settings.setDomStorageEnabled(true);settings.setDatabaseEnabled(true);settings.setCacheMode(WebSettings.LOAD_DEFAULT);settings.setAppCacheEnabled(true);settings.setAppCachePath(mWebView.getContext().getCacheDir().getAbsolutePath());//璁剧疆鍙闂枃浠秙ettings.setAllowFileAccess(true);//褰搘ebview璋冪敤requestFocus鏃朵负webview璁剧疆鑺傜偣settings.setNeedInitialFocus(true);//鏀寔鑷姩鍔犺浇鍥剧墖if(Build.VERSION.SDK_INT>=19){settings.setLoadsImagesAutomatically(true);}else{settings.setLoadsImagesAutomatically(false);}settings.setNeedInitialFocus(true);//璁剧疆缂栫爜鏍煎紡settings.setDefaultTextEncodingName("UTF-8");}privatevoidinitWebViewClient(){mWebView.setWebViewClient(newWebViewClient(){//椤甸潰寮�濮嬪姞杞芥椂@OverridepublicvoidonPageStarted(WebViewview,Stringurl,Bitmapfavicon){super.onPageStarted(view,url,favicon);mProgressbar.setVisibility(View.VISIBLE);}//椤甸潰瀹屾垚鍔犺浇鏃禓OverridepublicvoidonPageFinished(WebViewview,Stringurl){super.onPageFinished(view,url);mProgressbar.setVisibility(View.GONE);}//鏄惁鍦╓ebView鍐呭姞杞芥柊椤甸潰@OverridepublicbooleanshouldOverrideUrlLoading(WebViewview,WebResourceRequestrequest){view.loadUrl(request.toString());returntrue;}//缃戠粶閿欒鏃跺洖璋冪殑鏂规硶@OverridepublicvoidonReceivedError(WebViewview,WebResourceRequestrequest,WebResourceErrorerror){super.onReceivedError(view,request,error);/**
聽 聽 聽 聽 聽 聽 聽 * 鍦ㄨ繖閲屽啓缃戠粶閿欒鏃剁殑閫昏緫,姣斿鏄剧ず涓�涓敊璇〉闈�
聽 聽 聽 聽 聽 聽 聽 *
聽 聽 聽 聽 聽 聽 聽 * 杩欓噷鎴戝伔涓噿涓嶅啓浜�
聽 聽 聽 聽 聽 聽 聽 * */}@TargetApi(Build.VERSION_CODES.M)@OverridepublicvoidonReceivedHttpError(WebViewview,WebResourceRequestrequest,WebResourceResponseerrorResponse){super.onReceivedHttpError(view,request,errorResponse);}});}privatevoidinitWebChromeClient(){mWebView.setWebChromeClient(newWebChromeClient(){privateBitmapmDefaultVideoPoster;//榛樿鐨勮棰戝睍绀哄浘@OverridepublicvoidonReceivedTitle(WebViewview,Stringtitle){super.onReceivedTitle(view,title);setToolbarTitle(title);}@OverridepublicBitmapgetDefaultVideoPoster(){if(mDefaultVideoPoster==null){mDefaultVideoPoster=BitmapFactory.decodeResource(getResources(),R.drawable.video_default);returnmDefaultVideoPoster;}returnsuper.getDefaultVideoPoster();}});}/**
聽 * 璁剧疆Toolbar鏍囬
聽 *
聽 * @param title
聽 */privatevoidsetToolbarTitle(finalStringtitle){Log.d("setToolbarTitle"," WebDetailActivity "+title);if(mToolbar!=null){mToolbar.post(newRunnable(){@Overridepublicvoidrun(){mToolbar.setTitle(TextUtils.isEmpty(title)?getString(R.string.loading):title);}});}}@OverridepublicbooleanonCreateOptionsMenu(Menumenu){getMenuInflater().inflate(R.menu.menu_main,menu);returnsuper.onCreateOptionsMenu(menu);}@OverridepublicbooleanonOptionsItemSelected(MenuItemitem){switch(item.getItemId()){caseR.id.page_up:Toast.makeText(getApplicationContext(),"椤甸潰鍚戜笂",Toast.LENGTH_SHORT).show();mWebView.pageUp(true);break;caseR.id.page_down:Toast.makeText(getApplicationContext(),"椤甸潰鍚戜笅",Toast.LENGTH_SHORT).show();mWebView.pageDown(true);break;caseR.id.refresh:Toast.makeText(getApplicationContext(),"鍒锋柊~",Toast.LENGTH_SHORT).show();mWebView.reload();default:returnsuper.onOptionsItemSelected(item);}returnsuper.onOptionsItemSelected(item);}@OverridepublicbooleanonKeyDown(intkeyCode,KeyEventevent){//濡傛灉鎸変笅鐨勬槸鍥為��閿笖鍘嗗彶璁板綍閲岀‘瀹炶繕鏈夐〉闈f((keyCode==KeyEvent.KEYCODE_BACK)&&mWebView.canGoBack()){mWebView.goBack();returntrue;}else{Toast.makeText(getApplicationContext(),"鑰冭瘯缁撴潫,鎭枩鎮ㄨ�冭瘯鍚堟牸!",Toast.LENGTH_LONG).show();}returnsuper.onKeyDown(keyCode,event);}}
鏁堟灉鍥撅細
fuck锛屾晥鏋滃浘澶ぇ鍘嬬缉鏃犳湜~~~鏁堟灉鍥剧湅澶栭摼瑙嗛鍚�
涓婅矾
濂戒簡锛屽埌姝や负姝綘宸查槄璇诲畬鎵�鏈夌殑鎸囧崡锛岄�佷綘涓�杈嗚溅锛屼笂璺惂銆�
鍙傝�冩枃绔�
Android瀹樻柟鏂囨。
Android Web Apps Using Android WebView
鍙蹭笂鏈�鍏╳ebview璇﹁В
H5 缂撳瓨鏈哄埗娴呮瀽 绉诲姩绔� Web 鍔犺浇鎬ц兘浼樺寲
WebView鍔犺浇閫熷害浼樺寲