Android WebView summary in the project, Android webview Summary
I. Introduction
In Android development, we will encounter many web page processing functions. The following are some of the functions I have used to share with you.
1. Display and render Web pages
2. Interactive calls with JavaScript
Ii. Common Methods
// Activate WebView to be active, and the webView can be normally executed. onResume (); // when the page is switched to the invisible state in the background when the page is out of focus, the onPause action must be executed to notify the kernel to suspend all actions, such as JavaScript Execution, through the onPause action. WebView. onPause (); // when Activity is disabled, if the Webview music or video is still playing. You must first remove Webview from the parent container and then destroy webview: rootLayout. removeView (webview); webView. destroy ();
Private WebViewClient webViewClient = new WebViewClient (){
@ Override
Public void onLoadResource (WebView view, String url ){
Super. onLoadResource (view, url );
}
@ Override
Public boolean shouldOverrideUrlLoading (WebView webview, String url ){
Webview. loadUrl (url );
Return true;
}
// Set the start operation of loading.
@ Override
Public void onPageStarted (WebView view, String url, Bitmap favicon ){
LogUtil. print ("onPageStarted ");
}
// Set the end of the Load Operation
@ Override
Public void onPageFinished (WebView view, String url ){
String viewTitle = view. getTitle ();
Title_webview = viewTitle;
Url = url;
}
}
@ Override
Public void onReceivedError (WebView view, int errorCode, String description, String failingUrl ){
// Use javascript to hide the system-defined 404 page information
LogUtil. print ("onReceivedError errorCode =" + errorCode );
String data = "";
View. loadUrl ("javascript: document. body. innerHTML = \" "+ data + "\"");
}
};
3. Interaction with js
MWebView. getSettings (). setJavaScriptEnabled (true); // Add mWebView. addJavascriptInterface (new JSInterfaceCoupon (this), "yulinjs") below for JS interaction ");
// Yulinjs is the field mWebView for identifier interaction. getSettings (). setsuppzoom zoom (false); mWebView. getSettings (). setRenderPriority (RenderPriority. HIGH); mWebView. getSettings (). setCacheMode (WebSettings. LOAD_DEFAULT); // sets the cache mode if (! NullUtil. isStringEmpty (type) & type. equals (TOKEN_URL) {mOpenFileWebChromeClient = new OpenFileWebChromeClient (this); mWebView. setWebChromeClient (mOpenFileWebChromeClient);} else {mWebView. setWebChromeClient (new CustomChromeClient ("HostApp", hostjss.pdf. class ));}
// Input the current context through Constructor
Public class JSInterface {
Activity activity;
Public JSInterface (Activity activity ){
This. activity = activity;
}
// Write a method to call js
@JavascriptInterface
public void jumpToYuQuanAdmin(String id){
Intent intent = new Intent(activity, ActivityUserInfo_2.class);
int uid = UnitSociax.stringParseInt(id);
intent.putExtra("uid", uid);
activity.startActivity(intent);
}
}
// The following is the html code
<Script type = "text/javascript"> function showToast () {yulinjs. showToast ("Haha") ;}</script> (ShwoToast is the native method name, and android is the name we defined in addJavascriptInterface ).
4. Return questions for each layer
/Can you move Webview. canGoBack () // Webview. goBack () // Webview. canGoForward () // forward Webview. goForward ()
// The return button at the bottom of the mobile phone does not return the whole webview from the previous page.
@ Override
Public boolean onKeyDown (int keyCode, KeyEvent event ){
If (isNeedBack & keyCode = KeyEvent. KEYCODE_BACK & mWebView. canGoBack ()){
MWebView. goBack (); // return to the previous page
Return true;
}
Return super. onKeyDown (keyCode, event );
}
Author: LeiLei_Tua