Development and use of Android development WebView (source code sharing)

Source: Internet
Author: User

If we want to provide a Web application (or just a Web page) as part of the client application, we can use WebView. The WebView class is an extension of the Android view class, which allows you to display a portion of the active layout of a Web page. It does not include any of the 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 can create a acitivity containing WebView. Then use to display the online documents you host. Another scenario, in which WebView can help if your application provides data to the user, always requires a network connection to retrieve data, such as e-mail. In this case, you may find that creating a WebView in an Android application displays a Web page that displays all user data instead of performing a network request and then parsing the data and rendering it in an Android layout to be easy. The steps to call WebView are as follows

(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)

Add a network authorization to a manifest file

<manifest >    <uses-permission android:name= "Android.permission.INTERNET"/>    ... </ Manifest>
About JavaScript
If 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 achieve 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 automatically accumulate 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 development WebView (source code 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.