1. Instantiate the WebView component in the Mainactivity.java under SRC:
WebView WebView = new WebView (this);
2. Call WebView's OnCreate () method to set the page to display Wevview:
Network address: Webview.loadurl ("http://www.xxxx.com");
Local file: Webview.loadurl ("file:///android_asset/XX.html"); Local files are stored in the: Assets file
3. Call the activity's Setcontentview () method to display the page view
4. Use WebView point link to see a lot of pages later in order to let WebView support fallback function, need to overwrite the Activity class onkeydown () method, if do not do any processing, click the system fallback shear key, the entire browser will call finish () and end itself, Instead of retreating back to the previous page
5. You need to add permissions to the Androidmanifest.xml file, or the Web page not available error will occur.
<uses-permission android:name= "Android.permission.INTERNET"/>
Here is a concrete example:
Mainactivity.java
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 WebView;
@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Instantiating a WebView object
WebView = new WebView (this);
Set the WebView property to be able to execute JavaScript scripts
Webview.getsettings (). Setjavascriptenabled (True);
Load Web pages that need to be displayed
Webview.loadurl ("http://www.31358.cn/");
Set up a Web view
Setcontentview (WebView);
}
@Override
Set fallback
onkeydown (int keycoder,keyevent event) method that overrides the activity class
public boolean onKeyDown (int keycode, keyevent event) {
if ((keycode = = keyevent.keycode_back) && webview.cangoback ()) {
Webview.goback (); GoBack () indicates that the previous page of the WebView is returned
return true;
}
return false;
}
Add permissions 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= "Ten"/>
<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>