Development and use of Android WebView (source sharing)

Source: Internet
Author: User

Suppose we want to provide a Web application (or just a Web page) as part of a client application, we can use WebView. The WebView class is an extension of the Android view class, which agrees to show you a portion of the active layout of a Web page.

It does not contain any features of a fully developed web browser, such as navigation controls or an address bar. All WebView, by default, displays a Web page.

WebView What is the application scenario, we can use WebView to display some information, such as you may need to update, such as an end user agreement or user Guide. In your Android app, you are able to create a acitivity including WebView. Then use to display the online documents you host.

There is also a scenario in which WebView can help assume that your application provides data to the user, always requiring a network connection to retrieve data, such as e-mail. In such cases, you may find that creating a WebView in an Android application displays a Web page displaying all the user data instead of running a network request, then parsing the data and presenting it to an Android layout that is now easy.

Calls to WebView have procedures such as those seen below

(1)

Add the WebView control to your application by simply including the layout of the < WebView > elements in the activity.

<?xml version= "1.0" encoding= "Utf-8"? ><webview  xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:id=" @+id/webview "    android:layout_width=" fill_parent "    android:layout_height=" Fill_ Parent "/>

(2)

Use Loadurl () to load a webview.

WebView Mywebview = (WebView) Findviewbyid (R.id.webview); Mywebview.loadurl ("http://www.example.com");

(3)

Join a network license in a manifest file

<manifest >    <uses-permission android:name= "Android.permission.INTERNET"/>    ... </ Manifest>
About JavaScript
Assuming you plan to use JavaScript on your load Web page WebView, you must enable JavaScript WebView. Once JavaScript is enabled, you can also create an interface between your application code and JavaScript code.

JavaScript is disabled by default in WebView. You can make it by attaching websettings to him. You can retrieve the GetSettings websettings () and then use setjavascriptenabled to enable JavaScript ().

WebView Mywebview = (WebView) Findviewbyid (R.id.webview); WebSettings websettings = Mywebview.getsettings (); websettings.setjavascriptenabled (true);

About historical return

When your WebView rewrite URL is loaded, it will proactively accumulate its own historical access to the Web page. You can navigate backwards and forwards to historical GoBack () and GoForward ().

@Overridepublic boolean onKeyDown (int keycode, keyevent event) {    //Check If the key event is the back button and if There ' s history    if ((keycode = = keyevent.keycode_back) && mywebview.cangoback ()) {        mywebview.goback () ;        return true;    }    If It wasn ' t the back key or there's no Web page history, bubble up to the default    //system behavior (probably Exi t the activity)    return Super.onkeydown (KeyCode, event);}



Development and use of Android WebView (source sharing)

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.