kotlinCode.kt 1.5 KB
Newer Older
1 2
package uts.sdk.modules.utsSyntaxcase

杜庆泉's avatar
杜庆泉 已提交
3 4
import android.app.ActivityManager
import android.content.Context.ACTIVITY_SERVICE
5 6
import android.os.Build
import io.dcloud.uts.UTSAndroid
杜庆泉's avatar
杜庆泉 已提交
7 8 9
import io.dcloud.uts.setInterval
import io.dcloud.uts.clearInterval
import io.dcloud.uts.console
10 11 12 13

object NativeCode {

    fun getPhoneInfo():String{
杜庆泉's avatar
杜庆泉 已提交
14 15 16
        val ret = "${Build.BOARD}-${Build.USER}"
        console.log("PhoneInfo",ret)
        return ret
17 18 19 20 21 22 23 24 25
    }

    fun finishActivity(){
        UTSAndroid.getUniActivity()?.finish()
    }
	
    fun getJavaUser():JavaUser{
      return JavaUser("张三",12)
    }
杜庆泉's avatar
杜庆泉 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
    
     /**
     * 记录上一次的任务id
     */
    private var lastTaskId = -1

    fun kotlinCallbackUTS(callback: (String) -> Unit){

        if(lastTaskId != -1){
            // 避免重复开启
            clearInterval(lastTaskId)
        }

        lastTaskId = setInterval({

            val activityManager = UTSAndroid.getUniActivity()?.getSystemService(ACTIVITY_SERVICE) as ActivityManager
            val memoryInfo = ActivityManager.MemoryInfo()
            activityManager.getMemoryInfo(memoryInfo)
            val availMem = memoryInfo.availMem / 1024 / 1024
            val totalMem = memoryInfo.totalMem / 1024 / 1024

            callback("设备内存:$totalMem MB,可用内存:$availMem MB")

        },1000,2000)

    }
    
    fun kotlinStopMemListenTest(){
        if(lastTaskId != -1){
            // 避免重复开启
            clearInterval(lastTaskId)
        }
    }
59 60

}