1、首先在布局xml里面指定webview根节点
<webview android:id="@+id/mywebview" android:layout_width="match_parent" android:layout_height="match_parent"/>
2、在.java的oncreate()里使用
@override protected void oncreate(@nullable bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_three); //1. asset目录下的index.html文件 string filepath = "file:///android_asset/html/index.html"; //2.本地内存中的index.html文件 // 获取文件夹路径 string htmlpath = getexternalfilesdir("html").getpath(); file htmlfile = new file(htmlpath); // 判断是否存在,不存在则创建 if (htmlfile.exists()){ htmlpath = htmlfile.getpath()+file.separator+"index.html"; }else { htmlfile.mkdirs(); htmlpath = htmlfile.getpath()+file.separator+"index.html"; } // 地址 string localfilepath = "file:///"+htmlpath; //3.指定的url的html文件 /** * 若是不显示,在androidmanifest.xml中添加android:usescleartexttraffic="true" * 并且设置网络权限 */ string urlpath = "https://www.baidu.com/"; mywebview = findviewbyid(r.id.mywebview); websettings mywebsettings = mywebview.getsettings(); // webview解决加载html页面空白问题 mywebsettings.setjavascriptenabled(true);// 设置支持javascript mywebsettings.setusewideviewport(true);//将图片调整到适合webview大小 mywebsettings.setloadwithoverviewmode(true);//缩放至屏幕大小 mywebsettings.setdomstorageenabled(true);//设置dom缓存,当h5网页使用localstorage时一定要设置 mywebsettings.setcachemode(android.webkit.websettings.load_no_cache);// 设置去缓存,防止加载的是上一次数据 mywebsettings.setdatabaseenabled(true); // 解决加载本地内存中报错 err_access_denied mywebsettings.setallowfileaccess(true); mywebsettings.setallowcontentaccess(true); // 解决webview报错 loading local files from file:// urls is not possible due browser security restrictions /** * 设置是否允许运行在一个file schema url环境下的javascript访问来自其他任何来源的内容, * 包括其他file schema urls。 * 通过此api可以设置是否允许通过file url加载的javascript可以访问其他的源, * 包括其他的文件和http,https等其他的源。与上面的类似,实现一个就可以。 * websetting.setallowuniversalaccessfromfileurls(true); * */ mywebsettings.setallowuniversalaccessfromfileurls(true); /** * 设置是否允许运行在一个file schema url环境下的javascript访问来自其他任何来源的内容, * 包括其他file schema urls。 * 通过此api可以设置是否允许通过file url加载的javascript可以访问其他的源, * 包括其他的文件和http,https等其他的源。与上面的类似,实现一个就可以。 */ //mywebsettings.setallowuniversalaccessfromfileurls(true); //加载html if (filepath != null) { mywebview.loadurl(urlpath); } }
3、创建assets目录(与res目录同一级别)
4、将要访问的*.html页面放置到assets目录即可
5、使用x5内核 腾讯sdk
地址:腾讯浏览服务
下载sdk:腾讯浏览服务-sdk下载
放置在libs文件夹,引用
as高版本:
implementation(filetree("libs"))
as低版本:
android{ ... sourcesets { main { jnilibs.srcdirs = ['libs'] } } } dependencies{ ... compile files('libs/tbs_sdk_thirdapp_v4.3.0.386_44286_sharewithdownloadwithfile_withoutgame_obfs_20230210_114429.jar') }
androidmanifest.xml配置权限
<uses-permission android:name="android.permission.internet" /> <uses-permission android:name="android.permission.access_network_state" /> <application android:name=".activity.app.myaplication" *** /application>
application.java设置初始化
package com.example.yuanzhoulv.activity.app;; import android.app.application; import com.tencent.smtt.sdk.qbsdk; public class myaplication extends application { @override public void oncreate() { // todo auto-generated method stub super.oncreate(); //搜集本地tbs内核信息并上报服务器,服务器返回结果决定使用哪个内核。 qbsdk.preinitcallback cb = new qbsdk.preinitcallback() { @override public void onviewinitfinished(boolean arg0) { // todo auto-generated method stub //x5內核初始化完成的回调,为true表示x5内核加载成功,否则表示x5内核加载失败,会自动切换到系统内核。 } @override public void oncoreinitfinished() { // todo auto-generated method stub } }; //x5内核初始化接口 qbsdk.initx5environment(getapplicationcontext(), cb); } }
使用:
*.xml
<com.tencent.smtt.sdk.webview android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="match_parent"/>
*.java
//1. asset目录下的index.html文件 string filepath = "file:///android_asset/html/index.html"; //2.本地内存中的index.html文件 // 获取文件夹路径 string htmlpath = getexternalfilesdir("html").getpath(); file htmlfile = new file(htmlpath); // 判断是否存在,不存在则创建 if (htmlfile.exists()){ htmlpath = htmlfile.getpath()+file.separator+"index.html"; }else { htmlfile.mkdirs(); htmlpath = htmlfile.getpath()+file.separator+"index.html"; } // 地址 string localfilepath = "file:///"+htmlpath; //3.指定的url的html文件 /** * 若是不显示,在androidmanifest.xml中添加android:usescleartexttraffic="true" * 并且设置网络权限 */ string urlpath = "https://www.baidu.com/"; webview = findviewbyid(r.id.webview); com.tencent.smtt.sdk.websettings websettings = webview.getsettings(); websettings.setjavascriptenabled(true);// 设置支持javascript websettings.setusewideviewport(true);//将图片调整到适合webview大小 websettings.setloadwithoverviewmode(true);//缩放至屏幕大小 websettings.setdomstorageenabled(true);//设置dom缓存,当h5网页使用localstorage时一定要设置 websettings.setcachemode(android.webkit.websettings.load_no_cache);// 设置去缓存,防止加载的是上一次数据 websettings.setdatabaseenabled(true); // 解决加载本地内存中报错 err_access_denied websettings.setallowfileaccess(true); websettings.setallowcontentaccess(true); websettings.setallowuniversalaccessfromfileurls(true); //加载html if (filepath != null) { webview.loadurl(localfilepath); }
总结
到此这篇关于android使用webview加载html页面的文章就介绍到这了,更多相关android用webview加载html页面内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论