First, some common methods
1.WebChromeClient
WebChromeClient to deal with JS interaction and Internet industry content, commonly used methods are
-
OnJsAlert () receives an alert event from a web page
-
OnJsPrompt () receives the Prompt input box event for the web page
-
OnJsConfirm () Receives the confirm confirmation event for the web page
-
OnProgressChanged () page load percentage
-
OnReceivedIcon () receives the icon of the web page
-
OnReceivedTitle () Receives the title of the page
2.WebSettings
Webview setting class, can set various detailed parameters of webview
-
SetAllowFileAccess (Boolean allow) setAllowFileAccess(Boolean allow) Specifies whether to use the file protocol to access web pages.
-
SetBuiltInZoomControls (Boolean enabled) setBuiltInZoomControls(Boolean enabled)
-
SetDatabaseEnabled (Boolean Flag) setDomStorageEnabled(Boolean Flag);
-
SetDefaultFontSize (int size)
-
SetJavaScriptEnabled (Boolean flag) setJavaScriptEnabled(Boolean flag)
-
LOAD_DEFAULT (loaded from cache, not network), LOAD_CACHE_ELSE_NETWORK (loaded from cache, not network) LOAD_NO_CACHE LOAD_CACHE_ONLY setCacheMode()
3.WebView
Android provides controls that can load web pages and HTML data.
-
Add an Object to webView to allow Java code to interact with JS addJavascriptInterface(Object Object, String Name)
-
Load a web page, either a web page or a local web page, which is loaded using (file:///assets/). You can also call a javaScrpit method loadUrl(String URL)
-
LoadData (String data, String mimeType, String encoding) loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl)
-
Reload ()
-
Get the current page’s title getTitle()
-
CanGoBack () goBack()
2. Basic use of Webview
Because of the simplicity of the layout, I won’t show it here. There is only one VebView control in the layout
public class WebViewActivity extends Activity {
WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view); webView = (WebView)findViewById(R.id.webwiew); // Enable javaScript webView.getSettings().true); / / full-screen webView. GetSettings () setLayoutAlgorithm (WebSettings. LayoutAlgorithm. SINGLE_COLUMN); webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.loadUrl("Here's the URL of the page I want to load."); // Handle URL redirection without throwing it into the system browser webview.setWebViewClient (new)WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view,String url){
view.loadUrl(url);
return true; }}); } @Override public voidonBackPressed(){// If WebWiew can fall back to the previous pageif(webView.canGoBack()){ webView.goBack(); } super.onBackPressed(); }}Copy the code
Interaction between Java and JS in Android
- Start by writing a method (javaShow() method in my case) in the Activity. The @javascriptInterface tag must be posted above the method body
- Webview enables Java code to interact with JS by calling addJavascriptInterface(Object Object, String Name) to add a Java Object. Object is the Java Object passed to JS, and name is the name given to the Object.
- Js passes window. object name. Method name ()(I here the object name is demo, Java method name is javaShow, so can use window.demo to call the javaShow() method) can call the Java method
public class WebViewActivity extends Activity { WebView mWebView; // The content of the HTML String body; @Override @JavascriptInterface protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);setContentView(R.layout.activity_web_view);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true); // Javascript () can call the Java javaShow() method body = with window.demo.javashow ()"<html><head><style>img{width:100%}</style><script type='text/javascript'>function show(){window.demo.javaShow()} </script></head><body>"+ HTML code (HTML code is a few words instead of some HTML content because of space problems) +"</body></html>";
mWebView.addJavascriptInterface(this,"demo");
mWebView.loadDataWithBaseURL(null, body, "text/html"."utf-8", null);
}
@JavascriptInterface
public void javaShow(){
Intent intent = new Intent();
intent.setClass(this,DetailImageActivity.class);
intent.putExtra("image",images); startActivity(intent); }}Copy the code
Four, the use of WebView need to pay attention to the problem
A problem with android phones below version 4.2 is that webView will be injected with JavaScript, causing information security problems, such as downloading viruses and sending SMS messages. The problem is the addJavascriptInterface method. JavaScript can directly manipulate the native JAVA interface by calling this interface. Google solved this problem in version 4.2 by adding a declaration, @javascriptInterface, to methods called by JS, but there is no official solution for phones under version 4.2.