Package Com.dream.myapplication;
Import Android.app.ProgressDialog;
Import Android.content.Intent;
Import Android.net.Uri;
Import android.support.v7.app.AppCompatActivity;
Import Android.os.Bundle;
Import Android.view.KeyEvent;
Import Android.webkit.WebChromeClient;
Import Android.webkit.WebSettings;
Import Android.webkit.WebView;
Import Android.webkit.WebViewClient;
Import Android.widget.Toast;
public class MainactivityExtends Appcompatactivity {
Private StringURL ="Https://www.baidu.com";
Private WebViewWebView;
Private ProgressDialogDialog;
@Override
protected voidOnCreate (Bundle savedinstancestate) {
Super.oncreate (Savedinstancestate);
Setcontentview (r.layout.Activity_main);
Uri uri = uri.parse (URL);
Intent Intent = new Intent (Intent.action_view,uri);
StartActivity (Intent);
Init ();
}
private voidInit () {
WebView = (WebView) Findviewbyid (r.id.Web_view);
Webview.loadurl (Url;
Open Web page in WebView
Webview.setwebviewclient (New Webviewclient () {
@Override
public BooleanShouldoverrideurlloading (WebView View, String URL) {
When the return value is true, the control Web page opens in WebView, false calls the system/third-party browser
View.loadurl (URL);
return true;
}
});
WebSettings settings =Webview.getsettings ();
Settings.setjavascriptenabled (True;
WebView Using cache Loading
Settings.setcachemode (websettings.Load_cache_else_network);
Webview.setwebchromeclient (New Webchromeclient () {
@Override
public voidOnprogresschanged (WebView View, int newprogress) {
if (newprogress = =100) {
Page loading complete, close
Closedialog ();
}else {
Web page load, open dialog
OpenDialog (newprogress);
}
}
});
}
private voidOpenDialog (int newprogress) {
Ifdialog==NULL) {
Dialog =New ProgressDialog (This;
Dialog.settitle ("Loading");
Dialog.setprogressstyle (ProgressDialog.Style_horizontal);
Dialog.setprogress (newprogress);
Dialog.show ();
}else{
Dialog.setprogress (newprogress);
}
}
private voidClosedialog () {
if (Dialog! =Null &&Dialog.isshowing ()) {
Dialog.dismiss ();
dialog=Null
}
}
Rewrite the logic returned by the physical key
@Override
public BooleanOnKeyDown (int KeyCode, KeyEvent event) {
if (keycode==keyevent.Keycode_back) {
//toast.maketext (This,webview.geturl (), Toast.length_short). Show ();
if (webview.cangoback ()) {
webview.goback ();
return true;
}Else {
System. exit (0);
}
}
return Super.onkeydown (KeyCode, event);
}
}
WebView Easy Browser