Use of WebView in android development (with a complete Program)

Source: Internet
Author: User

WebView is a good thing. It serves as a mini browser and adopts the Webkit kernel. Therefore, it supports html, javascript, css, and so on perfectly. Sometimes, we can hand over the UI and even data processing to WebView, and cooperate with PHP and other server programs. In this way, Android development becomes web development, saving a lot of effort.
The following is a simple example of WebView. If all the functions are handed over to the server script for processing, this program is complete. You only need to write the webpage, fill in the URL, and compile the program, is a new software.
Program Function Description: When a Web page is opened, a ProgressDialog is displayed. After the web page is loaded, the ProgressDialog is hidden. When you click the link on the page, the ProgressDialog is displayed again. After loading and hiding, you can return to the previous page with the return key.

XML layout:

<?xml version="1.0" encoding="UTF-8"?><AbsoluteLayout android:orientation="vertical" android:id="@+id/tab1" android:layout_width="fill_parent" android:layout_height="fill_parent"  xmlns:android="http://schemas.android.com/apk/res/android">    <WebView android:id="@+id/wv"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:layout_x="0.0dip"    android:layout_y="0.0dip"    android:layout_weight="1.0" /></AbsoluteLayout>

JAVA code:

Package com. pocketdigi. webview; import android. app. activity; import android. app. alertDialog; import android. app. progressDialog; import android. content. dialogInterface; import android. OS. bundle; import android. OS. handler; import android. OS. message; import android. view. keyEvent; import android. webkit. webChromeClient; import android. webkit. webView; import android. webkit. webViewClient; public class main extends Activity {/** Called when the activity is first created. */WebView wv; ProgressDialog pd; Handler handler; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); init (); // execute the initialization function loadurl (wv, "http://www.pocketdigi.com"); handler = new Handler () {public void handleMessage (Message msg) {// define a Handler for processing the communication between the download thread and the UI if (! Thread. currentThread (). isInterrupted () {switch (msg. what) {case 0: pd. show (); // display the progress dialog box break; case 1: pd. hide (); // hide the progress dialog box. dismiss () and cancel () are not allowed. Otherwise, when show () is called again, the small circle of the displayed dialog box does not change. Break;} super. handleMessage (msg) ;}};} public void init () {// initialize wv = (WebView) findViewById (R. id. wv); wv. getSettings (). setJavaScriptEnabled (true); // JS wv is available. setScrollBarStyle (0); // the scroll bar style. If it is set to 0, no space is left for the scroll bar. the scroll bar overwrites the wv on the webpage. setWebViewClient (new WebViewClient () {public boolean shouldOverrideUrlLoading (final WebView view, final String url) {loadurl (view, url); // return true when loading a webpage ;} // rewrite the click action and load it with webview}); wv. set WebChromeClient (new WebChromeClient () {public void onProgressChanged (WebView view, int progress) {// triggers if (progress = 100) {handler when the loading progress changes. sendEmptyMessage (1); // if all is loaded, hide the progress dialog box} super. onProgressChanged (view, progress) ;}}); pd = new ProgressDialog (main. this); pd. setProgressStyle (ProgressDialog. STYLE_SPINNER); pd. setMessage ("loading data. Please wait! ");} Public boolean onKeyDown (int keyCode, KeyEvent event) {// capture the return key if (keyCode = KeyEvent. KEYCODE_BACK) & wv. canGoBack () {wv. goBack (); return true;} else if (keyCode = KeyEvent. KEYCODE_BACK) {confirmit (); // if the return key is pressed but the return key cannot be returned, exit and confirm return true;} return super. onKeyDown (keyCode, event);} public void ConfirmExit () {// exit to confirm AlertDialog. builder ad = new AlertDialog. builder (main. this); ad. setTitle ("Exit "); Ad. setMessage (" Exit software? "); Ad. setPositiveButton ("yes", new DialogInterface. onClickListener () {// exit button @ Overridepublic void onClick (DialogInterface dialog, int I) {// TODO Auto-generated method stubmain. this. finish (); // close activity}); ad. setNegativeButton ("no", new DialogInterface. onClickListener () {@ Overridepublic void onClick (DialogInterface dialog, int I) {// no action is required to exit}); ad. show (); // display dialog box} public void loadurl (final WebView view, final String url) {new Thread () {public void run () {handler. sendEmptyMessage (0); view. loadUrl (url); // load webpage }}. start ();}}
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.