Android Development Study Notes: WebView

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 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 done, click the system rollback scissors, 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:
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 );
// Instantiate a WebView object
Webview = new WebView (this );
// Set WebView attributes to execute Javascript scripts
Webview. getSettings (). setJavaScriptEnabled (true );
// Load the webpage to be displayed
Webview. loadUrl ("http://www.51cto.com /");
// Set the Web View
SetContentView (webview );
}

@ Override
// Set rollback
// Override the onKeyDown (int keyCoder, KeyEvent event) method of 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 WebView is returned.
Return true;
}
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" type = "codeph" text = "/codeph">
<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>
:

Step 2:
1. Declare WebView in the layout File
2. instantiate WebView in Activity
3. Call the WebView loadUrl () method to set the web page to be displayed in WevView.
4. To enable WebView to respond to the hyperlink function, call the setWebViewClient () method to set the WebView
5. 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 scissors, the entire browser calls finish () instead of rolling back to the previous page.
6. You must add permissions to the AndroidManifest. xml file. Otherwise, the Web page not available error occurs.
<Uses-permission android: name = "android. permission. INTERNET"/>
The following is an example:
MainActivity. java
Package com. android. webview. activity;
 
Import android. app. Activity;
Import android. OS. Bundle;
Import android. view. KeyEvent;
Import android. webkit. WebView;
Import android. webkit. WebViewClient;
 
Public class MainActivity extends Activity {
Private WebView webview;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
Webview = (WebView) findViewById (R. id. webview );
// Set WebView attributes to execute Javascript scripts
Webview. getSettings (). setJavaScriptEnabled (true );
// Load the webpage to be displayed
Webview. loadUrl ("http://www.51cto.com /");
// Set the Web View
Webview. setWebViewClient (new HelloWebViewClient ());
}

@ Override
// Set rollback
// Override the onKeyDown (int keyCoder, KeyEvent event) method of 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 WebView is returned.
Return true;
}
Return false;
}

// Web View
Private class HelloWebViewClient extends WebViewClient {
@ Override
Public boolean shouldOverrideUrlLoading (WebView view, String url ){
View. loadUrl (url );
Return true;
}
}
}
Main. xml
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<WebView
Android: id = "@ + id/webview"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
/>
</LinearLayout>
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" type = "codeph" text = "/codeph">
<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>
:

This article is from the "IT" blog

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.