*********************************WebView初见**************************************
布局文件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.webview.MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:layout_marginTop="82dp" />
</RelativeLayout>
package com.example.webview;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class MainActivity extends Activity {
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
//默认通过浏览器打开
webView.loadUrl("http://www.baidu.com");
/**
* 应用开发中需要获取WebView当前页面的标题
* 可能通过对WebChromeClient.onReceivedTitle()方法的重写来实现
*/
webView.setWebChromeClient(new WebChromeClient(){
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}
});
webView.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// 使用自己的WebView组件来响应Url加载事件,而不是使用默认浏览器器加载页面
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
});
}
}
*************************************自定义标题栏、错误页面展示、下载apk的2种方式( 代码没有测试)*************************************
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.webview.MainActivity" >
<RelativeLayout
android:id="@+id/web_title_layout"
android:layout_width="wrap_content"
android:layout_height="50dp" >
<Button
android:id="@+id/return_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="返回" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<Button
android:id="@+id/refresh_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="刷新" />
</RelativeLayout>
<WebView
android:id="@+id/webView"
android:layout_below="@id/web_title_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:id="@+id/textview_error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/webView"
android:layout_centerHorizontal="true"
android:layout_marginTop="73dp"/>
</RelativeLayout>
package com.example.demo;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
private WebView webView;
private Button return_button, refresh_button;
private TextView titleView;
private TextView textview_error;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView);
// 默认通过浏览器打开
webView.loadUrl("http://shouji.baidu.com/");
return_button = (Button) findViewById(R.id.return_button);
refresh_button = (Button) findViewById(R.id.refresh_button);
titleView = (TextView) findViewById(R.id.title);
textview_error = (TextView) findViewById(R.id.textview_error);
/**
* 应用开发中需要获取WebView当前页面的标题
* 可能通过对WebChromeClient.onReceivedTitle()方法的重写来实现
*/
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
titleView.setText(title);
super.onReceivedTitle(view, title);
}
});
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// 使用自己的WebView组件来响应Url加载事件,而不是使用默认浏览器器加载页面
view.loadUrl(url);
return super.shouldOverrideUrlLoading(view, url);
}
/**
* 错误页面展示
*/
@Override
public void onReceivedError(WebView view, int errorCode,
String description, String failingUrl) {
// TODO Auto-generated method stub
super.onReceivedError(view, errorCode, description, failingUrl);
textview_error.setText("404错误");
webView.setVisibility(View.GONE);
}
});
webView.setDownloadListener(new MyDownLoad());
refresh_button.setOnClickListener(new MyListener());
return_button.setOnClickListener(new MyListener());
}
/**
* 拿到url的地址信息
*
*/
class MyDownLoad implements DownloadListener{
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype, long contentLength) {
if (url.endsWith(".apk")) {
/**
* 方法一
*/
new HttpThread(url).start();
/**
* 方法二:通过系统下载apk
*/
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);
}
}
}
class MyListener implements OnClickListener {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.return_button:
finish();
break;
case R.id.refresh_button:
// 刷新
webView.reload();
break;
default:
break;
}
}
}
}
package com.example.demo;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.webkit.WebView;
public class HttpThread extends Thread {
private String url;
public HttpThread(String url) {
super();
this.url = url;
}
@Override
public void run() {
super.run();
try {
URL httpUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) httpUrl
.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
InputStream inputStream = connection.getInputStream();
File downFile = null;
File sdFile = null;
FileOutputStream out = null;
Log.e("TAG", "sddd");
if (Environment.getExternalStorageState().equals(
Environment.MEDIA_CHECKING)) {
Log.e("TAG", "sd");
downFile = Environment.getExternalStorageDirectory();
sdFile = new File(downFile, "test.apk");
out = new FileOutputStream(sdFile);
}
byte[] b = new byte[6 * 1024];
int len;
while ((len = inputStream.read(b)) != -1) {
if (out != null) {
out.write(b, 0, len);
}
}
if (out != null) {
out.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}