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>