鴻蒙JSFA 使用 WebView
我的具體流程:JSFA 呼叫 JAVAPA 拉起 JAVAFA
1.JSFA
export const AboutAbility = {
openWebView: async function(){
var actionData = {};
var action = {};
action.bundleName = 'com.example.phone';
action.abilityName = 'com.example.phone.ability.AboutAbility';
action.messageCode = 1001;
action.data = actionData;
action.abilityType = 0;
action.syncOption = 0;
var result = await FeatureAbility.callAbility(action);
var ret = JSON.parse(result);
if (ret.code == 0) {
console.log(ret);
} else {
console.error(JSON.stringify(ret.code));
}
},
}
2.JAVAPA
case OPEN_WEBVIEW:{
System.out.println("開啟webview");
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder().withBundleName(getBundleName())
.withAbilityName(WebViewAbility.class.getName()).build();
intent.setOperation(operation);
startAbility(intent);
break;
}
3.JAVAFA
web_view.xml
<?xml version="1.0" encoding="utf-8"?>
<DirectionalLayout
xmlns:ohos="http://schemas.huawei.com/res/ohos"
ohos:height="match_parent"
ohos:width="match_parent"
ohos:orientation="vertical">
<ohos.agp.components.webengine.WebView
ohos:id="$+id:webview"
ohos:height="match_parent"
ohos:width="match_parent">
</ohos.agp.components.webengine.WebView>
</DirectionalLayout>
WebViewAbility.java
public class WebViewAbility extends Ability {
private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");
@Override
public void onStart(Intent intent) {
super.onStart(intent);
System.out.println("WEBVIEW start");
super.setMainRoute(WebViewAbilitySlice.class.getName());
}
}
WebViewAbilitySlice.java
public class WebViewAbilitySlice extends AbilitySlice {
@Override
public void onStart(Intent intent) {
super.onStart(intent);
// 加載XML布局作為根布局
super.setUIContent(ResourceTable.Layout_web_view);
WebView webView = (WebView) findComponentById(ResourceTable.Id_webview);
final String url = "https://www.bilibili.com/"; // EXAMPLE_URL由開發者自定義
webView.load(url);
}
}
4.配置
securityConfig
"securityConfig": {
"domainSettings": {
"cleartextPermitted": true,
"domains": [
{
"subDomains": true,
"name": "www.bilibili.com"
}
]
}
}
abilities
{
"skills": [
{
"entities": [
"entity.system.home"
],
"actions": [
"action.system.home"
]
}
],
"name": "com.example.phone.MainAbility",
"icon": "$media:icon",
"description": "$string:mainability_description",
"label": "XXXXX",
"type": "page",
"launchType": "standard"
},
{
"skills": [
{
"entities": [
],
"actions": [
"action.ability.webview"
]
}
],
"orientation": "portrait",
"formEnabled": false,
"name": "com.example.phone.ability.WebViewAbility",
"icon": "$media:icon",
"description": "$string:mainability_description",
"label": "webview",
"type": "page",
"launchType": "standard"
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/275026.html
標籤:其他
