WebView 寮�杞︽寚鍗椾箣鏈�鍏ㄥ疄鐢ㄦ渚�

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鍔犺浇閫熷害浼樺寲

你可能感兴趣的:(WebView 寮�杞︽寚鍗椾箣鏈�鍏ㄥ疄鐢ㄦ渚�)