Android mobile App embed web page operation

Source: Internet
Author: User

1, The main activity of the source code:

 Packagecom.jhtg.happyagent;ImportAndroid.annotation.SuppressLint;Importandroid.app.Activity;ImportAndroid.graphics.Bitmap;ImportAndroid.os.Bundle;Importandroid.view.KeyEvent;ImportAndroid.view.View;ImportAndroid.view.Window;ImportAndroid.webkit.CookieManager;Importandroid.webkit.WebChromeClient;Importandroid.webkit.WebSettings;ImportAndroid.webkit.WebView;Importandroid.webkit.WebViewClient;ImportCom.jhtg.view.SendingProgressDialog; @SuppressLint ("Setjavascriptenabled") Public classHomeactivityextendsActivity {PrivateWebView Mywebview =NULL; PrivateSendingprogressdialog M_progressdialog; /*** Get the WebView view and set webviewclient to open the link in your WebView*/@SuppressWarnings ("Deprecation") @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); //the title bar of the program is not displayedSetcontentview (r.layout.home); Mywebview=(WebView) Findviewbyid (R.id.homeview);        Cookiemanager.getinstance (). Removeallcookie (); WebSettings websettings= Mywebview.getsettings ();//get websettings Settings Object//The following 3 lines are WebView support JS and can interact with JS code settingsWebsettings.setjavascriptenabled (true); Websettings.setsupportzoom (false); Websettings.setbuiltinzoomcontrols (false); Websettings.setpluginstate (WebSettings.PluginState.ON);//here are the relevant settings for Flash supportMywebview.setscrollbarstyle (View.scrollbars_outside_overlay); Mywebview.getsettings (). Setsupportmultiplewindows (true); Mywebviewclient mwebclient=Newmywebviewclient (); Mywebview.setwebviewclient (mwebclient);//provide a webviewclient to webview so that you can open the link in your WebView        /*mywebview.setwebchromeclient (New Mychromeclient () {public void onprogresschanged (WebView view, int progres            s) {setprogress (progress * 100); }        });*/Mywebview.loadurl ("Http://www.taobao.com"); //mywebview.loadurl ("File:///android_asset/test.html ");//include local file, open in WebView    }        /*class Mychromeclient extends Webchromeclient {public void Onreceivedtitle (WebView view, final String title) { Always update the title of the page}}*/        Private classMywebviewclientextendswebviewclient {@Override Public Booleanshouldoverrideurlloading (WebView view, String URL) {if(Url.indexof ("Tel:") >= 0) {//a number on the page will cause the system to automatically connect to the phone, shielding this feature                return true;            } view.loadurl (URL); return true; } @Override Public voidonpagefinished (WebView view, String URL) {Super. onpagefinished (view, URL); if(M_progressdialog! =NULL) {m_progressdialog.stop (); }} @Override Public voidonpagestarted (WebView view, String URL, Bitmap favicon) {Super. onpagestarted (view, URL, favicon); Try{Initsendingdialog ("Loading Web page, please later ..."); } Catch(Exception e) {e.printstacktrace (); }        }    }        protected voidinitsendingdialog (String message) {//TODO auto-generated Method Stub        if(M_progressdialog = =NULL) {M_progressdialog=NewSendingprogressdialog ( This, message);    } m_progressdialog.start (); }        /*** Key response, when viewing the page in WebView, press the Back button when the return to browse history, if not do the whole webview return to exit*/@Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {        if((keycode = = Keyevent.keycode_back) &&Mywebview.cangoback ()) {            //return key BounceMywebview.goback ();//back to previous history page            return true; }        return Super. OnKeyDown (KeyCode, event); }}

2,home.xml the source code as follows:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "    android:layout_width=" Match_parent "    android:layout_height=" Match_parent "     android:orientation= "vertical" >    <WebView        android:id= "@+id/homeview"         android:layout_width= "Match_parent"        android:layout_height= "Match_parent"/></ Linearlayout>

3,Sendingprogressdialog custom pop-up box class source code as follows:

 PackageCom.jhtg.view;ImportAndroid.app.ProgressDialog;ImportAndroid.content.Context;Importandroid.view.KeyEvent;/** * @authorsang *@versioncreation Time: 2014-7-10 9:25:00 class Description*/ Public classSendingprogressdialog { PublicProgressDialog Dialog; Private FinalString message = "In the middle of sending, please wait ...";  PublicSendingprogressdialog (Context paramcontext) { This(Paramcontext,NULL); }     PublicSendingprogressdialog (Context paramcontext, String paramstring) {dialog=NewProgressDialog (paramcontext) {@Override Public BooleanOnKeyDown (intKeyCode, KeyEvent event) {                if( This. isshowing ()) {                    return false; }                return Super. OnKeyDown (KeyCode, event);        }                    };  This. dialog.setcancelable (true); if(Paramstring = =NULL) {             This. Dialog.setmessage (message); } Else {             This. Dialog.setmessage (paramstring); }    }     Public voidstart () {Try {             This. Dialog.show (); } Catch(Exception e) {}} Public voidStop () {Try {            if( This. dialog! =NULL) {                 This. Dialog.dismiss ();  This. Dialog.cancel (); }        } Catch(Exception e) {} }}

Finally, remember to add permissions to the Androidmanifest.xml:

<uses-permission android:name= "Android.permission.INTERNET"/>

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.