WebView Easy Browser

Source: Internet
Author: User

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

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.