先眼熟WebView
package com.example.test; 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.WebResourceRequest; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; public class Main2Activity extends AppCompatActivity { private String url="http://www.baidu.com"; private WebView webView; private ProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); /* Uri uri=Uri.parse(url); Intent intent=new Intent(Intent.ACTION_VIEW,uri); startActivity(intent);*/ init(); } private void init() { webView= (WebView) findViewById(R.id.webView); //webView.loadUrl("file:///android_asset/example.html"); webView.loadUrl(url); //覆盖webview默认第三方或者系统浏览器打开网页的行为,使得网页可在webview打开 webView.setWebViewClient(new WebViewClient(){ @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) { //返回值是true的时候控制网页在Webview中打开,如果为false调用系统浏览器或三方浏览器打开 view.loadUrl(url); return true; } //webviewClient帮助webview去处理一些页面控制或请求通知 }); //启用javaScrip WebSettings settings=webView.getSettings(); settings.setJavaScriptEnabled(true); //WebView加载页面优先使用缓存加载 settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); webView.setWebChromeClient(new WebChromeClient(){ @Override public void onProgressChanged(WebView view, int newProgress) { //newProgress 1-100之间的整数 if (newProgress==100){ //网页加载完毕 closeDialog(); }else { //网页正在加载 openDialog(newProgress); } } private void closeDialog(){ if (dialog!=null&&dialog.isShowing()){ dialog.dismiss(); dialog=null; } } private void openDialog(int newProgress) { if (dialog==null){ dialog=new ProgressDialog(Main2Activity.this); dialog.setTitle("正在加载"); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setProgress(newProgress); dialog.show(); }else { dialog.setProgress(newProgress); } } }); } //改写物理按键返回的逻辑 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode==KeyEvent.KEYCODE_BACK){ // Toast.makeText(Main2Activity.this, webView.getUrl(), Toast.LENGTH_SHORT).show(); if (webView.canGoBack()){ webView.goBack();//返回上衣界面 return true; }else { System.exit(0); } } return super.onKeyDown(keyCode, event); } }