index.uts 3.5 KB
Newer Older
杜庆泉's avatar
杜庆泉 已提交
1

杜庆泉's avatar
杜庆泉 已提交
2
import { getUniActivity,getAppContext } from "io.dcloud.uts.android";
杜庆泉's avatar
杜庆泉 已提交
3 4 5 6 7 8
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";
杜庆泉's avatar
杜庆泉 已提交
9 10 11
import Runnable from 'java.lang.Runnable';
import Exception from 'java.lang.Exception';
import MediaPlayer from 'android.media.MediaPlayer';
杜庆泉's avatar
杜庆泉 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31

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) {
	
杜庆泉's avatar
杜庆泉 已提交
32
	let taskRet = setInterval(function() {
杜庆泉's avatar
杜庆泉 已提交
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
		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 {
		
杜庆泉's avatar
杜庆泉 已提交
53 54 55
        let textView = new TextView(getUniActivity())
        textView.setText("HELLO WORLD");
        textView.textSize = 30.0.toFloat();
杜庆泉's avatar
杜庆泉 已提交
56
        textView.setBackgroundColor(Color.RED)
杜庆泉's avatar
杜庆泉 已提交
57 58
        textView.setTag("helloText")
        textView.setGravity(Gravity.CENTER)
杜庆泉's avatar
杜庆泉 已提交
59

杜庆泉's avatar
杜庆泉 已提交
60
        let decorView = getUniActivity()!.window.decorView;
杜庆泉's avatar
杜庆泉 已提交
61 62


杜庆泉's avatar
杜庆泉 已提交
63
        let frameContent = decorView.findViewById<FrameLayout>(android.R.id.content)
杜庆泉's avatar
杜庆泉 已提交
64
        let layoutParam = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
杜庆泉's avatar
杜庆泉 已提交
65 66
        layoutParam.topMargin = 200;

杜庆泉's avatar
杜庆泉 已提交
67
        frameContent.addView(textView,layoutParam)
杜庆泉's avatar
杜庆泉 已提交
68 69 70 71 72 73 74 75

    }
};

class RemoveUIRunnable extends Runnable {

    override run():void {

杜庆泉's avatar
杜庆泉 已提交
76
        let decorView = getUniActivity()!.window.decorView;
杜庆泉's avatar
杜庆泉 已提交
77
        let frameContent = decorView.findViewById<FrameLayout>(android.R.id.content)
杜庆泉's avatar
杜庆泉 已提交
78 79 80
		
		let targetTV = frameContent.findViewWithTag<TextView>("helloText")
		frameContent.removeView(targetTV)
杜庆泉's avatar
杜庆泉 已提交
81 82 83 84 85 86

    }
};


export function addViewToDecorView() {
杜庆泉's avatar
杜庆泉 已提交
87
    let uiRunable = new AddUIRunnable();
杜庆泉's avatar
杜庆泉 已提交
88 89 90 91 92 93
    // 获取android环境下 宿主decorview的基本信息
    getUniActivity()!.runOnUiThread(uiRunable)

}

export function removeViewToDecorView() {
杜庆泉's avatar
杜庆泉 已提交
94
    var uiRunable = new RemoveUIRunnable();
杜庆泉's avatar
杜庆泉 已提交
95 96 97 98 99
    // 获取android环境下 宿主decorview的基本信息
    getUniActivity()!.runOnUiThread(uiRunable)
}


杜庆泉's avatar
杜庆泉 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
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");
    });

}