Http://code.google.com/p/apps-for-android/source/browse/trunk/Samples/WebViewDemo/
I modified it and added a simple comment. In this example, not only JS operations are performed, but Android browsers are also briefly introduced,
I will explain these in the annotations.
The javascript pop-up box contains the following three types:
Java code
- Alert ();
- Window. Confirm ("are you srue? ");
- Window. Prompt ("Please input some word";, "this is text ");
Three dialog s are captured in webchromeclient, but unfortunately, no callback function is available,
Or you cannot obtain the operation result of clicking "OK" or "cancel.
I personally think that these methods are used to test some applications involving HTML operations.
The following methods are available in webchromeclient: Java code
- Onprogresschanged (webview view, int newprogress );
- Onreceivedicon (webview view, bitmap icon );
- Onreceivedtitle (webview view, String title );
- Onrequestfocus (webview view );
- Onclosewindow (webview window );
- Onprogresschanged (webview view, int newprogress)
I will discuss the use of these methods in a later blog.
Let's take a look at the key JAVA code explained today.
- Public class webviewdemo extends activity {
- Private Static final string log_tag = "webviewdemo ";
- Private webview mwebview;
- Private textview mreusulttext;
- Private handler mhandler = new handler ();
- @ Override
- Public void oncreate (bundle icicle ){
- Super. oncreate (icicle );
- Setcontentview (R. layout. Main );
- // Obtain browser Components
- // Webview is a simple browser
- // The Android browser source code is stored in Linux \ Android \ package \ apps \ Browser
- // All operations in it are carried out around webview
- Mwebview = (webview) findviewbyid (R. Id. webview );
- Mreusulttext = (textview) findviewbyid (R. Id. resulttext );
- // Websettings almost all browser settings are in this class
- Websettings = mwebview. getsettings ();
- Websettings. setsavepassword (false );
- Websettings. setsaveformdata (false );
- Websettings. setjavascriptenabled (true );
- Websettings. setsuppzoom zoom (false );
- Mwebview. setwebchromeclient (New mywebchromeclient ());
- /*
- * The demojavascriptinterface class provides interfaces for js to call the android server.
- * Android is called by JS as the client interface of the demojavascriptinterface class.
- * The call method is defined in demojavascriptinterface:
- * For example, clickonandroid
- */
- Mwebview. addjavascriptinterface (New demojavascriptinterface (), "androd ");
- Mwebview. loadurl ("file: // android_asset/page.html ");
- }
- Final class demow.criptinterface {
- Demow.criptinterface (){}
- /**
- * This method is called by the browser.
- */
- Public void clickonandroid (){
- Mhandler. Post (New runnable (){
- Public void run (){
- // Call the onjsandroid method in JS
- Mwebview. loadurl ("javascript: onjsandroid ()");
- }
- });
- }
- }
- /**
- * Inherit the webchromeclient class
- * Process the JS pop-up box time
- *
- */
- Final class mywebchromeclient extends webchromeclient {
- /**
- * Processing the alert pop-up box
- */
- @ Override
- Public Boolean onjsalert (webview view, string URL,
- String message, jsresult result ){
- Log. D (log_tag, "onjsalert:" + message ");
- Mreusulttext. settext ("alert:" + message );
- // Simple alert Encapsulation
- New alertdialog. Builder (webviewdemo. This ).
- Settitle ("alert"). setmessage (Message). setpositivebutton ("OK ",
- New dialoginterface. onclicklistener (){
- @ Override
- Public void onclick (dialoginterface arg0, int arg1 ){
- // Todo
- }
- }). Create (). Show ();
- Result. Confirm ();
- Return true;
- }
- /**
- * Handling the confirm pop-up box
- */
- @ Override
- Public Boolean onjsconfirm (webview view, string URL, string message,
- Jsresult result ){
- Log. D (log_tag, "onjsconfirm:" + message );
- Mreusulttext. settext ("confirm:" + message );
- Result. Confirm ();
- Return super. onjsconfirm (view, URL, message, result );
- }
- /**
- * Processing prompt pop-up box
- */
- @ Override
- Public Boolean onjsprompt (webview view, string URL, string message,
- String defaultvalue, jspromptresult result ){
- Log. D (log_tag, "onjsprompt:" + message );
- Mreusulttext. settext ("prompt input is:" + message );
- Result. Confirm ();
- Return super. onjsprompt (view, URL, message, message, result );
- }
- }
- }