package ohos.samples.jsapplication;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.rpc.IRemoteObject;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.app.Context;
import ohos.rpc.*;
import java.io.OutputStream;
import java.net.Socket;
public class MiddleAbility extends Ability {
private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");
private static final int OPEN_NEWPAGE = 1001;
private MiddleAbility.MideleRemote mideleRemote;
@Override
public void onStart(Intent intent) {
HiLog.error(LABEL_LOG, "MiddleAbility::onStart");
super.onStart(intent);
}
@Override
public void onBackground() {
super.onBackground();
HiLog.info(LABEL_LOG, "MiddleAbility::onBackground");
}
@Override
public void onStop() {
super.onStop();
HiLog.info(LABEL_LOG, "MiddleAbility::onStop");
}
@Override
public void onCommand(Intent intent, boolean restart, int startId) {
}
@Override
public IRemoteObject onConnect(Intent intent) {
Context context = getContext();
mideleRemote = new MiddleAbility.MideleRemote(context);
return mideleRemote.asObject();
}
@Override
public void onDisconnect(Intent intent) {
}
class MideleRemote extends RemoteObject implements IRemoteBroker {
private Context context;
private Socket socket = null;
private OutputStream os = null;
public MideleRemote(Context context) {
super("MideleRemote");
this.context = context;
}
@Override
public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) throws RemoteException {
switch (code) {
case OPEN_NEWPAGE:{
System.out.println("开启页面2");
//主要代码
Intent intent = new Intent();
Operation operation = new Intent.OperationBuilder().withBundleName(getBundleName())
.withAbilityName(MainAbility2.class.getName()).build();
intent.setOperation(operation);
startAbility(intent);
//主要代码
break;
}
default: {
reply.writeString("service not defined");
return false;
}
}
return true;
}
@Override
public IRemoteObject asObject() {
return this;
}
}
}
(5)修改Demo 中的js文件夹下的index.js 内容为:
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export const MiddleAbility = {
openNewPage: async function(){
var actionData = {};
var action = {};
action.bundleName = 'ohos.samples.jsapplication';
action.abilityName = 'ohos.samples.jsapplication.MiddleAbility';
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));
}
}
}
export default {
data: {
cartText: '下一步',
cartStyle: 'cart-text',
isCartEmpty: true,
descriptionFirstParagraph: 'This is the merchandise page that includes fresh fruit, meat, snacks, merchandise, and more. \nYou can pick anything you like and add it to your cart. \nYour order will arrive within 48 hours. We guarantee that we are organic and healthy. \n Feel free to ask our 24-hour online service to learn more about our platforms and products.',
imageList: ['/common/item_000.png', '/common/item_001.png', '/common/item_002.png', '/common/item_003.png'],
},
swipeToIndex(index) {
this.$element('swiperImage').swipeTo({index: index});
},
addCart() {
MiddleAbility.openNewPage();
},
getFocus() {
if (this.isCartEmpty) {
this.cartStyle = 'cart-text-focus';
}
},
lostFocus() {
if (this.isCartEmpty) {
this.cartStyle = 'cart-text';
}
},
}