Android Development--webview

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.