First, Introduction
In the development of Android we will encounter a lot of Web page functions, the following is what I used in the actual sharing to everyone
1. Displaying and rendering Web pages
2. Can be called interactively with JavaScript
Second, the commonly used methods
The OnPause action notifies the kernel to suspend all actions such as JavaScript execution. Webview.onpause ();//When activity is turned off, if WebView's music or video is still playing. Must be destroyed webview//need to remove WebView from the parent container before destroying 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 action to start loading
@Override
public void onpagestarted (WebView view, String URL, Bitmap favicon) {
Logutil.print ("onpagestarted");
}
Set the action to end the load
@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) {
Hide system-defined 404 page information with JavaScript
Logutil.print ("Onreceivederror errorCode =" + ErrorCode);
String data = "";
View.loadurl ("javascript:document.body.innerhtml=\" "+ Data +" \ ");
}
};
Third, the interaction with JS
Mwebview.getsettings (). Setjavascriptenabled (True); With JS interactive words plus the mwebview.addjavascriptinterface below (new Jsinterfacecoupon (This), "Yulinjs"); The
//yulinjs field mwebview.getsettings () that interacts with the identifier. Setsupportzoom (false); Mwebview.getsettings (). setrenderpriority (Renderpriority.high); Mwebview.getsettings (). Setcachemode (Websettings.load_default); Set the cache mode if (! Nullutil.isstringempty (Type) &&type.equals (Token_url)) {mopenfilewebchromeclient = new OpenFileWebChrome Client (this); Mwebview.setwebchromeclient (mopenfilewebchromeclient); }else{mwebview.setwebchromeclient (New Customchromeclient ("Hostapp", Hostjsscope.class));
//write tired pass through constructs to the current context
public class Jsinterface {
Activity activity;
Public Jsinterface (activity activity) {
this.activity=activity;
}
Write method to invoke 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);
}
}
Here is the HTML code
<script type= "Text/javascript" > function showtoast () { yulinjs.showtoast (" "); } </script>(Shwotoast is the native method name, and Android is the name we define in the Addjavascriptinterface method).
Iv. the question of returning to each layer
/Can I back webview.cangoback ()//Back Page Webview.goback ()//Can advance webview.cangoforward ()//Forward page Webview.goforward ()
The return button below the phone returns to the previous page does not return the entire WebView
@Override
public boolean onKeyDown (int keycode, keyevent event) {
if (isneedback && keycode = = Keyevent.keycode_back && mwebview.cangoback ()) {
Mwebview.goback ();//Return to previous page
return true;
}
Return Super.onkeydown (KeyCode, event);
}
Leilei_tua
Android WebView in project summary