当前位置: 首页 > 科技观察

HarmonyOS-JavaUI框架使用WebView加载本地H5页面_0

时间:2023-03-21 19:42:40 科技观察

更多内容请访问:51CTOOpenHarmony技术社区https://ost.51cto.com前言现在应用开发不可避免地需要加载一些H5页面。HarmonyOS应用程序使用WebView提供在应用程序中集成H5页面的能力。在HarmonyOS应用中,出于安全考虑,WebView不支持通过File协议直接加载资源文件或本地文件,因此无法通过文件存放路径直接加载本地H5页面。下面介绍如何在鸿蒙应用程序中加载本地文件。H5页面。WebView简介WebView是一个基于webkit引擎的显示网页的控件。它可以显示和渲染网页,相当于应用程序中的浏览器,可以加载网络上或应用程序中的HTML文件。WebView的能力:显示和渲染网页。直接使用HTML文件(在网络上或本地资源中)进行布局。它可以通过JavaScript交互调用。效果展示:实现步骤:1、首先在resources目录下创建一个rawfile文件夹,该目录下的资源会被打包到应用中。2.将H5页面放到entry/src/main/resources/rawfile文件夹下。3、WebView要访问本地的web文件,需要通过DataAbility来访问。在这里创建一个WebAbility.java文件。在WebAbility中分析本地资源文件,重写RawFileDescriptor(),得到我们解析的RawFileDescriptor对象。RawFileDescriptor可以看作是我们访问HarmonyOS应用本地资源文件的入口,通过它可以将我们的H5页面加载到WebView控件中。注意:privatestaticfinalStringENTRY_PATH_PREFIX=“entry/resources”这里,将“entry”替换为你对应模块的路径。publicclassWebAbilityextendsAbility{privatestaticfinalStringPLACEHOLDER_RAW_FILE="/rawfile/";privatestaticfinalStringPLACEHOLDER_LOCAL_FILE="/local/";privatestaticfinalStringENTRY_PATH_PREFIX="条目/资源";,字符串模式)throwsFileNotFoundException{finalintsplitChar='/';if(uri==null){thrownewFileNotFoundException("InvalidUri");}//获取uri对应的资源路径,例如:com.example.dataability/entry/resources/rawfile/Stringpath=uri.getEncodedPath();finalintsplitIndex=path.indexOf(splitChar,1);如果(splitIndex<0){thrownewFileNotFoundException("InvalidUri"+uri);}//处理不同路径下的资源文件StringtargetPath=path.substring(splitIndex);if(targetPath.startsWith(PLACEHOLDER_RAW_FILE)){//打开entry/resources/rawfile目录下的资源try{返回getResourceManager().getRawFileEntry(ENTRY_PATH_PREFIX+targetPath).openRawFileDescriptor();}catch(IOExceptione){thrownewFileNotFoundException("Notfoundsupportrawfileat"+uri);}}elseif(targetPath.startsWith(PLACEHOLDER_LOCAL_FILE)){//打开手机本地存储目录下的资源Filefile=newFile(getContext().getFilesDir(),targetPath.replace(PLACEHOLDER_LOCAL_FILE,""));if(!file.exists()){thrownewFileNotFoundException("Notfoundsupportlocalfileat"+uri);}返回getRawFileDescriptor(文件,uri);}else{thrownewFileNotFoundException("Notfoundsupportfileat"+uri);}}//获取手机本地存储目录下文件资源的访问入口privateRawFileDescriptorgetRawFileDescriptor(Filefile,Uriuri)throwsFileNotFoundException{try{finalFileDescriptorfileDescriptor=newFileInputStream(文件).getFD();返回新的RawFileDescriptor(){@OverridepublicFileDescriptorgetFileDescriptor(){returnfileDescriptor;}@OverridepubliclonggetFileSize(){return-1;}@OverridepubliclonggetStartPosition(){返回0;}@Overridepublicvoidclose()throwsIOException{}};}catch(IOExceptione){thrownewFileNotFoundException("Notfoundsupportlocalfileat"+uri);}}4.然后在"entry/src/main/config.json"中完成WebAbility的声明,代码如下:{"name":"com.example.webdemo.WebAbility","type":"data","uri":"dataability://com.example.webdemo.dataability"},在config.json中找到对应的模块,在abilities节点中添加以上代码,具体位置如下:"abilities":[{"name":"com.example.webdemo.WebAbility","type":"data","uri":"dataability://com.example.webdemo.dataability"},{"visible":true,"name"":"com.example.webdemo.MainAbility","icon":"$media:icon","description":"$string:mainability_description","formsEnabled":true,"label":"$string:entry_MainAbility","type":"page",}],......abilities中声明的uri的值为webview加载的路径,在WebAbility中解析资源文件。当路径指向WebAbility时,H5页面可以显示在WebAbility5.创建一个WebView,加载本地页面。在MainAbility的onStart()中创建一个WebView,并配置为支持访问DataAbility资源,支持JavaScript,通过webView.load()加载本地H5页面。加载地址为:"dataability://com.example.webdemo.dataability/rawfile/help-center/index.html#/","dataability://com.example.webdemo.dataability"指的是解析能力本地资源文件,依次拼接加载页面路径,具体代码如下:DirectionalLayoutdLayout=newDirectionalLayout(this);dLayout.setLayoutConfig(newComponentContainer.LayoutConfig(ComponentContainer.LayoutConfig.MATCH_PARENT,ComponentContainer.LayoutConfig.MATCH_PARENT));super.setUIContent(dLayout);WebViewwebmitView=newWebView(this);webView.getWebConfig().setJavaScriptPer(true);webView.getWebConfig().setDataAbilityPermit(true);webView.load("dataability://com.example.webdemo.dataability/rawfile/help-center/index.html#/");dLayout.addComponent(webView);综上所述,实际项目中APP中的H5页面一般都是通过网络获取的,不需要对资源文件进行本地解析。但是当手机掉线或者网络??不稳定的时候,可以将H5页面下载到本地,通过上面的方法使用webview加载本地的H5页面,避免出现页面无法访问的情况手机掉线或网络不稳定时加载。更多信息请访问:51CTOOpenHarmony技术社区https://ost.51cto.com

最新推荐
猜你喜欢