1/* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import UIAbility from '@ohos.app.ability.UIAbility' 17import emitter from '@ohos.events.emitter' 18import Logger from '../common/Logger' 19 20const TAG = '[Sample_StartMode]' 21const eventId = 1 // 设置一个Id,用来emitter传输 22 23 24export default class SingletonAbility extends UIAbility { 25 onCreate(want) { 26 Logger.info(TAG, 'SingletonAbility onCreate') 27 const that = this 28 this.context.eventHub.on("getAbilityData", (data) => { 29 Logger.info(TAG, 'SingletonAbility' + JSON.stringify(data)) 30 data.context = that.context 31 data.launchWant = want 32 }) 33 } 34 35 onDestroy() { 36 Logger.info(TAG, 'SingletonAbility onDestroy') 37 } 38 39 onWindowStageCreate(windowStage) { 40 Logger.info(TAG, 'SingletonAbility onWindowStageCreate') 41 42 windowStage.loadContent('pages/FoodDetail', (err, data) => { 43 if (err.code) { 44 Logger.info(TAG, 'Failed to load the content. Cause:' + JSON.stringify(err)) 45 return 46 } 47 Logger.info(TAG, 'Succeeded in loading the content. Data: ' + JSON.stringify(data)) 48 }) 49 } 50 51 onNewWant(want) { 52 Logger.info(TAG, 'SingletonAbility onNewWant') 53 if (want && want.parameters) { 54 let eventData = { 55 data: { 56 "foodItemId": null 57 } 58 } 59 let innerEvent = { 60 eventId, 61 priority: emitter.EventPriority.HIGH 62 } 63 // 对获取到的id进行判断,等于0的话,赋值为1,传到前端,前端根据此id获取对应的数据 64 if (want.parameters.foodItemId == 0) { 65 want.parameters.foodItemId = 1 66 eventData.data.foodItemId = want.parameters.foodItemId 67 emitter.emit(innerEvent, eventData) 68 } else { 69 want.parameters.foodItemId = 0 70 eventData.data.foodItemId = want.parameters.foodItemId 71 emitter.emit(innerEvent, eventData) 72 } 73 } 74 } 75 76 onWindowStageDestroy() { 77 Logger.info(TAG, 'SingletonAbility onWindowStageDestroy') 78 } 79 80 onForeground() { 81 Logger.info(TAG, 'SingletonAbility onForeground') 82 } 83 84 onBackground() { 85 // Ability has back to background 86 Logger.info(TAG, 'SingletonAbility onBackground') 87 } 88} 89