简单的三行代码既可以实现
private String url="http://2014.qq.com";
private WebView webView;
private ProgressDialog dialog;
Uri uri=Uri.EMPTY.parse(url);
Intent i=new Intent(Intent.ACTION_VIEW,uri);
startActivity(i);
我们实现一个请求服务器上的页面操作步骤
private void init(){
webView=(WebView)findViewById(R.id.webview);
webView.loadUrl(url);//加载页面路径
如果说需要加载后台服务器的路径需要获取(有配置文件)
Properties properties = readConf.getProperties(getActivity());
serverUrl = properties.getProperty("serverUrl");
reRefresh()
//覆盖webview默认通过第三方或者浏览器打开第三方的行为,是的网页在webview中打开
webview.setWebViewClient(webClient); //设置Web视图
//启用支持javascript
setUpView()
//加载页面优先使用加载缓存页面 其次再从网路获取
settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
//判断页面加载过程
setViewProgress()
}
--------------------------下面都是封装的方法提供调用------------------------------------------------------
//请求页面路径:
private void reRefresh() {
OFZAPLog.info(LOG_TAG, "reRefresh");
webview.loadUrl(serverUrl + "/mess/message.html");
//webview.loadUrl("https://www.baidu.com");
webview.reload();
}
WebViewClient webClient = new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
//true的时候控制网也在webview中打开,false在第三方浏览器打开
view.loadUrl(url);
return true;
}
//WebViewClient帮助webview去处理一些页面控制,和请求通知
@TargetApi(android.os.Build.VERSION_CODES.M)
@Override
public void onReceivedHttpError(WebView view, WebResourceRequest request, WebResourceResponse errorResponse) {
super.onReceivedHttpError(view, request, errorResponse);
// 这个方法在6.0才出现
int statusCode = errorResponse.getStatusCode();
System.out.println("onReceivedHttpError code = " + statusCode);
if (404 == statusCode || 500 == statusCode) {
if (isRefresh) {
view.loadUrl("file:///android_asset/error.html?info=页面错误,请稍后再试");
isRefresh = false;
} else {
webview.reload();
isRefresh = true;
}
}
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
super.onReceivedError(view, errorCode, description, failingUrl);//处理网页加载失败时
view.setVisibility( View.GONE );
view.loadUrl("file:///android_asset/error.html?info=网络连接错误,请稍后再试");
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
});
}
//判断页面加载过程
private void setViewProgress() {
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onProgressChanged(WebView view, int newProgress) {
super.onProgressChanged(view, newProgress);
//newProgress1--100的整数
if (newProgress==100){
//加载完毕页面 关闭progressDialog
closeDialog();
}else {
//网页正在加载 打开progressDialog
openDialog(newProgress);
}
}
private void openDialog(int newProgress) {
if (dialog==null){
dialog=new ProgressDialog(WevViewActivity.this);
dialog.setTitle("正在加载");
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setProgress(newProgress);
dialog.show();
}else {
dialog.setProgress(newProgress);
}
}
private void closeDialog() {
if (dialog!=null&& dialog.isShowing()){
dialog.dismiss();
dialog=null;
}
}
});
}
//启用支持javascript
@SuppressLint("SetJavaScriptEnabled")
private void setUpView() {
//加载需要显示的网页
WebSettings mWebSettings = webview.getSettings();
mWebSettings.setJavaScriptEnabled(true); //允许加载javascript
mWebSettings.setDomStorageEnabled(true);
webview.setWebViewClient(webClient); //设置Web视图
}
@Override
public boolean onKeyDown(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);
}