import { UTSAndroid } from "io.dcloud.uts" import { OffMemoryWarning, OnMemoryWarning, OnMemoryWarningCallback, OnMemoryWarningCallbackResult } from "../interface.uts" let listeners: OnMemoryWarningCallback[] = [] const onAppTrimMemoryListener = (ret: number) => { listeners.forEach(listener => { let res:OnMemoryWarningCallbackResult = { level:ret } listener(res) }) } @Suppress("DEPRECATION") export function onMemoryWarning(callback: OnMemoryWarningCallback) { if (listeners.length == 0) { // 仅首次执行底层的实际监听 UTSAndroid.onAppTrimMemory(onAppTrimMemoryListener) UTSAndroid.onAppActivityDestroy(()=>{ // listeners 默认是静态常量周期,activity 销毁时,需要手动清空 listeners = [] }) } listeners.push(callback) } @Suppress("DEPRECATION") export function offMemoryWarning(callback: OnMemoryWarningCallback | null = null) { if(callback == null){ // 清除全部回调 listeners = [] UTSAndroid.offAppTrimMemory(null); return } // 清除指定回调 const index = listeners.indexOf(callback) if (index > -1) { listeners.splice(index, 1) } if (listeners.length == 0) { // 当用户不再监听时,移除底层实际监听 UTSAndroid.offAppTrimMemory(onAppTrimMemoryListener) } }