Package com.zhangxi.test01;
Import android.app.Activity;
Import Android.app.ProgressDialog;
Import Android.os.Bundle;
Import android.os.Process;
Import android.view.KeyEvent;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import android.webkit.WebChromeClient;
Import android.webkit.WebSettings;
Import Android.webkit.WebView;
Import android.webkit.WebViewClient;
public class Mainactivity extends Activity {
//
WebView WebView;
ProgressDialog Dialog;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
 
webview= (WebView) Findviewbyid (R.ID.WEBVIEW1);
Webview.loadurl ("http://sports.163.com");
Set WebView View
Webview.setwebviewclient (New Webviewclient () {
@Override
public boolean shouldoverrideurlloading (WebView view, String URL) {
TODO auto-generated method stubs
Return super.shouldoverrideurlloading (view, URL);
View.loadurl (URL);
return true;
}
});
Set JavaScript available
WebSettings websettings=webview.getsettings ();
Websettings.setjavascriptenabled (TRUE);
Prioritize using caching
Webview.getsettings (). Setcachemode (Websettings.load_cache_else_network);
Do not use cache Websettings.load_no_cache
Webview.setonkeylistener (this);
Webview.setwebchromeclient (New Webchromeclient () {
@Override
public void onprogresschanged (WebView view, int newprogress) {
TODO auto-generated method stubs
Super.onprogresschanged (view, newprogress);
if (newprogress==100) {
Closedialog ();
} else {
OpenDialog (newprogress);//Opening Web page
}
}
private void Closedialog () {
TODO auto-generated method stubs
dialog is not equal to null and dialog is being displayed
if (dialog! = null && dialog.isshowing ()) {
Dialog.dismiss ();//Cancel Display
dialog=null;//object set to Empty
}
}
private void OpenDialog (int newprogress) {
TODO auto-generated method stubs
if (dialog==null) {
Dialog=new ProgressDialog (mainactivity.this);
Dialog.settitle ("Loading");
Dialog.setprogressstyle (progressdialog.style_horizontal);//progress bar style
Dialog.setprogress (newprogress);//Display current progress
Dialog.show ();
}
else {
Dialog.setprogress (newprogress);//update current progress at any time
}
}
});
}
Set fallback
@Override
public boolean onKeyDown (int keycode, keyevent event) {
if (Keycode==keyevent.keycode_back)
{
if (Webview.cangoback ()) {
Webview.goback ();
return true;
}
else{
System.exit (0);
}
}
TODO auto-generated method stubs
Return Super.onkeydown (KeyCode, event);
return false;
}
}
Android Development--webview