把网页加载进安卓显示,及其android系统webview控件使用详解
2015-11-19 15:59
816 查看
<p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">WebView</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">android 之<WebView>控件,可以将html文件转换成webview显示到手机上</p><p style="color: rgb(51, 51, 51); font-family: Arial; font-size: 14px; line-height: 26px;">请求网络需要获得权限 <uses-permission android:name="android.permission.INTERNET" /></p> <p>首先在android工程包中建一个activity。</p><p>同样在建一个xml文件布局,把webview控件添加进去。</p>
package com.scxh.android.ui.wedget; import android.annotation.SuppressLint; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.webkit.JavascriptInterface; import android.webkit.WebSettings; import android.webkit.WebView; import android.widget.Toast; import com.scxh.android.frame.R; @SuppressLint("JavascriptInterface") public class MyWebView extends Activity { WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mywebactivity_layout); mWebView = (WebView) findViewById(R.id.my_web); mWebView.loadUrl("http://192.168.1.111:8080/marry.html");// 写好的html加载到webview上 WebSettings setting = mWebView.getSettings(); setting.setJavaScriptEnabled(true);// 设置script可用 <strong><span style="color:#990000;">mWebView.addJavascriptInterface(new MyWebClick(), "</span><span style="color:#993399;">htmlListener</span><span style="color:#990000;">")</span></strong>;//htmlListener 交互类名, } /* * 交互类,处理html中的监听事件 */ public class MyWebClick { @JavascriptInterface//如果点击未响应,代码又没错,记得在方法上加上这句话 public void playMusics() {//要做的事 Log.v("WebClick", "playMusic...."); Toast.makeText(MyWebView.this, "welcome~", Toast.LENGTH_SHORT) .show(); } } }
在html中 在需要响应的标间上通过onclick属性,写一个方法名,在
<script>
function 方法名{
window.交互类名.方法
}
</script>
<html>
<head>
<meta http-equiv="content-type" content ="text/type charset=UTF-8"/>
<link rel="stylesheet" href="m.css" type="text/css"/>
<script>
<span style="color:#3333ff;"><strong>function palymusic(</strong>)</span>{
alert("你好,我是一张喇叭图片");/*在网页中点击弹出的对话框*/
<strong><span style="color:#cc0000;">window.</span><span style="color:#cc33cc;">htmlListener</span><span style="color:#cc0000;">.playMusics()</span></strong>;/*window.交互类名.方法*/
}
</script>
</head>
<body bgcolor="#680011">
<div align="center" width="320px" >
<div class="top">
<img class="bg" src="./img/index_top_bg.png"/>
<img class="ring" src="./img/index_microphone.png" <strong><span style="color:#3333ff;">onclick="palymusic()"</span></strong>/>
<div class="text">
<p>离我们结婚</p>
<p class="text_below">还有<span class="text_num">36</span>天</p>
</div>
</div>
<div class="pic">
<img width="150px" height="130px" src="./img/index_yaoqinghan.png" />
<img width="150px" height="130px" src="./img/index_hunsha.png" />
<img class ="pad" width="150px" height="100px" src="./img/index_yaoyiyao.png" />
<img class ="pad" width="150px" height="100px" src="./img/index_zhufuqiang.png" />
</div>
<div class="foot">
<a href="www.baidu.com">婚礼承办方:成都幸福公社婚庆公司<img src="./img/redRightarrow.png"/></a>
</div>
</div>
</body>
</html>
缓存处理
package com.scxh.android1503.ui.webview; import java.io.File; import android.annotation.SuppressLint; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Window; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.Toast; import com.scxh.android1503.R; import com.scxh.android1503.ui.adapter.gridview.GridViewActivity; import com.scxh.android1503.util.Logs; /** * 当我们加载Html时候,会在我们data/应用package下生成database与cache两个文件夹: 我们请求的Url记录是保存在webviewCache.db里,而url的内容是保存在webviewCache文件夹下. WebView中存在着两种缓存:网页数据缓存(存储打开过的页面及资源)、H5缓存(即AppCache)。 一、网页缓存 1、缓存构成 /data/data/package_name/cache/ /data/data/package_name/database/webview.db /data/data/package_name/database/webviewCache.db 综合可以得知 webview 会将我们浏览过的网页url已经网页文件(css、图片、js等)保存到数据库表中 缓存模式(5种) LOAD_CACHE_ONLY: 不使用网络,只读取本地缓存数据 LOAD_DEFAULT: 根据cache-control决定是否从网络上取数据。 LOAD_CACHE_NORMAL: API level 17中已经废弃, 从API level 11开始作用同LOAD_DEFAULT模式 LOAD_NO_CACHE: 不使用缓存,只从网络获取数据. LOAD_CACHE_ELSE_NETWORK,只要本地有,无论是否过期,或者no-cache,都使用缓存中的数据。 如:www.taobao.com的cache-control为no-cache,在模式LOAD_DEFAULT下,无论如何都会从网络上取数据,如果没有网络,就会出现错误页面;在LOAD_CACHE_ELSE_NETWORK模式下,无论是否有网络,只要本地有缓存,都使用缓存。本地没有缓存时才从网络上获取。 www.360.com.cn的cache-control为max-age=60,在两种模式下都使用本地缓存数据。 总结:根据以上两种模式,建议缓存策略为,判断是否有网络,有的话,使用LOAD_DEFAULT,无网络时,使用LOAD_CACHE_ELSE_NETWORK。 * */ public class MyWebViewActivity extends Activity { private static final String APP_CACAHE_DIRNAME = "/webcache"; private WebView mWebView; private String url = "http://192.168.1.156:8080/html/index.html"; // private String url = "http://m.dianhua.cn/detail/31ccb426119d3c9eaa794df686c58636121d38bc?apikey=jFaWGVHdFVhekZYWTBWV1ZHSkZOVlJWY&app=com.yulore.yellowsdk_ios&uid=355136051337627"; @SuppressLint("JavascriptInterface") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.webview_main_layout); mWebView = (WebView) findViewById(R.id.my_webview); mWebView.loadUrl(url); /** 第一步设置JavaScript可用 */ WebSettings setting = mWebView.getSettings(); setting.setJavaScriptEnabled(true); setting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); // 设置 缓存模式 /** 第三步 */ mWebView.addJavascriptInterface(new WebViewJavaScript(), "musicServiceInterfaceName"); /**使用加载URL在webView内部跳转*/ mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView webview, String url) { webview.loadUrl(url); return true; } }); } /** * 第二步 定义交互接类名"musicServiceInterfaceName"和方法 * 交互接类名"musicServiceInterfaceName"和方法名"playMusic"在html页面javaScript代码中引用 例如: * window.musicServiceInterfaceName.playMusic */ class WebViewJavaScript { public void playMusic() { // 播放音乐 Toast.makeText(MyWebViewActivity.this, "html代码调用原生应用代码方法", Toast.LENGTH_SHORT).show(); } public void startGridViewHttp() { startActivity(new Intent(MyWebViewActivity.this, GridViewActivity.class)); } } @Override protected void onDestroy() { super.onDestroy(); // clearWebViewCache(); } /** * 清除WebView缓存 */ public void clearWebViewCache() { // 清理Webview缓存数据库 try { deleteDatabase("webview.db"); deleteDatabase("webviewCache.db"); } catch (Exception e) { e.printStackTrace(); } // WebView 缓存文件 File appCacheDir = new File(getFilesDir().getAbsolutePath()+ APP_CACAHE_DIRNAME); Logs.e("appCacheDir path=" + appCacheDir.getAbsolutePath()); File webviewCacheDir = new File(getCacheDir().getAbsolutePath()+ "/webviewCache"); Logs.e("webviewCacheDir path=" + webviewCacheDir.getAbsolutePath()); // 删除webview 缓存目录 if (webviewCacheDir.exists()) { deleteFile(webviewCacheDir); } // 删除webview 缓存 缓存目录 if (appCacheDir.exists()) { deleteFile(appCacheDir); } } /** * 递归删除 文件/文件夹 * * @param file */ public void deleteFile(File file) { Logs.i("delete file path=" + file.getAbsolutePath()); if (file.exists()) { if (file.isFile()) { file.delete(); } else if (file.isDirectory()) { File files[] = file.listFiles(); for (int i = 0; i < files.length; i++) { deleteFile(files[i]); } } file.delete(); } else { Logs.e("delete file no exists " + file.getAbsolutePath()); } } }
相关文章推荐
- iOS开发之MD5封装及应用
- Android 使用Fragment,ViewPagerIndicator 开发APP项目的主框架
- iOS设置同一个Label的不同颜色的字体
- android Graphics
- warn: Appium support for versions of node < 0.12 has been deprecated and will be removed in a future
- Android音频系统之AudioFlinger(二)
- Ubuntu搭建Android开发环境
- android 新手错误
- Android官方提供的支持不同屏幕大小的全部方法(转)
- Mac安装非app store下载的软件
- android canvas.drawText()的研究
- iOS 9 适配中出现的坑
- webView 显示一段 html 代码
- iOS检测耳机插入拔出
- 【转】ios开发之生成所缩略图方式
- android第三方接入(QQ、微信、新浪微博和百度)
- android Intent实现页面跳转
- 编译ijkplayer-android
- iOS系统的这些小功能会影响到你的APP哦~你测了没呢?
- IOS创建弹出层