package com.liyuanjinglyj.javacarddemo.slice;
import com.liyuanjinglyj.javacarddemo.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.webengine.*;
import ohos.agp.utils.TextTool;
import ohos.agp.window.dialog.ToastDialog;
import ohos.global.resource.Resource;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLConnection;
public class MainAbilitySlice extends AbilitySlice {
HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x00201, "TAG");
private WebView webView;
private Button button;
@Override
public void onStart(Intent intent) {
super.onStart(intent);
super.setUIContent(ResourceTable.Layout_ability_main);
this.webView=(WebView)findComponentById(ResourceTable.Id_ability_main_webview);
this.button=(Button)findComponentById(ResourceTable.Id_ability_main_button);
this.webView.setWebAgent(new WebAgent(){
@Override
public boolean isNeedLoadUrl(WebView webView, ResourceRequest request) {
return super.isNeedLoadUrl(webView, request);
}
@Override
public ResourceResponse processResourceRequest(WebView webView, ResourceRequest request) {
final String authority = "example.com";
final String rawFile = "/rawfile/";
final String local = "/local/";
Uri requestUri = request.getRequestUrl();
if (authority.equals(requestUri.getDecodedAuthority())) {
String path = requestUri.getDecodedPath();
if (TextTool.isNullOrEmpty(path)) {
return super.processResourceRequest(webView, request);
}
if (path.startsWith(rawFile)) {
// 根据自定义规则访问资源文件
String rawFilePath = "entry/resources/rawfile/" + path.replace(rawFile, "");
String mimeType = URLConnection.guessContentTypeFromName(rawFilePath);
try {
Resource resource = getResourceManager().getRawFileEntry(rawFilePath).openRawFile();
ResourceResponse response = new ResourceResponse(mimeType, resource, null);
return response;
} catch (IOException e) {
HiLog.info(TAG, "open raw file failed");
}
}
if (path.startsWith(local)) {
// 根据自定义规则访问本地文件
String localFile = getContext().getFilesDir() + path.replace(local, "/");
HiLog.info(TAG, "open local file " + localFile);
File file = new File(localFile);
if (!file.exists()) {
HiLog.info(TAG, "file not exists");
return super.processResourceRequest(webView, request);
}
String mimeType = URLConnection.guessContentTypeFromName(localFile);
try {
InputStream inputStream = new FileInputStream(file);
ResourceResponse response = new ResourceResponse(mimeType, inputStream, null);
return response;
} catch (IOException e) {
HiLog.info(TAG, "open local file failed");
}
}
}
return super.processResourceRequest(webView, request);
}
});
this.webView.getWebConfig().setJavaScriptPermit(true);
this.webView.load("https://example.com/rawfile/example.html");
this.button.setClickedListener(new Component.ClickedListener() {
@Override
public void onClick(Component component) {
webView.executeJs("javascript:aAddb(5,5)", new AsyncCallback<String>() {
@Override
public void onReceive(String msg) {
new ToastDialog(getContext())
.setText("a+b="+msg)
.show();
}
});
}
});
final String jsName = "JsCallbackToApp";
webView.addJsCallback(jsName, new JsCallback() {
@Override
public String onCallback(String msg) {
new ToastDialog(getContext())
.setText(msg)
.show();
return "jsResult";
}
});
}
@Override
public void onActive() {
super.onActive();
}
@Override
public void onForeground(Intent intent) {
super.onForeground(intent);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
鸿蒙WebView的使用方式.zip
共29个文件
xml:7个
java:5个
json:4个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 17 浏览量
2024-02-07
11:59:56
上传
评论
收藏 79KB ZIP 举报
温馨提示
华为 鸿蒙系统 开发
资源推荐
资源详情
资源评论
收起资源包目录
鸿蒙WebView的使用方式.zip (29个子文件)
JavaCardDemo-master
gradle.properties 816B
gradle
wrapper
gradle-wrapper.jar 57KB
gradle-wrapper.properties 194B
gradlew.bat 3KB
entry
src
test
java
com
liyuanjinglyj
javacarddemo
ExampleTest.java 138B
ohosTest
java
com
liyuanjinglyj
javacarddemo
ExampleOhosTest.java 432B
main
resources
base
media
icon.png 7KB
element
string.json 275B
graphic
background_ability_main.xml 176B
layout
ability_main.xml 640B
en
element
string.json 276B
zh
element
string.json 279B
rawfile
example.html 526B
java
com
liyuanjinglyj
javacarddemo
MyApplication.java 221B
MainAbility.java 372B
slice
MainAbilitySlice.java 5KB
config.json 1KB
proguard-rules.pro 45B
build.gradle 762B
.gitignore 7B
build.gradle 2KB
.idea
jarRepositories.xml 1KB
vcs.xml 180B
misc.xml 214B
compiler.xml 170B
gradle.xml 910B
.gitignore 47B
settings.gradle 17B
gradlew 5KB
共 29 条
- 1
资源评论
博士僧小星
- 粉丝: 1819
- 资源: 5875
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功