提交 e407bc2d 编写于 作者: 杜庆泉's avatar 杜庆泉

混编代码增加 kotlin/uts 持续回调示例

上级 098ecf1e
...@@ -5,19 +5,24 @@ ...@@ -5,19 +5,24 @@
// #ifdef APP-ANDROID && UNI-APP-X // #ifdef APP-ANDROID && UNI-APP-X
<button @tap="callKotlinMethodGetInfoTest">调用kotlin方法</button> <button @tap="callKotlinMethodGetInfoTest">调用kotlin方法</button>
<button @tap="callJavaMethodGetInfoTest">调用java方法(需自定义基座)</button> <button @tap="callJavaMethodGetInfoTest">调用java方法(需自定义基座)</button>
<view style="height: 10px;width: 100%;"></view>
<button @tap="kotlinMemListenTest">kotlin监听内存并持续回调UTS</button>
<button @tap="kotlinStopMemListenTest">停止监听</button>
<text>{{memInfo}}</text>
// #endif // #endif
</view> </view>
</view> </view>
</template> </template>
<script> <script>
// #ifdef APP-ANDROID && UNI-APP-X // #ifdef APP-ANDROID && UNI-APP-X
import { callKotlinMethodGetInfo, callJavaMethodGetInfo} from "../../uni_modules/uts-syntaxcase"; import { callKotlinMethodGetInfo, callJavaMethodGetInfo,callKotlinCallbackUTS,callKotlinStopCallbackUTS} from "../../uni_modules/uts-syntaxcase";
// #endif // #endif
export default { export default {
data() { data() {
return { return {
title: 'UTS混编示例', title: 'UTS混编示例',
memInfo:''
} }
}, },
...@@ -36,6 +41,16 @@ ...@@ -36,6 +41,16 @@
title:javaInfo title:javaInfo
}) })
}, },
kotlinMemListenTest: function () {
callKotlinCallbackUTS(function(ret:string){
this.memInfo = ret
})
},
kotlinStopMemListenTest:function () {
callKotlinStopCallbackUTS()
},
// #endif // #endif
} }
......
...@@ -4,16 +4,21 @@ ...@@ -4,16 +4,21 @@
<view class="uni-btn-v uni-common-mt"> <view class="uni-btn-v uni-common-mt">
<button @tap="callKotlinMethodGetInfoTest">调用kotlin方法</button> <button @tap="callKotlinMethodGetInfoTest">调用kotlin方法</button>
<button @tap="callJavaMethodGetInfoTest">调用java方法(需自定义基座)</button> <button @tap="callJavaMethodGetInfoTest">调用java方法(需自定义基座)</button>
<view style="height: 10px;width: 100%;"></view>
<button @tap="kotlinMemListenTest">kotlin监听内存并持续回调</button>
<button @tap="kotlinStopMemListenTest">停止监听</button>
<text>{{memInfo}}</text>
</view> </view>
</view> </view>
</template> </template>
<script> <script>
import { callKotlinMethodGetInfo, callJavaMethodGetInfo} from "../../uni_modules/uts-syntaxcase"; import { callKotlinMethodGetInfo, callJavaMethodGetInfo,callKotlinCallbackUTS,callKotlinStopCallbackUTS} from "../../uni_modules/uts-syntaxcase";
export default { export default {
data() { data() {
return { return {
title: 'UTS混编示例', title: 'UTS混编示例',
memInfo:''
} }
}, },
...@@ -31,6 +36,15 @@ ...@@ -31,6 +36,15 @@
title:javaInfo title:javaInfo
}) })
}, },
kotlinMemListenTest: function () {
callKotlinCallbackUTS((ret) =>{
this.memInfo = ret
})
},
kotlinStopMemListenTest:function () {
callKotlinStopCallbackUTS()
},
} }
} }
......
...@@ -119,6 +119,10 @@ ...@@ -119,6 +119,10 @@
},{ },{
name: "实例测试示例", name: "实例测试示例",
url: "SyntaxCase/instanceTest" url: "SyntaxCase/instanceTest"
},
{
name: "混编测试示例",
url: "SyntaxCase/MixNativeCode"
} }
] ]
}, },
......
package uts.sdk.modules.utsSyntaxcase package uts.sdk.modules.utsSyntaxcase
import android.app.ActivityManager
import android.content.Context.ACTIVITY_SERVICE
import android.os.Build import android.os.Build
import io.dcloud.uts.UTSAndroid import io.dcloud.uts.UTSAndroid
import io.dcloud.uts.setInterval
import io.dcloud.uts.clearInterval
import io.dcloud.uts.console import io.dcloud.uts.console
object NativeCode { object NativeCode {
...@@ -19,5 +23,38 @@ object NativeCode { ...@@ -19,5 +23,38 @@ object NativeCode {
fun getJavaUser():JavaUser{ fun getJavaUser():JavaUser{
return JavaUser("张三",12) return JavaUser("张三",12)
} }
/**
* 记录上一次的任务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)
}
}
} }
\ No newline at end of file
...@@ -159,9 +159,11 @@ class RequestTaskImpl implements RequestTask { ...@@ -159,9 +159,11 @@ class RequestTaskImpl implements RequestTask {
export function request(url : string) : RequestTask | null { export function request(url : string) : RequestTask | null {
return new RequestTaskImpl(url) return new RequestTaskImpl(url)
} }
// #ifdef APP-ANDROID
// #ifdef UNI-APP-X
// #ifdef APP-ANDROID
export function callKotlinMethodGetInfo():String { export function callKotlinMethodGetInfo():String {
return NativeCode.getPhoneInfo() return NativeCode.getPhoneInfo()
} }
...@@ -170,6 +172,21 @@ export function callJavaMethodGetInfo():String { ...@@ -170,6 +172,21 @@ export function callJavaMethodGetInfo():String {
return new JavaUser("jack",12).name return new JavaUser("jack",12).name
} }
export function callKotlinCallbackUTS(callback: (res: string) => void) {
NativeCode.kotlinCallbackUTS(function(res:string){
console.log(res)
callback(res)
})
}
export function callKotlinStopCallbackUTS() {
NativeCode.kotlinStopMemListenTest()
}
// #endif
// #ifdef APP-ANDROID
// #ifdef UNI-APP-X
import KeyEvent from 'android.view.KeyEvent'; import KeyEvent from 'android.view.KeyEvent';
import Configuration from 'android.content.res.Configuration'; import Configuration from 'android.content.res.Configuration';
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册