WebView Usage Summary

Source: Internet
Author: User

The license "Android.permission.INTERNET" must be used in 1.androidmanifest.xml, otherwise the WEB page not available error will occur.

2. If there is javasrcipt on the page visited, the WebView must be set to support JavaScript

WebSettings websetting = Webview.getsettings ();
Websetting.setjavascriptenabled (TRUE);


3. If the page is connected, if you want the click Connection to continue in the current browser, instead of the new Android system browser should be connected, you must overwrite the WebView Webviewclient object.


Mwebview.setwebviewclient (New Webviewclient () {

public boolean shouldoverrideurlloading (WebView view,string URL) {
View.loadurl (URL);
return true;
}
})

4. If you do not do any processing, browse the Web page, click the System "back" button, the entire browser will call finish () and end itself, if you want to browse the page fallback rather than launch the browser, you need to process and consume the back event in the current activity.


public boolean onKeyDown (int keycode, keyevent event) {
if ((keycode = = keyevent.keycode_back) && mwebview.cangoback ()) {
Mwebview.goback ();
return true;
}

Return Super.onkeydown (KeyCode, event);
}

5. If the user is required to manually enter a user name, password, or other webview, the WebView must be set to support the get gesture focus.

Webview.requestfocusfromtouch ();

6.WebView loading interface mainly calls three methods: Loadurl, LoadData, Loaddatawithbaseurl
1.LOADURL load the page directly, picture and display

2.LoadData display text and picture content

3.LoadDataWithBase display text and picture content


Common methods of WebSettings

Setjavascriptenabled (TRUE);
Support JS Script
Setpluginsenabled (TRUE);
Support Plugins
Setuserwideviewport (FALSE);
Adjust the picture to fit the size of the WebView
Setsupportzoom (TRUE);
Support Scaling
Setlayoutalgorithm (Layoutalgrithm.single_column);
Support Content from new layout
Supportmultiplewindows ();
Multi-window
Setcachemode (websettings.load_cache_else_network);
Close Cache in WebView
Setallowfileaccess (TRUE);
Settings can access files
Setneedinitialfocus (TRUE);
Set node for WebView when WebView calls Requestfocus
Setjavascriptcanopenwindowsautomatically (TRUE);
Support to open a new window via JS
Setloadsimagesautomatically (TRUE);
Supports automatic loading of pictures

Setbuiltinzoomcontrols (TRUE);

Support Scaling

Webview.setinitialscale (35);

Set the zoom ratio

Webview.setscrollbarstyle (View.scrollbars_outside_overlay);

Set scroll bar Hide

Webview.getsettings (). Setgeolocationenabled (True);

Enable Geo-targeting

Webview.getsettings (). setrenderpriority (Renderpriority.high);

Set the rendering priority level

String dir = "/sdcard/temp";
Set the location of the database path
Webview.getsettings (). Setgeolocationdatabasepath (dir); Here are the settings for using WebView and WebView, and the lessons are set according to the actual situation.

The whole solution of the method of Webviewclient

Doupdatevisitedhistory (WebView view, String URL, boolean isreload)
Update history
Onformresubmission (WebView view, message dontresend, message resend)
Application re-requesting page data
Onloadresource (WebView view, String URL)
Called when a page resource is loaded, and each resource (a slice) is loaded once
Onpagestarted (WebView view, String URL, Bitmap favicon)
This event is to start loading the page call, usually we can set a loading in this page, tell the user program is waiting for the network corresponding.
Onpagefinished (WebView view, String URL)
Called at the end of the page load, the same thing, we know a page loading is complete, so we can close the loading bar, switch program action.
Onreceivederror (WebView view, int errorCode, string description, String failingurl)
Reporting error messages
Onreceivedhttpauthrequest (WebView view, Httpauthhandler handler, String host,stirng Realm)
Get Return Information Authorization request
Onscalechanged (WebView view, float Oldscale, float newscale)
WebView called when a change occurs

Onunhandledkeyevent (WebView view, KeyEvent event)
Called when the key event is not loaded

Shouldoverrideurlloading is not always called before onpagestarted, meaning that a new URL is not shouldoverrideurlloading every time. It is called only when the Webview.loadurl is called.

Set to open a new page in the same webview
Webview.setwebviewclient (New Webviewclient () {
public boolean shouldoverrideurlloading (WebView view, String URL) {
View.loadurl (URL);
return true;
}
});

Set Webchromeclient
Webview.setwebchromeclient (New Webchromeclient () {
Configure permissions
public void Ongeolocationpermissionsshowprompt (String origin,geolocationpermissions.callback Callback) {
Callback.invoke (origin, True, false);
Super.ongeolocationpermissionsshowprompt (origin, callback);
}

Handling Alert in JavaScript
public boolean Onjsalert (WebView view, string URL, String message, final Jsresult result) {
Build a builder to display a dialog box in a Web page
Builder builder = new Builder (mainacitivity.this);
Builder.settitle ("hint");
Builder.setmessage (message);
Builder.setpositivebutton (Android. R.string.ok,
New Alertdialog.onclicklistener () {
public void OnClick (Dialoginterface dialog, int which) {
Result.confirm ();
}
});

Builder.setcancelable (FALSE);
Builder.create ();
Builder.show ();
return true;

@Override
Set the progress bar for page loading
public void onprogresschanged (WebView view, int newprogress) {
MainAcitivity.this.getWindow (). Setfeatureint (window.feature_progress, newprogress * 100);
Super.onprogresschanged (view, newprogress);
}//setting the title of the application title
public void Onreceivedtitle (WebView view, String title) {
MainAcitivity.this.setTitle (title);
Super.onreceivedtitle (view, title);
}
});

Deciding whether to connect
Connectivitymanager cm;
CM = (Connectivitymanager) getsystemservice (Context.connectivity_service);
Boolean Iswificonnected=cm.getnetworkinfo (Connectivitymanager.type_wifi). GetState () = = NetworkInfo.State.CONNECTED;
if (!iswificonnected) {
Boolean Isgprsconnected=cm.getnetworkinfo (Connectivitymanager.type_mobile). GetState () = = NetworkInfo.State.CONNECTED;
if (!isgprsconnected) {
New Alertdialog.builder (This)
. Settitle ("hint")
. Setmessage ("Offline status, enable Offline mode")
. Setpositivebutton ("OK", NULL)
. Show ();
}
else{
result = CALLWCF ();
}
}

Reference Link: http://hi.baidu.com/mowangsheng/item/3a0921295ffbee0276272c5e

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.