Android WebView (5) WebChromeClient, androidwebview

Source: Internet
Author: User

Android WebView (5) WebChromeClient, androidwebview

You can set WebChromeClient to obtain the page title, respond to alert in js, and obtain the page loading progress.
First, let's take a look at the code for setting WebChromeClient:

webchromeclient = new SelfWebChromeClient( progressBar);webView.setWebChromeClient(webchromeclient);
SelfWebChromeClient inherits from WebChromeClient:
class SelfWebChromeClient extends WebChromeClient {}
To obtain the page title, WebView must inherit and implement the onReceivedTitle method of WebChromeClient.
@Overridepublic void onReceivedTitle(WebView view, String title) {titleText.setText(title);}
This method returns the title of the page. You can display the title information in this method.
WebView to get the page loading progress

The onProgressChanged method that needs to be inherited and implemented by WebChromeClient

@ Overridepublic void onProgressChanged (WebView view, int newProgress) {// when the WebView progress changes, update the window to if (progressbar. getVisibility ()! = View. VISIBLE & newProgress! = 100) {progressbar. setVisibility (View. VISIBLE);} progressbar. setProgress (newProgress); super. onProgressChanged (view, newProgress );}

This method returns the page loading progress newProgress.

In the WebView response js, alert is added to your html page and the following js is called:
Alert ("Hello everyone ");
What should we do at this time? WebChromeClient provides the callback method onJsAlert to inherit and implement this method. In this method body, do what you want to do, for example:
@ Overridepublic boolean onJsAlert (WebView view, String url, String message, final JsResult result) {// construct a Builder to display the alert dialog box Builder = new builder (TestWebActivity. this); builder. setTitle ("alert dialog box"); 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 methods such as onJsConfirm.

WebView:
1. It is best to set WebChromeClient. Otherwise, the video frame may not be loaded successfully.
2. In Menifest, set android: layerType = "software ". Otherwise, the video frame may not be loaded successfully.
3. Set <uses-sdk android: minSdkVersion = "8" android: targetSdkVersion = "16"/> In Menifest. The two values must be large enough, and the other values must be small enough. Otherwise, your screen may only have no sound.
In addition, you need to set a proper version number for switching between the portrait and landscape screens in full screen mode. I gave a parameter that I tried to run normally. I don't know if other parameters can be used.
4. It is best to develop the sdk Based on Android3.2.

WebChromeClient must be implemented as follows for full-screen WebView playback. When you click the full-screen button on the page, the onShowCustomView method is triggered. In this method, capture the video component and place it in a proper place to enable full-screen playback:
Private View customView; class 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 (); parent. removeView (webView); parent. addView (view); webviewFrame. addView (webView); customView = view; myCallback = callback;} public void onHideCustomView () {if (customView! = Null) {if (myCallback! = Null) {myCallback. onCustomViewHidden (); myCallback = null;} try {setRequestedOrientation (ActivityInfo. SCREEN_ORIENTATION_PORTRAIT); ViewParent pat = customView. getParent (); ViewGroup parent = (ViewGroup) pat; parent. removeView (customView); webviewFrame. removeAllViews (); parent. addView (webView); customView = null;} catch (Exception e) {e. printStackTrace () ;}}// Add the default icon for video loading @ Overridepublic Bitmap getdefavidevideoposter () {System. out. println ("getdefavidevideoposter -->"); if (defaltvideo = null) {defaltvideo = BitmapFactory. decodeResource (getResources (), R. drawable. ic_launcher);} return defaltvideo;} // process loading @ Overridepublic View getVideoLoadingProgressView () {System. out. println ("getVideoLoadingProgressView -->"); ProgressBar progressbar = new ProgressBar (TestWebActivity. this, null, android. r. attr. progressBarStyleHorizontal); progressbar. setLayoutParams (new AbsoluteLayout. layoutParams (LayoutParams. FILL_PARENT, 3, 0, 0); return progressbar ;}}

The code for replying to a small screen is as follows:
@Overridepublic boolean onKeyDown(int keyCode, KeyEvent event) {if (keyCode == KeyEvent.KEYCODE_BACK) {if (customView != null) {webchromeclient.onHideCustomView();return true;} else {if (webView.canGoBack()) {webView.goBack();return true;} else {finish();}}}return false;}

Some people say that webView. loadUrl ("about: blank") is executed after the video is exited "). However, my test is not very effective. We recommend that you add the following code to solve the problem:
@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();}






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.