import { getUniActivity,getAppContext } from "io.dcloud.uts.android"; import Rect from "android.graphics.Rect"; import Color from "android.graphics.Color"; import TextView from "android.widget.TextView"; import FrameLayout from "android.widget.FrameLayout"; import ViewGroup from "android.view.ViewGroup"; import Gravity from "android.view.Gravity"; import Runnable from 'java.lang.Runnable'; import Exception from 'java.lang.Exception'; import MediaPlayer from 'android.media.MediaPlayer'; /** * 定时任务参数封装 */ type TimerOptions = { start: (res: string) => void; work: (res: string) => void; }; /** * 执行延时任务 */ export function doTimerTask(opts:TimerOptions) { opts.start('doTimerTask start'); setTimeout(function() { opts.work("doTimerTask work"); }, 2000); return { name: "doTimerTask" }; } /** * 执行周期任务 */ export function doIntervalTask(opts:TimerOptions) { let taskRet = setInterval(function() { opts.work("doIntervalTask work"); }, 2000); opts.start('doIntervalTask start'); return { name: "doIntervalTask",taskId:taskRet}; } /** * 清除周期任务 */ export function clearIntervalTask(taskId:number) { clearInterval(taskId); return { name: "clearIntervalTask"}; } class AddUIRunnable extends Runnable { override run():void { let textView = new TextView(getUniActivity()) textView.setText("HELLO WORLD"); textView.textSize = 30.0.toFloat(); textView.setBackgroundColor(Color.RED) textView.setTag("helloText") textView.setGravity(Gravity.CENTER) let decorView = getUniActivity()!.window.decorView; let frameContent = decorView.findViewById(android.R.id.content) let layoutParam = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT); layoutParam.topMargin = 200; frameContent.addView(textView,layoutParam) } }; class RemoveUIRunnable extends Runnable { override run():void { let decorView = getUniActivity()!.window.decorView; let frameContent = decorView.findViewById(android.R.id.content) let targetTV = frameContent.findViewWithTag("helloText") frameContent.removeView(targetTV) } }; export function addViewToDecorView() { let uiRunable = new AddUIRunnable(); getUniActivity()!.runOnUiThread(uiRunable) } export function removeViewToDecorView() { var uiRunable = new RemoveUIRunnable(); getUniActivity()!.runOnUiThread(uiRunable) } import logo from "../../static/logo.png"; import { onAppActivityDestroy, onAppActivityPause, onAppActivityResume, onAppActivityBack, } from "io.dcloud.uts.android"; export function getLogoPath(): string { return logo; } export function playAssetAudio() { let assetManager = getAppContext()!.getAssets(); let afd = assetManager.openFd("free.mp3"); let mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength()); mediaPlayer.prepare(); mediaPlayer.start(); } export function initAppLifecycle(onLifecycleChange: (event:string) => void) { onAppActivityDestroy(() => { let eventName = "onAppActivityDestroy - " + Date.now(); onLifecycleChange(eventName); console.log(eventName); }); onAppActivityPause(() => { let eventName = "onAppActivityPause - " + Date.now(); onLifecycleChange(eventName); console.log(eventName); }); onAppActivityResume(() => { let eventName = "onAppActivityResume - " + Date.now(); onLifecycleChange(eventName); console.log(eventName); }); onAppActivityBack(() => { let eventName = "onAppActivityBack - " + Date.now(); onLifecycleChange(eventName); console.log(eventName); }); }