WebView such as getting the title of the page, responding to the alert in JS, and getting the loading progress of the page can be achieved by setting the webchromeclient.
First look at the code that sets Webchromeclient:
Webchromeclient = new Selfwebchromeclient (ProgressBar); webview.setwebchromeclient (webchromeclient);
Selfwebchromeclient inherited from Webchromeclient:
Class Selfwebchromeclient extends Webchromeclient {}
WebView Get page titleOnreceivedtitle methods that need to inherit and implement Webchromeclient
@Overridepublic void Onreceivedtitle (WebView view, String title) {Titletext.settext (title);}
This method returns the title of the page, and you can display this title information in the body of the method.
WebView Getting the page loading progress
Onprogresschanged methods that need to inherit and implement Webchromeclient
@Overridepublic void onprogresschanged (WebView view, int newprogress) {//Update the window in if the WebView progress is changed ( Progressbar.getvisibility ()! = view.visible&&newprogress! =) {progressbar.setvisibility (View.VISIBLE);} Progressbar.setprogress (newprogress); super.onprogresschanged (view, newprogress);}
This method returns the progress of the page load newprogress.
WebView response to the alert in JSJoin your HTML page to invoke the following JS:
Alert ("Good people");
What do you need to do to deal with it? Webchromeclient provides a callback method Onjsalert, inheriting and implementing this method, doing what you want to do in this method body, for example:
@Overridepublic Boolean Onjsalert (WebView view, string URL, string message,final jsresult result) {//Build a builder to display a in the Web page Lert Dialog Builder builder = new Builder (testwebactivity.this); Builder.settitle ("Alert Dialog"); Builder.setmessage (Message ); Builder.setpositivebutton (Android. R.string.ok,new Alertdialog.onclicklistener () {@Overridepublic void OnClick (dialoginterface dialog, int which) { Result.confirm ();}}); Builder.setcancelable (false); Builder.create (); Builder.show (); return true;}
There are also ways to do things like onjsconfirm.
WebView playing videoWebView to play the video, there are a few things to note:
1.It is best to set webchromeclient. Otherwise, it is likely that your video frame will not load successfully.
2.Set in Menifest: android:layertype= "Software". Otherwise, it is likely that your video frame will not load successfully.
3.Set in Menifest: <uses-sdk android:minsdkversion= "8" android:targetsdkversion= "/>". One of these two values is big enough, one is small enough, or your video screen may only have a sound
Image, in addition to the full screen when the screen switch also need to set a suitable version number. I gave the parameters that I tried to run normally, and the other parameters could not be known.
4.It is best to develop SDK based on Android3.2.
WebView full-screen playback needs to implement Webchromeclient, when you click on the Full Screen button of the page will trigger the Onshowcustomview method, in this method capture the video component to the appropriate place to achieve your full-screen playback:
Private View Customview;class Selfwebchromeclient extends webchromeclient {private Bitmap defaltvideo;private ProgressBar progressbar;private customviewcallback mycallback = null;public selfwebchromeclient (ProgressBar ProgressBar) {super (); this.progressbar = ProgressBar;} @Overridepublic void Onshowcustomview (view view, Customviewcallback callback) {if (mycallback! = null) { Mycallback.oncustomviewhidden (); mycallback = Null;return;} Setrequestedorientation (Activityinfo.screen_orientation_landscape); ViewGroup parent = (ViewGroup) webview.getparent ();p Arent.removeview (WebView);p arent.addview (view); Webviewframe.addview (webView); CustomView = View;mycallback = callback;} public void Onhidecustomview () {if (CustomView! = null) {if (mycallback! = null) {Mycallback.oncustomviewhidden (); mycallb ack = NULL;} try {setrequestedorientation (activityinfo.screen_orientation_portrait); Viewparent Pat = customview.getparent (); ViewGroup parent = (viewgroup) pat;parent.removeview (CustomView); webviewframe.removeAllViews ();p Arent.addview (webView); customview = null;} catch (Exception e) {e.printstacktrace ();}}} Video load Add default icon @overridepublic Bitmap getdefaultvideoposter () {System.out.println ("getdefaultvideoposter-->>") ; if (Defaltvideo = = null) {Defaltvideo = Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher);} return defaltvideo;} Video loading process loading@overridepublic View Getvideoloadingprogressview () {System.out.println (" Getvideoloadingprogressview-->> "); ProgressBar ProgressBar = new ProgressBar (Testwebactivity.this, null,android. R.attr.progressbarstylehorizontal);p Rogressbar.setlayoutparams (New Absolutelayout.layoutparams ( Layoutparams.fill_parent, 3, 0, 0)); return ProgressBar;}}
The code to reply to the small screen playback is as follows:
@Overridepublic boolean onKeyDown (int keycode, keyevent event) {if (keycode = = Keyevent.keycode_back) {if (CustomView! = N ull) {Webchromeclient.onhidecustomview (); return true;} else {if (Webview.cangoback ()) {webview.goback (); return true;} else {finish ();}}} return false;}
If there is a problem with the sound of the video playback after exiting, some people say that the exit is performed: Webview.loadurl ("About:blank"). But my test results are not very good, it is recommended to add the following code to resolve:
@Overrideprotected void Onresume () {//Logacts.i ("onresume-->>"); try {webview.getclass (). GetMethod ("Onresume" ). Invoke (WebView, (object[]) null);} catch (Exception e) {e.printstacktrace ();} Super.onresume ();} @Overrideprotected void OnStop () {try {webview.getclass (). GetMethod ("OnPause"). Invoke (WebView, (object[]) null);} catch (Exception e) {e.printstacktrace ();} Webview.loadurl ("About:blank"); Super.onstop ();}
Android WebView (Fri) webchromeclient