Android webview Study Notes

Source: Internet
Author: User

Webview can load and display webpages as a browser. It uses the WebKit rendering engine to load and display webpages. There are two different ways to achieve webview:
Step 1:
1. instantiate the webview component in the activity: webview = new webview (this );
2. Call the webview loadurl () method to set the web page to be displayed in wevview:
For Internet: webview. loadurl ("http://www.google.com ");
Local file: webview. loadurl ("file: // android_asset/xx.html"); local files are stored in the assets file.
3. Call the setcontentview () method of activity to display the webpage view.
4. after reading many pages through the webview link, in order to allow webview to support the rollback function, we need to overwrite the onkeydown () method of the activity class. If no processing is performed, click the system rollback key, the entire browser calls finish () instead of rolling back to the previous page.

5. You must add permissions to the androidmanifest. xml file. Otherwise, the web page not available error may occur.

 <uses-permission android:name="android.permission.INTERNET" />

The following is an example:

Package COM. android. webview. activity; import android. app. activity; import android. OS. bundle; import android. view. keyevent; import android. webKit. webview; public class mainactivity extends activity {private webview; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); // instantiate the webview object webview = new webview (this); // you can run the Javascript script webview to set webview attributes. getsettings (). setjavascriptenabled (true); // load the webview of the page to be displayed. loadurl ("http://www.51cto.com/"); // sets the Web View setcontentview (webview);} @ override // sets the rollback // override the onkeydown (INT keycoder, keyevent event) of the activity class) method public Boolean onkeydown (INT keycode, keyevent event) {If (keycode = keyevent. keycode_back) & webview. cangoback () {webview. goback (); // Goback () indicates that return true on the previous page of webview;} return false ;}

Add permissions to row 17 in the androidmanifest. xml file

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.android.webview.activity"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="10" />     <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".MainActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application>    <uses-permission android:name="android.permission.INTERNET"/></manifest>

 

Related Article

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.