1.将WebView加入到你的应用当中
2.使用WebView加载页面 (覆盖默认的加载方式)
3.使用网络权限(通常需要网络权限)
4.在WebView中使用JavaScript(设置websetting)
5.处理页面导航
6.后退与前进
7判断页面加载过程
8WebView缓存的应用
9设置useragent(web端统计)
webView= (WebView) findViewById(R.id.
webView);
//WebView加载本地资源 打开了点击链接,让用户选择用什么方式来加载
// webView.loadUrl("file:///android_asset/example.html");
// WebView加载web资源
webView.loadUrl(
"https://www.baidu.com/");
// 弹出对话框让用户选择
// 使用Intent加载时一样的为什么呢
// WebView 覆盖默认通过第三方或者系统浏览器打开的网页行为方式,使得网页可以在WebView中打开
webView.setWebViewClient(
new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// ture 控制网页在WebView中打开
// false就是在系统系统浏览器或者第三方浏览器
view.loadUrl(url);
return true;
}
// WebClient帮助WebView处理一些页面控制和请求通知
}); 在WebView中使用JavaScript。如果你想要你加载在WebView中的web页面使用JavaScript,你需要在Webview中启勇JavaScript,启用JavaScript你可以通过WebView中带有的WebSettings来启动它,你可以通过getSettings()来获取,WebSetting的值,然后通过setJavaScriptEnabled()来启用JavaScript;
WebSettings settings =
webView.getSettings();
settings.setJavaScriptEnabled(
true); 当你点击返回的时候整个页面都退出了,这是不合理的
当你的WebView覆盖了URL加载,它会自动生成历史访问记录,你可以通过goBack()或goForward()向前或向后访问已访问过的站点
// 改写手机物理按键--返回的逻辑
@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);
}
重定向,转发等原因用户体验并不是那么好
无法预测显示进度是多少,一段间隔
判断页面加载过程,给用户一个友好的提示
webView.setWebChromeClient(
new WebChromeClient(){
@Override
public void onProgressChanged(WebView view,
int newProgress) {
// 参数2 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(
this);
dialog.setTitle(
"正在加载");
dialog.setProgressStyle(ProgressDialog.
STYLE_HORIZONTAL);
dialog.setProgress(newProgress);
dialog.show();
}
else {
dialog.setProgress(newProgress);
}
}
WebView缓存的应用,优先使用缓存
WebSettings settings =
webView.getSettings();
// WebView加载页面优先使用缓存加载
settings.setCacheMode(WebSettings.
LOAD_CACHE_ELSE_NETWORK); 本地当中如何保存信息,
WebView帮我们管理,当我们要清除
可获取标题显示在TextView上,标题包括返回键,刷新,标题等设置监听
webView.setWebChromeClient(
new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
textTitle.setText(title);
super.onReceivedTitle(view, title);
}
});
class MyLisstener
implements View.OnClickListener{
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.
refresh:
webView.reload();
break;
case R.id.
back:
if(
webView.canGoBack()) {
webView.goBack();
}
else {
finish();
}
break;
}
}
} WebView下载文件:通过SDK下载
webView.setDownloadListener(
new MyDownload());
class MyDownload
implements DownloadListener{
@Override
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
new HttpThread(url).start();
}
}
public class HttpThread
extends Thread {
private String
url;
public HttpThread(String url) {
this.
url = url;
}
@Override
public void run() {
try {
URL url=
new URL(
this.
url);
HttpURLConnection con=(HttpURLConnection)url.openConnection();
con.setDoInput(
true);
con.setDoOutput(
true);
File downLoadFile;
File sdFile;
if(Environment.getExternalStorageState().equals(Environment.
MEDIA_MOUNTED)){
downLoadFile=Environment.getExternalStorageDirectory();
sdFile =
new File(downLoadFile,
"test.apk");
}
byte[] bytes=
new byte[
6*
1024];
int len;
InputStream in=con.getInputStream();
FileOutputStream out=
null;
while((len=in.read(bytes))!=-
1){
if (out !=
null) {
out.write(bytes,
0,len);
}
}
if (out !=
null) {
out.close();
}
if (in !=
null) {
in.close();
}
}
catch (MalformedURLException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
} WebView下载文件:通过系统方式下载
Uri uri=Uri.parse();
if(url.endsWith(
".apk")){
// new HttpThread(url).start();
Uri uri=Uri.parse(url);
Intent intent=
new Intent(Intent.
ACTION_VIEW,uri);
startActivity(intent);
}
}
优点
构建WebView就可以展示Web数据
利用WebView打开一个地址,构建手机浏览器一样
已经建好的直接使用,随时更新,服务端直接
bug客户端下一个版本
缺点:
耗电量加载速递慢,手机发热
使用
转载请注明原文地址: https://ju.6miu.com/read-677839.html