Android WebView Development Detailed

Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>Android WebView Development Detailed</strong></p></p><p><p><strong>1. overview:</strong><br>Android WebView as a vector control for hosting web pages, He will generate some events during the Web page display and callback to our application so that we can do what the application wants to do during the page loading process. For example, the client needs to display the Web page loading progress, page loading errors and so on Events. WebView provides two event callback classes to the application layer, respectively, for webviewclient,webchromeclient developers to inherit these two classes and take over the corresponding event Handling. Webviewclient mainly provides the Web page loading various stages of the notification, such as Web page start loading onpagestarted, page end loading onpagefinished, etc. webchromeclient mainly provide the content of the data provided during the Web page loading, such as the return page Title,favicon.</p></p><p><p><strong>Basic use of 2.WebViewClient:</strong><br>Create the Webviewclient instance and set it to the WebView object, with the following code reference:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs axapta"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyAndroidWebViewClient</span> <span class="hljs-inheritance"><span class="hljs-keyword">extends</span></span> <span class="hljs-title">WebViewClient</span> {</span> @Override <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span> onPageStarted(WebView view, String url, Bitmap favicon) { <span class="hljs-comment">// TODO </span> } @Override <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span> onPageFinished(WebView view, String url) { <span class="hljs-comment">// TODO </span> } } webview.setWebViewClient(<span class="hljs-keyword">new</span> MyAndroidWebViewClient ()); </code></pre></pre><p><p><strong>3.WebViewClient API Detailed:</strong><br><strong>1) Page Loading Timing section:</strong></p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword">public</span><span class="hljs-keyword">boolean</span><span class="hljs-title">shouldOverrideUrlLoading</span>(WebView view, String url) </code></pre></pre><p><p>When the loaded Web page needs to redirect, it will call back this function tells us whether the application needs to take over the control page load, if the application takes over, and return true means that the main program takes over the page load, if return false let WebView handle it by itself.</p></p><p><p><strong>parameter Description:</strong><br>@param view receives the instance of webviewclient, webview.setwebviewclient (new myandroidwebviewclient ()), which is the WEBVIEW.<br>@param url is the URL that will be loaded<br>@return True the current application will handle the URL itself and return false to not process it.<br><strong>Tips</strong><br>(1) This callback is not notified when the request is in the "POST" Mode.<br>(2) when the address we visit needs to be handled by our application, we can intercept it here, for example, if we find a link to a market, we can go directly to the marketplace, or to other apps.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onPageStarted</span>(WebView view, String url, Bitmap favicon) </code></pre></pre><p><p>When the kernel starts to load the accessed url, the application is notified that the function for each main frame is only called once, the page contains an iframe, or framesets does not call another onpagestarted, when the frame embedded within the page Onpagestarted is not called when a change occurs.</p></p><p><p><strong>parameter Description:</strong><br>@param view receives the instance of webviewclient, webview.setwebviewclient (new myandroidwebviewclient ()), which is the WEBVIEW.<br>@param url is the URL that will be loaded<br>@param favicon If the favicon is already stored in the local database, the favicon of the page is returned, otherwise null is Returned.</p></p><p><p><strong>Tips:</strong><br>(1) iframe may be a lot of people do not know what the meaning, here I explain the iframe we loaded a sheet, there are many links below, we casually click on a link is the current host an Iframe.<br>(2) There is a problem that may be confusing to developers, onpagestarted and shouldoverrideurlloading in the process of Web page loading, which of these two functions is called First.<br>When we reload a URL by loadurl, we call onpagestarted again and call shouldoverrideurlloading, when we click on a link in the open url, This will call Shouldoverrideurlloading before calling Onpagestarted. however, shouldoverrideurlloading is not necessarily called every time, but only when it is Needed.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"> <span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onPageFinished</span>(WebView view, String url) </code></pre></pre><p><p>When the kernel loads the current page, it notifies our application that the function is called only in the case of main frame, when the function is called, the rendered picture is not updated, and if you need to get notification of the new image you can use @link Webview.picturelistener#onnewpicture.</p></p><p><p><strong>parameter Description:</strong><br>@param view receives the instance of webviewclient, webview.setwebviewclient (new myandroidwebviewclient ()), which is the WEBVIEW.<br>@param url is the URL that will be loaded</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-title">shouldInterceptRequest</span>(WebView view, String url) </code></pre></pre><p><p>Notifies the application that the kernel is about to load url-developed resources, the application can return local resources to the kernel, and if the returned data is processed locally, the kernel does not fetch data from the Network.</p></p><p><p><strong>parameter Description:</strong><br>@param view receives the instance of webviewclient, webview.setwebviewclient (new myandroidwebviewclient ()), which is the WEBVIEW.<br>Resources developed @param URL Raw URL<br>@return returns Webresourceresponse contains a data object, or returns null</p></p><p><p><strong>Tips</strong><br>This callback does not necessarily work on the UI thread, so we need to be aware of the actions associated with view or private data here.<br>If we need to change the background of the page, or need to implement the page color customization needs, can be processed at this callback Time.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onReceivedError</span><span class="hljs-keyword">int</span> errorCode, </code></pre></pre><p><p>When a browser accesses a URL that has been made, it notifies us of the application, such as a network Error.</p></p><p><p><strong>parameter Description:</strong><br>@param view receives the instance of webviewclient, webview.setwebviewclient (new myandroidwebviewclient ()), which is the WEBVIEW.<br>@param errorCode Error number can find the corresponding error name in the webviewclient.error_*.<br>@param description describes the wrong information<br>@param failingurl The URL of the current access failure, Note that it is not necessarily our main URL</p></p><p><p><strong>Tips</strong><br>In Onreceiveerror we can customize the Page's error page.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onFormResubmission</span>(WebView view, Message dontResend, Message resend) </code></pre></pre><p><p>If the browser needs to resend the post request, it can be handled by this Time. The default is to not resend the Data.<br><strong>parameter Description:</strong><br>@param view receives the instance of webviewclient, webview.setwebviewclient (new myandroidwebviewclient ()), which is the WEBVIEW.<br>@param dontresent This parameter can be used when the browser does not need to resend the Data.<br>@param resent this parameter can be used when the browser needs to resend the Data.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">doUpdateVisitedHistory</span>(WebView view, String url, <span class="hljs-keyword">boolean</span> isReload) </code></pre></pre><p><p>The notification application can store the current URL in the database, meaning that the current access URL is in effect and is logged in the Kernel. This function is only called once during page Loading. Note that the page forward and backward does not callback this Function.<br><strong>parameter Description:</strong><br>@param view receives the instance of webviewclient, webview.setwebviewclient (new myandroidwebviewclient ()), which is the WEBVIEW.<br>@param the URL currently being accessed by the URL<br>@ param Isreload If true this is the URL that is being reload</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onReceivedSslError</span>(WebView view, SslErrorHandler handler, SslError error) </code></pre></pre><p><p>This method is called when an SSL error is found during a Web page load Resource. Our application must respond by canceling the request Handler.cancel () or continuing to request Handler.proceed (); The default behavior of the kernel is Handler.cancel ();</p></p><p><p><strong>parameter Description:</strong><br>@param view receives the instance of webviewclient, webview.setwebviewclient (new myandroidwebviewclient ()), which is the WEBVIEW.<br>@param handler the object to process the user Request.<br>@param Error SSL Error object</p></p><p><p><strong>Tips</strong><br>The kernel remembers this selection, and if the next time there is the same error, the kernel executes the results of the previous selection directly.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs lasso"><span class="hljs-keyword">public</span><span class="hljs-literal">void</span> onReceivedHttpAuthRequest(WebView view, <span class="hljs-built_in">String</span><span class="hljs-built_in">String</span></code></pre></pre><p><p>Notifies the application that WebView receives an HTTP auth request, and the application can use supplied to set the WebView response Request. The default behavior is to cancel this Request.<br><strong>parameter Description:</strong><br>@param view receives the instance of webviewclient, webview.setwebviewclient (new myandroidwebviewclient ()), which is the WEBVIEW.<br>@param handler object used to respond to WebView requests<br>Host requesting authentication @param host<br>@param realm earnestly requests the domain</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-title">shouldOverrideKeyEvent</span><span class="hljs-keyword">event</span>) </code></pre></pre><p><p>Provides application synchronization an opportunity to handle key events, menu shortcuts need to be filtered Out. If return True,webview does not handle the event, WebView will always handle the event if it returns false, so there is no parent class on the view chain that can respond to this event. The default behavior is return false;<br><strong>parameter Description:</strong><br>@param view receives the instance of webviewclient, webview.setwebviewclient (new myandroidwebviewclient ()), which is the WEBVIEW.<br>@param event Keyboard Events Name<br>@return If True is returned, the application processes the time and returns false with WebView Processing.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onScaleChanged</span><span class="hljs-keyword">float</span><span class="hljs-keyword">float</span> newScale) </code></pre></pre><p><p>Notifies the application that WebView is to be scale. Applications can handle change events, such as adjusting the adaptation Screen.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs lasso"><span class="hljs-keyword">public</span><span class="hljs-literal">void</span><span class="hljs-built_in">String</span> realm, <span class="hljs-built_in">String</span><span class="hljs-built_in">String</span> args) </code></pre></pre><p><p>Notification application has an account process for automatic login<br><strong>parameter Description:</strong><br>@param View Request Login WebView<br>@param the domain of your realm account to find your Account.<br>@param accounts An optional account, if NULL is required to check with the local account, and if it is an available account, provide login.<br>Login user @param args to validate parameters</p></p><p><p><strong>5. Webchromeclient API Detailed</strong><br>Create the Webchromeclient instance and set it to the WebView object, with the following code reference:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onProgressChanged</span><span class="hljs-keyword">int</span> newProgress) </code></pre></pre><p><p>Notifies the application of the current page load Progress.<br><strong>parameter Description:</strong><br>@param view receives the instance of webviewclient, webview.setwebviewclient (new myandroidwebviewclient ()), which is the WEBVIEW.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onReceivedTitle</span>(WebView view, String title) </code></pre></pre><p><p>When the title of document changes, the application is notified<br><strong>parameter Description:</strong><br>@param view receives the instance of webviewclient, webview.setwebviewclient (new myandroidwebviewclient ()), which is the WEBVIEW.<br>@param title of document new title<br><strong>Tips</strong><br>This function call time is uncertain, it may be very early, it may be very late, depending on where the Web page puts the title, most Web pages generally set the title to the front of the page, so many things will be earlier callback to this Function.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onReceivedIcon</span>(WebView view, Bitmap icon) </code></pre></pre><p><p>This function is recalled when the current page has a new Favicon.<br><strong>parameter Description:</strong><br>@param view receives the instance of webviewclient, webview.setwebviewclient (new myandroidwebviewclient ()), which is the WEBVIEW.<br>Favicon of the current page of the @param icon</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onReceivedTouchIconUrl</span>(WebView view, String url, <span class="hljs-keyword">boolean</span> precomposed) </code></pre></pre><p><p>Notifies the application of the Apple-touch-icon URL<br><strong>parameter Description:</strong><br>@param view receives the instance of webviewclient, webview.setwebviewclient (new myandroidwebviewclient ()), which is the WEBVIEW.<br>Service-side address for @param URL Apple-touch-icon<br>@param precomposed If precomposed is true Touch-icon is pre-created<br><strong>Tips</strong><br>If the application requires this icon, you can get the icon from this Url.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onShowCustomView</span>(View view, CustomViewCallback callback) </code></pre></pre><p><p>Notifies the application that WebView needs to display a custom view, which is primarily used in the video full screen html5video Support.<br><strong>parameter Description:</strong><br>@param view the view that will be displayed<br>@param callback Use this object for callback notification when view requires DISMISS.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onHideCustomView</span>() </code></pre></pre><p><p>Exit Video Notifications</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword">public</span><span class="hljs-keyword">boolean</span><span class="hljs-title">onCreateWindow</span><span class="hljs-keyword">boolean</span> isDialog, <span class="hljs-keyword">boolean</span> isUserGesture, Message resultMsg) </code></pre></pre><p>The <p> Request creates a new window, and if our application takes over this request, it must return true and create a new WebView to host the main WINDOW. <br> If the application does not process, you need to return false, and the default behavior is the same as returning FALSE. <br> <strong> parameter description: </strong> <br> @param view request to create a new window of WebView <br> @param isusergesture If true, the user cleans up the action, such as the user clicks the link The <br> @param isdialog true to request that a new window be created must be a dialog, not a full-screen window. <br> @param resultmsg A message needs to be sent when WebView is Created. WebView.WebViewTransport.setWebView (WebView) <br> <strong> Tips specific examples are as Follows: </strong> </p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword">private</span><span class="hljs-keyword">void</span><span class="hljs-title">createWindow</span>(<span class="hljs-keyword">final</span> Message msg) { WebView.WebViewTransport transport = (WebView.WebViewTransport) msg.obj; <span class="hljs-keyword">final</span> Tab newTab = mWebViewController.openTab(<span class="hljs-keyword">null</span>, Tab.<span class="hljs-keyword">this</span><span class="hljs-keyword">true</span>, <span class="hljs-keyword">true</span>); transport.setWebView(newTab.getWebView()); msg.sendToTarget(); } </code></pre></pre><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onRequestFocus</span>(WebView view) </code></pre></pre><p><p>WebView request to get focus, this is mainly the current webview is not the foreground state, is the background webview.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onCloseWindow</span>(WebView window) </code></pre></pre><p><p>Notifies the application to remove the WebView passed over from the view Tree.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs vbnet"><span class="hljs-keyword">public</span><span class="hljs-built_in">boolean</span><span class="hljs-built_in">String</span><span class="hljs-built_in">String</span> message, JsResult result) </code></pre></pre><p><p>Notifies the application that the JavaScript Alert dialog box is displayed, and if the application returns True the kernel considers the application to handle this message, returning false to the kernel itself.<br><strong>parameter Description:</strong><br>@param view receives the instance of webviewclient, webview.setwebviewclient (new myandroidwebviewclient ()), which is the WEBVIEW.<br>@param URL Current request popup JavaScript dialog webview loaded URL address.<br>Content Information @param message pop-up<br>@result is used to respond to user Processing.</p></p><p><p><strong>Tips</strong><br>If we apply takeover processing, We must give results of result, result.cancel,result.comfirm must call it, otherwise the kernel will Hang.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs vbnet"><span class="hljs-keyword">public</span><span class="hljs-built_in">boolean</span><span class="hljs-built_in">String</span><span class="hljs-built_in">String</span> message, JsResult result) </code></pre></pre><p><p>Notifies the application that the Confirm dialog box is Available.<br>Parameter Description ditto Onjsalert</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs vbnet"><span class="hljs-keyword">public</span><span class="hljs-built_in">boolean</span><span class="hljs-built_in">String</span><span class="hljs-built_in">String</span> message, <span class="hljs-built_in">String</span> defaultValue, JsPromptResult result) </code></pre></pre><p><p>Notifies the application that a prompt dialog box is Displayed.<br><strong>Tips</strong><br>The Result.confirm method must be called if the application takes over this Method.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs vbnet"><span class="hljs-keyword">public</span><span class="hljs-built_in">boolean</span><span class="hljs-built_in">String</span><span class="hljs-built_in">String</span> message, JsResult result) </code></pre></pre><p><p>The notification application displays a dialog box that lets the user choose whether to leave the current page, which is the onbeforeunload event in javascript, and if the client returns true, the kernel considers the client to provide a dialog box. The default behavior is return False.<br>The parameter description is the same as the Onjsalert () described Earlier.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onExceededDatabaseQuota</span>(String url, String databaseIdentifier, <span class="hljs-keyword">long</span><span class="hljs-keyword">long</span><span class="hljs-keyword">long</span> totalQuota, WebStorage.QuotaUpdater quotaUpdater) </code></pre></pre><p><p>Notifies the application that the WebView kernel Web SQL database exceeds the quota and requests that the database disk quota be Enlarged. The default behavior is to not increase the database Quota.<br><strong>Parameter description:</strong></p></p><p><p>@param URL to trigger this database quota URL address<br>@param databaseidentifier indicates that the database exceeded the Quota's Identity.<br>@param quota the size of the original database quota, which is the byte unit bytes<br>@param estimateddatabasesize reach the baseline data size bytes<br>@param totalquota Total Database Quota size bytes<br>@param quotaupdater objects that update database quotas, you can use Quotaupdater.updatequota (newquota) and configure a new database quota size.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onReachedMaxAppCacheSize</span>(<span class="hljs-keyword">long</span><span class="hljs-keyword">long</span> quota, WebStorage.QuotaUpdater quotaUpdater) </code></pre></pre><p><p>Notifies the application that the kernel has reached the maximum Appcache.<br>AppCache is a data processing standard for HTML5 for OFFLINE.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onGeolocationPermissionsShowPrompt</span>(String origin, GeolocationPermissions.Callback callback) </code></pre></pre><p><p>Whether the current page request allows positioning.</p></p><p><p>Use of Geolocationpermissions.callback</p></p><p><p>public void Invoke (String origin, boolean allow, boolean retain);</p></p><p><p><strong>parameter Description:</strong><br>Source Address @param Origin permission settings<br>@param allow location is allowed<br>@retain whether the current selection is to be remembered by the Kernel.</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">void</span><span class="hljs-title">onGeolocationPermissionsHidePrompt</span>() </code></pre></pre><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs lasso"><span class="hljs-keyword">public</span><span class="hljs-literal">void</span> openFileChooser(ValueCallback<span class="hljs-subst"><</span>Uri<span class="hljs-subst">></span><span class="hljs-built_in">String</span><span class="hljs-built_in">String</span> capture) </code></pre></pre><p><p>This callback is a private callback, when the page needs to request to open the System's file selector, it will be called back to this method, such as we need to upload images, request photos, mail attachments upload and so On.<br>If you do not implement this private api, none of the above requests will be Executed.</p></p><p><p>There are questions to welcome the Discussion.</p></p> <p><p>Android WebView Development Detailed</p></p></span>

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.