Android Growth Diary-webview use

Source: Internet
Author: User

In the app sometimes see some pages are displayed in the form of web pages, the principle is to use the WebView, the following to tell WebView

1. Use intent to invoke the system browser or third-party browser to open the Web page

Call the System browser to open the page

Uri uri=uri.parse (URL); URL is the address of the Web page

Intent intent=new Intent (Intent.action_view,uri);

StartActivity (Intent);

2. Open the page with WebView

Step One: Create a new WebView control in the layout folder

Step one: When using WebView, you need to first overwrite the system browser or a third-party browser to open the Web page

WebView Loading Web Resources

Webview.loadurl (URL);

/*

* Overwrite WebView By default the behavior of opening a webpage through a third party or system browser, so that the Web page can be opened in WebView

*/

Webview.setwebviewclient (new webviewclient () {

Webviewclient help WebView to handle some control and request notifications

@Override

Public boolean shouldoverrideurlloading (WebView view, String URL) {

The return value is true when the control Web page is opened in WebView, and false is opened with a system browser or a third-party browser

View.loadurl (URL);

return true;

}

});

Step Three: Whether to enable JAVASCRIPT[S1]

Enable JavaScript

WebSettings settings=webview.getsettings ();

Settings.setjavascriptenabled (true);

Step Four: Return to previous page by the return key of the physical key

Change the physical keys--return logic

@Override

Public boolean onKeyDown (int keycode, keyevent event) {

if (Keycode==keyevent. keycode_back)

{

Webview.goback ();

return true;

}

Else

{

System. Exit (0);//Exit Program

return false;

}

Step Five: In order to increase the interactivity of the page, you can add the progress bar display

Webview.setwebchromeclient (new webchromeclient () {

@Override

Public void onprogresschanged (WebView view, int newprogress[s2]) {

if (newprogress==100)

{

Page loading complete, close ProgressDialog

Closedialog ();

}

Else

{

Web page is loading, opening ProgressDialog

OpenDialog (newprogress);

}

}

Private void Closedialog () {

if (dialog!=null&&dialog.isshowing ())

{

Dialog.dismiss ();//Disappear Dialog dialog box

dialog=null;

}

}

Private void opendialog (int newprogress) {

if (dialog==null)

{

dialog=New progressdialog (mainactivity. this);

Dialog.settitle ("Loading");

Dialog.setprogressstyle (ProgressDialog. Style_horizontal);

Dialog.setprogress (newprogress);//Show Progress

Dialog.show ();

}

Else

{

Dialog.setprogress (newprogress);

}

}

});

}

[S1] In order to improve the speed of the page, you can first use the Cache load page

WebView loading page takes precedence with cache loading

Settings.setcachemode (websettings. Load_cache_else_network);

[s2]newprogress: Displays the page's Load progress,<int> type

Android Growth Diary-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.