使用WebView实现一个简易的浏览器
1.将WebView加入到应用中
2.使用WebView加载页面
3.获取网络访问权限
4.在WebView中使用Javascript
5.处理页面导航
6.后退与前进
7.判断页面加载过程
8.WebView缓存的运用
使用WebView加载页面
1.要在WebView加载页面,使用loadUrl()方法
2.web资源:webView.loadUrl(“http://www.baidu.com“);
3.本地文件用:webView.loadUrl(“file///android_asset/XX.html”)
本地文件存放在:assets文件中
4.是页面获得焦点
webView.requestFocus();
获取网络访问权限:
在它有效工作之前,需要保证应用能访问网络,要访问网络,需要在配置文件中获取INTERNET权限
<manifest...>
<uses-permission android:name="android.permission.INTERNET" />
...
</manifest>
代码实现:
MainActivity:
public class MainActivity extends Activity implements OnClickListener {
private String url = "http://www.baidu.com";
private Button bt1;
private WebView webView;
private ProgressDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web);
bt1 = (Button) findViewById(R.id.bt1);
bt1.setOnClickListener(this);
init();
}
private void init() {
webView = (WebView) findViewById(R.id.webView);
// webView加载本地资源
// webView.loadUrl("file:///android_asset/example.html");
// webView加载web资源
webView.loadUrl("http://www.baidu.com");
// 覆盖WebView默认通过浏览器打开网页的行为,使网页可以在webView中打开
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// 返回值是true时,控制网页在webView中打开,为false,则调用系统或第三方浏览器打开
// return super.shouldOverrideUrlLoading(view, url);
view.loadUrl(url);
return true;
}
// WebViewClient帮助WebView处理一些页面的控制和请求通知
});
// 启用Javascript支持
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
// WebView加载页面优先选择使用缓存加载
webSettings.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(MainActivity.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) {
if (webView.canGoBack()) {
// 返回上级页面
webView.goBack();
return true;
} else {
// 退出程序
System.exit(0);
}
}
return super.onKeyDown(keyCode, event);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.bt1:
Uri uri = Uri.parse(url);
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
break;
default:
break;
}
}
}
web.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开浏览器,跳转到百度首页" />
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>