Web browser for Android Development (use of WebView) and androidwebview

Source: Internet
Author: User

Web browser for Android Development (use of WebView) and androidwebview

This example describes how to use WebView. For details about WebView, refer to WebView for Android development.

In this example, the browser can open the specified webpage, load the local webpage under the assets Directory, and view the previous Webpage Through the rollback key.

Program running:


Program code:

Package com. jph. simplebrowser; import android. OS. bundle; import android. app. activity; import android. app. alertDialog; import android. content. dialogInterface; import android. content. dialogInterface. onClickListener; import android. view. keyEvent; import android. view. menu; import android. webkit. webSettings; import android. webkit. webView; import android. webkit. webViewClient;/*** Describe: </br> * simple web browser * Open the specified webpage and load the local webpage under the assets Directory * @ author JPH * Date: 2014.08.03 **/public class SimpleBrowser extends Activity {WebView webView; // declare a WebView auxiliary class WebSettings wSettings; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); webView = (WebView) findViewById (R. id. webView); // obtain the WebSettings object wSettings = webView. getSettings (); // enable touch zoom wSettings. s EtBuiltInZoomControls (true); // enable the support window meta tag (double-click zoom) wSettings. setUseWideViewPort (true); // load the page wSettings in thumbnail mode. setLoadWithOverviewMode (true); // enables JavaScript to support wSettings. setJavaScriptEnabled (true); // sets the WebViewClient that will receive various notifications and requests (load all links in WebView) webView. setWebViewClient (new WebViewClient (); // sets the webView of the web page loaded by default. loadUrl ("http://www.baidu.com/"); // load the html under the Local assets Directory, can also be a file on the network // webView. loadUrl ("file: // android _ Asset/win8_Demo/index.html ") ;}@ Overridepublic boolean onKeyDown (int keyCode, KeyEvent event) {// TODO Auto-generated method stubswitch (keyCode) {case KeyEvent. KEYCODE_BACK: // process the if (webView. canGoBack () {webView. goBack (); // Let WebView roll back to the previous page and return true;} else {// If WebView cannot be rolled back // prompt whether the application exits the new AlertDialog. builder (SimpleBrowser. this ). setTitle ("prompt "). setMessage ("are you sure you want to exit the browser? "). SetPositiveButton ("OK", new OnClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int which) {// TODO Auto-generated method stubSimpleBrowser. this. finish (); // exit program }}). setNegativeButton ("cancel", null ). show ();} break; case KeyEvent. KEYCODE_SEARCH: // when // webView. loadUrl (""); break; default: break;} return false;} @ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. simple_browser, menu); return true ;}}

Layout file: main. xml

<WebView xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:id="@+id/webView"    android:layout_width="match_parent"    android:layout_height="match_parent" >   </WebView>
Finally, do not forget to add the network access permission to the program:

<! -- Add the permission to use the network --> <uses-permission android: name = "android. permission. INTERNET"/>




In Android development, the WebView component is used to display web pages, while clicking content in the web page opens the browser browsing. How does one default WebView browsing?

If you want to click the link for processing by yourself, instead of the link in the browser of the new Android system. Add an event listening object (WebViewClient) to WebView and rewrite some of the Methods: shouldOverrideUrlLoading: Response to the hyperlink button in the webpage. When you press a connection, WebViewClient will call this method and pass the parameter: the url to be pressed.
WebView. setWebViewClient (new WebViewClient (){
@ Override
Public boolean shouldOverrideUrlLoading (WebView view, String url ){

View. loadUrl (url); // jump to the new url in the current webview

Return true;
}
});

Develop a simple browser using WebView on the Android platform, and want to list eight websites that are frequently visited on the home page

You can use sharedPreferences or SQLite to save the data (SQLite is better ).
For example, you can create a table with a Column containing the website address and a Column containing the number of visits. Each time you access the table, you can modify the number of visits to increase it by 1. If you need to use it, you only need to sort it by the number of visits.
How to Use SQLite? Search for it online.
 

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.