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 MediaPlayer from 'android.media.MediaPlayer'; import logo from "../../static/logo.png"; import { onAppActivityDestroy, onAppActivityPause, onAppActivityResume, onAppActivityBack, getUniActivity, getAppContext } from "io.dcloud.uts.android"; /** * 定时任务参数封装 */ type TimerOptions = { /** * 定时任务开始的回调 * @res 回调参数 */ start: (res: string) => void; /** * 定时任务执行的回调 * @res 回调参数 */ 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"}; } /** * 实现一个添加view的 Runnable类 * 用法说明:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#%E5%8C%BF%E5%90%8D%E5%86%85%E9%83%A8%E7%B1%BB */ 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) } }; /** * 实现一个移除view的 Runnable类 * 用法说明:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#%E5%8C%BF%E5%90%8D%E5%86%85%E9%83%A8%E7%B1%BB */ 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) } }; /** * 实现添加view实例至decorview * */ export function addViewToDecorView() { let uiRunable = new AddUIRunnable(); getUniActivity()!.runOnUiThread(uiRunable) } /** * 实现从decorview上移除指定view */ export function removeViewToDecorView() { var uiRunable = new RemoveUIRunnable(); getUniActivity()!.runOnUiThread(uiRunable) } /** * 引用资源路径 */ export function getLogoPath(): string { return logo; } /** * 播放asset资源中的音频 */ 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) { /** * activity 销毁生命周期回调 * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitydestroy */ onAppActivityDestroy(() => { let eventName = "onAppActivityDestroy - " + Date.now(); onLifecycleChange(eventName); console.log(eventName); }); /** * activity 失去焦点生命周期回调 * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivitypause */ onAppActivityPause(() => { let eventName = "onAppActivityPause - " + Date.now(); onLifecycleChange(eventName); console.log(eventName); }); /** * activity 得到焦点的周期回调 * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityresume */ onAppActivityResume(() => { let eventName = "onAppActivityResume - " + Date.now(); onLifecycleChange(eventName); console.log(eventName); }); /** * activity 回退物理按键事件回调 * 说明文档:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#onappactivityback */ onAppActivityBack(() => { let eventName = "onAppActivityBack - " + Date.now(); onLifecycleChange(eventName); console.log(eventName); }); }