Android WebView Use

Source: Internet
Author: User

In recent work, basically has been using WebView, today to tidy it up:

WebView as the name implies, is to put a Web page, a look is very simple, but it is not so simple to use the control.

First you definitely want to define, initialize a webview, in fact there are many examples on the net, I here simply put some webview The most important properties that may be used, and the code that supports the full-screen playback video. Stick it out and put it right in the project.

Private WebView WebView;

WebView = (webview) Findviewbyid (R.id.webview);

set the WebView property to be able to execute Javascript Scripts

Webview.getsettings (). Setjavascriptenabled (True);

set WebView to load more format pages

Webview.getsettings (). Setloadwithoverviewmode (True);

set WebView to use a wide range of Windows

Webview.getsettings (). Setusewideviewport (True);

sets The user agent string for the WebView. If the string "ua" is null or empty , It will use the system default User-agent string

Webview.getsettings (). setuseragentstring ();

Support gesture Scaling

Webview.getsettings (). Setbuiltinzoomcontrols (True);

support for all versions above 2.2

Webview.getsettings (). Setpluginstate (Pluginstate.on);

tells WebView to enable the application cache API.

Webview.getsettings (). Setappcacheenabled (True);

sets whether the DOM storage APIis enabled.

Webview.getsettings (). Setdomstorageenabled (True);

automatically open windows

Webview.getsettings (). Setjavascriptcanopenwindowsautomatically (True);

No, it will be black screen support plugin

Webview.getsettings (). Setpluginsenabled (True);

/**

* Setallowfileaccess Enables or disables WebView access to file data setblocknetworkimage Whether the network image is displayed

* Setbuiltinzoomcontrols sets whether to support scaling setcachemode setting buffering mode

* setdefaultfontsize Set default font size setdefaulttextencodingname Set the default encoding to use when decoding

* setfixedfontfamily Sets whether the fixed-use font setjavasciptenabled setting supports Javascript

* Setlayoutalgorithm Set layout mode setlighttouchenabled settings with mouse activation is selected

* Setsupportzoom Set whether zoom is supported

* */

Typesetting adaptation Screen

Webview.setlayoutalgorithm (WebSettings.LayoutAlgorithm.NARROW_COLUMNS);

setting up The Web It is important to note the following two methods, many of which need to be rewritten:

Set a custom webchromeclient to set up some problems with video playback first

Buswifiwebview.setwebchromeclient (New Defaultwebchromeclient ());

Buswifiwebview.setwebviewclient (New Defaultwebviewclientclient ());

The first method:webchromeclient:

to override the Onshowcustomview method to indicate a full screen entry, and Onhidecustomview to exit the full screen

Interface words, is a webview, a framelayout, When the full screen is set webview hide, let Framelayout Full screen display, then the video will automatically run to framelayout this inside put. Exit full screen, the same reason, here to use the callback function, you understand it ... I don't speak well ...

Private class Defaultwebchromeclient extends Webchromeclient {

a callback interface uses a host application that notifies the current page that a custom view has been dismissed

Customviewcallback Customviewcallback;

when you go to full screen

@Override

public void Onshowcustomview (view view, Customviewcallback callback) {

assign a value to callback

Customviewcallback = callback;

set webView Hide

Webview.setvisibility (View.gone);

declare video and put it in there.

Framelayout video = (framelayout) Findviewbyid (R.id.video);

put video in the current view

Video.addview (view);

Horizontal Screen Display

Setrequestedorientation (Activityinfo.screen_orientation_landscape);

Set Full Screen

Setfullscreen ();

}

when you exit the full screen

@Override

public void Onhidecustomview () {

if (customviewcallback! = null) {

Hidden away

Customviewcallback.oncustomviewhidden ();

}

user's current preferred orientation

Setrequestedorientation (Activityinfo.screen_orientation_user);

Exit Full Screen

Quitfullscreen ();

set WebView visible

Webview.setvisibility (view.visible);

}

@Override

public void onprogresschanged (WebView view, int newprogress) {

Super.onprogresschanged (view, newprogress);

}

}

Here are two ways to set up full screen and exit full screen: Notice that I have captured the Flags in full-screen state and in the window state respectively . The following code is visible

/**

* Set Full screen

*/

private void Setfullscreen () {

set the relevant properties of the full screen, get the current screen state, and then set the full screen

GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN);

status code in full screen:1098974464

the State under the window:1098973440

}

/**

* exit Full Screen

*/

private void Quitfullscreen () {

declares parameters for the current screen state and gets

Final Windowmanager.layoutparams attrs = GetWindow (). GetAttributes ();

Attrs.flags &= (~windowmanager.layoutparams.flag_fullscreen);

GetWindow (). SetAttributes (Attrs);

GetWindow (). Clearflags (WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);

}

The second custom webviewclient, inherited webviewclient

/**

* Use system default WebView

*/

Private class Defaultwebviewclientclient extends Webviewclient {

@Override

public boolean shouldoverrideurlloading (WebView view, String URL) {

Return super.shouldoverrideurlloading (view, URL);

if you want to download the game on the page or continue to click on the link in the page to go to the next page, override this method, or you will jump to the mobile phone's own browser, and not continue to show in your webview

}

@Override

public void Onreceivederror (WebView view, int errorCode,

String description, String failingurl) {

If you want to get the error message, take some action, go this way

}

@Override

public void onpagestarted (WebView view, String URL, Bitmap favicon) {

super.onpagstarted (view, URL, favicon);

If you want to start loading the page, do some things, go this way

}

@Override

public void onpagefinished (WebView view, String URL) {

super.onpagefinished (view, URL);

If you want to perform some action at the end of the page load, go this way

}

}

Finally, it is important to go to the same webview to Browse the Web, and the point return key is to return in the webview , rather than directly exit the program, then you have to rewrite OnKeyDown method. also have

@Override

public boolean onKeyDown (int keycode, keyevent event) {

if (keycode = = Keyevent.keycode_back) {

WebView WebView = (WebView) Findviewbyid (R.id.webview_passenger);

if (Webview.cangoback ()) {

GoBack () indicates that the previous page of the WebView is returned

Webview.goback ();

Exit Full Screen

Quitfullscreen ();

} else {

}

return true;

}

Return Super.onkeydown (KeyCode, event);

Android WebView Use

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.