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 rpc from "@ohos.rpc" 17import wantAgent from '@ohos.wantAgent'; 18import backgroundTaskManager from '@ohos.backgroundTaskManager'; 19import featureAbility from '@ohos.ability.featureAbility'; 20import logger from '../MainAbility/utils/logger' 21 22const TAG: string = '[flybirdDebug.ServiceAbility]' 23 24class FirstServiceAbilityStub extends rpc.RemoteObject { 25 constructor(des: any) { 26 if (typeof des === 'string') { 27 super(des) 28 } else { 29 return 30 } 31 } 32 33 onRemoteRequest(code: number, data: any, reply: any, option: any) { 34 logger.log(`${TAG}onRemoteRequest called`) 35 if (code === 1) { 36 let dataStr = data.readString() 37 logger.log(`${TAG} string=${dataStr}`) 38 if (dataStr === 'start_game') { 39 this.startContinousTask(); 40 let result = 'ok start game' 41 logger.log(`${TAG} result=${result}`) 42 reply.writeString(result) 43 } else if (dataStr === 'disconnect_service') { 44 let result = 'ok disconnect service' 45 logger.log(`${TAG} result=${result}`) 46 reply.writeString(result) 47 } 48 else { 49 logger.log(`${TAG} error string}`) 50 } 51 } else { 52 logger.log(`${TAG} unknown request code`) 53 } 54 return true; 55 } 56 57 startContinousTask() { 58 logger.info(TAG + " start background continousTask api"); 59 60 let wantAgentInfo = { 61 wants: [ 62 { 63 bundleName: "com.samples.flybird", 64 abilityName: "com.samples.flybird.MainAbility" 65 } 66 ], 67 operationType:wantAgent.OperationType.START_ABILITY, 68 requestCode: 0, 69 wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG] 70 }; 71 72 } 73 callback(err, data) { 74 if (err) { 75 logger.error(TAG + "Operation failed Cause: " + err); 76 } else { 77 logger.info(TAG + "Operation succeeded"); 78 } 79 } 80 81} 82 83export default { 84 onStart() { 85 logger.info(`${TAG} onStart`) 86 }, 87 onStop() { 88 logger.info(`${TAG} onStop`) 89 }, 90 onConnect(want) { 91 logger.log(`${TAG} onConnect, want:${JSON.stringify(want)}`) 92 return new FirstServiceAbilityStub("first ts service stub") 93 }, 94 onDisconnect(want) { 95 logger.log(`${TAG} onDisconnect, want:${JSON.stringify(want)}`) 96 }, 97 onCommand(want, startId) { 98 logger.log(`${TAG} onCommand, want:${JSON.stringify(want)},startId:${startId}`) 99 } 100};