Android Web browser development (1) and android
Android Web browser development (1)
Please support originality, respect originality, reprinted please note the Source: http://blog.csdn.net/kangweijian (from kangweijian csdn blog)
Android Web browser development details (1): mainly uses the WebView class to load webpages, refresh webpages, load historical webpages forward, load historical webpages backward, and scale webpages.
Android Web browser Development (2) provides functions such as saving, deleting, and editing bookmarks and history records.
Android Web browser developer details (3), mainly through the AsyncTask class to achieve one-click access to multiple websites.
1、
WebView
Class
a)
WebView
Is A View class used to display webpages. This class is based on the fact that you can scroll through your web browser or simply display some online content in your
Activity
. It uses
WebKit
The rendering engine implements the following functions: loading webpages, loading historical webpages forward, loading historical webpages backward, scaling webpages, and performing text search.
b)
Use
WebView
Customize your web browser
I. ModifyWebView
Internal class
WebSettings
, Through
GetSettings ()Get
WebSettings
,
Here we use the following methods:
1.setJavaScriptEnabled()
Method to enable JavaScript.
2.setBuiltInZoomControls(boolean)
EnabledWebView
Built-in scaling function. Note: If the height and width of the widget are setWRAP_CONTENT
,
The scaling function is invalid.
3. setUseWideViewPort (true); set any scale. After zooming, you need to adapt the content to the screen without exceeding the screen display to implement line feed. In this regard, the effect should be controlled by html rather than webview. For example, <pstyle = "word-break: break-all"> test </p> Enables automatic line feed.
4.
addJavascriptInterface(Object, String)
Method To inject Java objects into WebView, so that you can access webpages through JavaScript. Of course, this is also risky, because the webpage can be directly connected to the application due to some possible problems.
Ii.Create and set
WebChromeClient
Subclass. When some may affect the browser
UI
When something happens
WebChromeClient
Class is called. For example, progress bar update and
JavaScript warning is sent. Here we use the following methods:
1. shouldOverrideUrlLoading (WebView view, String url ). When a new URL is loaded to WebView (for example, clicking a link), this method determines the next step. If true is returned, it means "Do not process this URL. I will do it myself ". If false is returned, it means "WebView loads this URL, and I will not process it"
Iii. Create and setWebViewClient
Subclass. When something that affects content presentation occursWebViewClient
Class is called. For example, submitting an error or report. You can interrupt URL loading here. Here we use the following methods:
1. onProgressChanged (WebView view, intnewProgress ). This method tells us how to load the PROGRESS program of the current page. The value of newProgress ranges from 0 to 100.
2. onmediaedtitle (WebView view, String title ). When the document title on the page changes, this method will inform us.
C)WebView
By default, components like browsers are not provided, but not by default.
JavaScript and ignore network errors. In addition to reading webpages, users cannot interact with webpages. If you want a complete and mature web browser, you can call the browser application:
Uri uri =Uri.parse("http://www.baidu.com") Intentintent = new Intent(Intent.ACTION_VIEW, uri); startActivity(intent);
2. Add necessary permissions (AndroidManifest. xml)
A) <uses-permissionandroid: name = "android. permission. INTERNET"/>
Please support originality, respect originality, reprinted please note the Source: http://blog.csdn.net/kangweijian (from kangweijian csdn blog)
Learn about the Android programming authoritative guide and notes by5.5.2.20 early