Objective:
Today, I modified a project on the use of WebView bug, aroused my summary of webview motivation, today to make a summary.
Usage scenarios:
1.) Add permissions
<uses-permission android:name= "Android.permission.INTERNET"/>
2.) Layout file
<webview
android:id= "@+id/webview"
android:layout_width= "Match_parent"
Match_parent "/>
3.) Data loading
Loading local Resources
Webview.loadurl ("file:///android_asset/example.html");
Load network resources
Webview.loadurl ("www.xxx.com/index.html");
Add Request header Information
Map<string,string> map=new hashmap<string,string> (); Map.put ("User-agent", "Android");
Webview.loadurl ("www.xxx.com/index.html", map);
You can also load HTML fragments
String data = "Html data";
Webview.loaddata (data, "text/html", "utf-8");
Test will find that LoadData will cause Chinese garbled, so the general situation using the following code
String data = "Html data";
Webview.loaddatawithbaseurl (Null,data, "text/html", "Utf-8", null);
4.) Support JavaScript
For example, project Total JS triggers a native function to close the activity
Setting support for JavaScript
WebSettings websettings = Webview.getsettings ();
Websettings.setjavascriptenabled (true);/setting supports JavaScript
webview.addjavascriptinterface (new Javascriptinterface (), "Xueleapp"); The Javascriptinterface interface defines the public
class Javascriptinterface {
@android. Webkit.javascriptinterface public
void Dotrainfinish () {
finish ();
}
}
5.) Set Webviewclient main auxiliary webview handle various notices, request events
For example, to implement WebView link in the WebView internal jump
Webview.setwebviewclient (New Webviewclient () {public
Boolean shouldoverrideurlloading (webview view, String URL) {
view.loadurl (URL);
return true;
}
});
In addition to webviewclient more processing of web address resolution and rendering, such as
onloadresource//Response when loading resources
onpagestart//Response when loading page
onpagefinish//response at end of load page
onreceiveerror//response in case of load error
onreceivedhttpauthrequest//obtain return Information authorization request
6.) Set Webchromeclient main auxiliary webview Processing JavaScript dialog box, site icon, site title, loading progress, etc.
such as load progress get title
Webview.setwebchromeclient (New Webchromeclient () {
@Override public
void onprogresschanged (webview view, int newprogress) {
if (newprogress = =) {
//web page load complete
} else {
//page load
}}}
);
In addition to the test progress above, there are
onclosewindow//off WebView
Oncreatewindow ()//trigger to create a new window
Onjsalert//Triggers a pop-up dialog box
Onjsprompt//trigger pops up a hint
onjsconfirm//Trigger Pop-up confirmation prompt
Onprogresschanged//Loading progress
Onreceivedicon//Get web icon
onreceivedtitle//Get page title
7.) Set the page stack back
WebView will default to the browsing past of the Web page for the stack storage, so we sometimes need to implement a fallback to the previous directory
@Override Public
boolean onKeyDown (int keycode, keyevent event) {
if (keycode = = keyevent.keycode_back) {
if (Webview.cangoback ()) {
webview.goback ()//Returns the previous Browse page return
true;
} else {
finish ();//close Activity
}
}
Super.onkeydown (KeyCode, event);
}
8.) WebView Cache Control
load_cache_only: Do not use the network, read only local cache data
Load_default: Depending on the Cache-control, decide whether to fetch data from the network.
Load_cache_normal:api level 17 has been deprecated, starting with API level 11 and Load_default mode
Load_no_cache: Do not use caching, only get data from the network.
Load_cache_else_network, the data in the cache is used as long as it is locally available, regardless of whether it expires or no-cache.
WebSettings websettings = Webview.getsettings ();
Websettings.setcachemode (websettings.load_cache_else_network);
9.) WebView Screen Adaptive
WebSettings websettings = Webview.getsettings ();
Websettings.setusewideviewport (true);
Websettings.setloadwithoverviewmode (TRUE);
10.) Other less common settings
WebSettings websettings = Webview.getsettings ();
Websettings.setsupportzoom (TRUE); Supports scaling
websettings.setlayoutalgorithm (WebSettings.LayoutAlgorithm.SINGLE_COLUMN);//support content re-layout
Websettings.supportmultiplewindows (); Multi-Window
websettings.setallowfileaccess (TRUE);//set access to file
Websettings.setneedinitialfocus (TRUE); Set node Websettings.setbuiltinzoomcontrols (true) for WebView when WebView calls Requestfocus
;//Set support for scaling
Websettings.setjavascriptcanopenwindowsautomatically (TRUE); Support to open a new window via JS
websettings.setloadsimagesautomatically (TRUE);//support automatic loading of pictures
11.) Knowledge Expansion Webviewjsbridge
Although Google also provides the way JS and native functions call each other, but by addjavascriptinterface this way there are some security risks in Android version 4.2, in Android 4.2 Above also need to add @javascriptinterface annotation, otherwise cannot call. Based on the above reasons to suggest learning
Webviewjsbridge This relatively good open source framework, address: Https://github.com/firewolf-ljw/WebViewJSBridge
12.) Hardware acceleration
Turn on hardware to accelerate the forced use of GPU rendering, it does bring a small increase in the flow of the app, but in the process of meeting webview flashing, but also caused the load webview black screen or white screen
Workaround: Turn off hardware acceleration
Webview.setlayertype (view.layer_type_software, NULL);
This is to turn the hardware in the webview to an accelerated shutdown. Setting Layer_type_software will convert the current view to bitmap save. This can not open multiple webview, or will report out of the memory.
You need to add the following code to the WebView
protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {
invalidate ();
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
}
The above is a small set to introduce the Android program development WebView Use summary, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!