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(); // 获取android环境下 宿主decorview的基本信息 getUniActivity()!.runOnUiThread(uiRunable) } export function removeViewToDecorView() { var uiRunable = new RemoveUIRunnable(); // 获取android环境下 宿主decorview的基本信息 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 getAssetAPath() { var assetManager = getAppContext()!.getAssets(); var afd = assetManager.openFd("free.mp3"); var mediaPlayer = new MediaPlayer(); mediaPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength()); mediaPlayer.prepare(); mediaPlayer.start(); } export function initAppLifecycle() { console.log("initAppLifecycle"); onAppActivityDestroy(() => { console.log("onAppActivityDestroy"); }); onAppActivityPause(() => { console.log("onAppActivityPause"); }); onAppActivityResume(() => { console.log("onAppActivityResume"); }); onAppActivityBack(() => { console.log("onAppActivityBack"); }); }