Android使用WebView浏览网页
2014-04-28 22:32
411 查看
webview.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
<ProgressBar
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_gravity="center"
android:visibility="visible"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="@drawable/horizontal_progress"
android:id="@+id/progress_bar" />
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>WebViewActivity.java
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff" >
<ProgressBar
android:layout_width="match_parent"
android:layout_height="5dp"
android:layout_gravity="center"
android:visibility="visible"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="@drawable/horizontal_progress"
android:id="@+id/progress_bar" />
<WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>WebViewActivity.java
import java.util.Timer; import java.util.TimerTask; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.KeyEvent; import android.view.View; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar; public class WebViewActivity extends Activity { WebView webView; String urlPath = "http://www.baidu.com"; private long timeout = 12000; private Timer timer; ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.webview); progressBar = (ProgressBar)findViewById(R.id.progress_bar); webView =(WebView) findViewById(R.id.webview); webView.getSettings().setAllowFileAccess(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setDefaultTextEncodingName("UTF-8") ; //设置加载进来的页面自适应手机屏幕 // webView.getSettings().setUseWideViewPort(true); // webView.getSettings().setLoadWithOverviewMode(true); webView.requestFocus(); webView.loadUrl(urlPath); WebSettings mWebSettings = webView.getSettings(); mWebSettings.setBuiltInZoomControls(true); mWebSettings.setSupportZoom(true); webView.setWebViewClient(new MyWebViewClient()); webView.setWebChromeClient(new MyWebChromeClient()); } @Override protected void onDestroy() { super.onDestroy(); if(timer != null){ timer.cancel(); } } class MyWebViewClient extends WebViewClient{ @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); TimerTask timerTask = new TimerTask() { @Override public void run() { //超时后,首先判断页面加载进度,超时并且进度小于100,就执行超时后的动作 if (WebViewActivity.this.webView.getProgress() < 100) { Message msg = new Message(); msg.what = 1; mHandler.sendMessage(msg); timer.cancel(); timer.purge(); } } }; if(timer == null){ timer = new Timer(); timer.schedule(timerTask, timeout, 1); } } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if(url.indexOf("tel:")<0){ view.loadUrl(url); } return super.shouldOverrideUrlLoading(view, url); } @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { super.onReceivedError(view, errorCode, description, failingUrl); webView.loadDataWithBaseURL(null, "<br><br><br><br><center><h1>未能打开网页,请稍后重试!</h1></center>", "text/html", "UTF-8", null); } @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); /** * onPageFinished指页面加载完成,完成后取消计时器 */ if(timer != null){ timer.cancel(); timer.purge(); } } } private Handler mHandler = new Handler(){ @Override public void handleMessage(Message msg) { super.handleMessage(msg); if(msg.what == 1){ progressBar.setVisibility(View.GONE); webView.loadDataWithBaseURL(null, "<br><br><br><br><center><h3>未能打开网页,请稍后重试!</h3></center>", "text/html", "UTF-8", null); } } }; public class MyWebChromeClient extends android.webkit.WebChromeClient { @Override public void onProgressChanged(WebView view, int newProgress) { try { if (newProgress == 100) { progressBar.setVisibility(View.GONE); } else { if (progressBar.getVisibility() == View.GONE){ progressBar.setVisibility(View.VISIBLE); } progressBar.setProgress(newProgress); } } catch (Exception e) { } super.onProgressChanged(view, newProgress); } } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()){ //返回键退回 webView.goBack(); return true; } return super.onKeyDown(keyCode, event); } }
相关文章推荐
- Android--在程序里浏览网页/Webview的使用
- Android 使用WebView浏览有声音或者视频的网页,关闭WebView之后,声音或者视频不停止的解决办法
- Android使用WebView浏览有声音或者视频的网页,关闭WebView之后,声音或者视频不停止的解决办法
- Android使用WebView浏览有声音或者视频的网页,关闭WebView之后,声音或者视频不停止的解决办法
- Android使用WebView浏览有声音或者视频的网页,关闭WebView或者当前界面之后,声音或者视频不停止
- android中使用WebView浏览网页
- Android网络编程实践之旅(四):使用WebView浏览网页
- Android使用WebView浏览有声音或者视频的网页,关闭WebView或者当前界面之后,声音或者视频不停止
- Android开发中使用WebView控件浏览网页的方法详解
- Android--在程序里浏览网页/Webview的使用
- Android webView 加载网页时,使用本地图片替换网页内的图片
- Android使用WebView加载网页及数据
- Android中使用WebView, WebChromeClient和WebViewClient加载网页
- Android中使用WebView, WebChromeClient和WebViewClient加载网页
- Android WebView的使用(禁止超链接调用其他浏览器 设置滚动条 禁止横竖屏切换重新加载网页 )
- Android使用WebView加载网页
- Android中使用WebView, WebChromeClient和WebViewClient加载网页
- Android中使用WebView, WebChromeClient和WebViewClient加载网页
- android使用webview加载网页
- Android中使用WebView, WebChromeClient和WebViewClient加载网页