1/* 2 * Copyright (C) 2024 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 */ 15import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 16import { hilog } from '@kit.PerformanceAnalysisKit'; 17import { window } from '@kit.ArkUI'; 18import { commonEventManager } from '@kit.BasicServicesKit'; 19 20 21const tag:string='specifedProcess' 22export default class SpecifedProcessAbility extends UIAbility { 23 24 //创建ability时 25 onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 26 hilog.info(0x0000, tag, '%{public}s', 'SpecifedProcessAbility onCreate'); 27 28 29 } 30 31 //销毁ability时 32 onDestroy(): void { 33 hilog.info(0x0000, tag, '%{public}s', 'SpecifedProcessAbility onDestroy'); 34 } 35 36 37 //创建舞台(页面)时 38 onWindowStageCreate(windowStage: window.WindowStage): void { 39 // Main window is created, set main page for this ability 40 hilog.info(0x0000, tag, '%{public}s', 'SpecifedProcessAbility onWindowStageCreate'); 41 42 windowStage.loadContent('pages/Index', (err) => { 43 if (err.code) { 44 hilog.error(0x0000, tag, 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 45 return; 46 } 47 hilog.info(0x0000, tag, 'Succeeded in loading the content.'); 48 }); 49 } 50 51 onWindowStageDestroy(): void { 52 // Main window is destroyed, release UI related resources 53 hilog.info(0x0000, tag, '%{public}s', 'SpecifedProcessAbility onWindowStageDestroy'); 54 } 55 56 57 //切换到前台时 58 onForeground(): void { 59 // Ability has brought to foreground 60 let applicationContext = this.context.getApplicationContext(); 61 applicationContext.getRunningProcessInformation((err, data) => { 62 if (err) { 63 console.error(`getRunningProcessInformation fail, error: ${JSON.stringify(err)}`); 64 65 66 } else { 67 console.log(`The process running information is: ${JSON.stringify(data)}`); 68 for (let i = 0; i < data.length; i++) { 69 const processInfo = data[i]; 70 console.log('Process state:', processInfo.state); 71 } 72 73 } 74 }); 75 hilog.info(0x0000, tag, '%{public}s', 'SpecifedProcessAbility onForeground'); 76 } 77 78 79 //切换到后台时 80 onBackground(): void { 81 // Ability has back to background 82 hilog.info(0x0000, tag, '%{public}s', 'SpecifedProcessAbility onBackground'); 83 hilog.info(0x0000, 'testtag', `onBackground`) 84 85 } 86} 87