Using WebView in the Android SDK

Source: Internet
Author: User

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" >
&LT;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>

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.