简体中文
需要在打开uni-app x的地方添加如下代码,触发逻辑即可打开uni-app x。
startActivity(Intent(this, UniAppActivity::class.java))
连接手机,点击运行按钮,可以在手机上查看效果。
实现实时通讯可以通过安卓原生的广播通信的方式。
首先需要在当前页面注册一个BroadcastReceiver接受android原生应用发送的广播信息。
发送广播可以通过UTSAndroid.getUniActivity()?.sendBroadcast(intent)
将信息传送到原生。
<template>
<view class="content">
<image class="logo" src="/static/logo.png" @click="send"></image>
<view class="text-area">
<text class="title">{{title}}</text>
</view>
</view>
</template>
<script>
// #ifdef APP-ANDROID
import BroadcastReceiver from 'android.content.BroadcastReceiver'
import Context from 'android.content.Context'
import Intent from 'android.content.Intent'
import IntentFilter from 'android.content.IntentFilter'
import Build from 'android.os.Build'
class MyReciver extends BroadcastReceiver {
constructor() {
}
override onReceive(context : Context, intent : Intent) {
var action = intent.getAction()
if (action.equals("ACTION_FROM_NATIVE")) {
var value = intent.getStringExtra("key");
uni.showToast({
title: value!!
})
}
}
}
// #endif
export default {
data() {
return {
title: '点击图片发送广播',
// #ifdef APP-ANDROID
receiver: null as BroadcastReceiver | null
// #endif
}
},
onLoad() {
},
onReady() {
// #ifdef APP-ANDROID
this.receiver = new MyReciver()
if (Build.VERSION.SDK_INT >= 33) {
UTSAndroid.getUniActivity()?.registerReceiver(this.receiver, IntentFilter("ACTION_FROM_NATIVE"), Context.RECEIVER_EXPORTED)
} else {
UTSAndroid.getUniActivity()?.registerReceiver(this.receiver, IntentFilter("ACTION_FROM_NATIVE"))
}
// #endif
},
onUnload() {
// #ifdef APP-ANDROID
UTSAndroid.getUniActivity()?.unregisterReceiver(this.receiver!)
// #endif
},
methods: {
send() {
// #ifdef APP-ANDROID
var intent = new Intent("ACTION_TO_NATIVE");
intent.putExtra("key", "接受到广播,三秒钟之后会接收到原生发送的广播");
UTSAndroid.getUniActivity()?.sendBroadcast(intent)
// #endif
}
}
}
</script>
<style>
.content {
display: flex;
align-items: center;
justify-content: center;
}
.logo {
height: 200rpx;
width: 200rpx;
margin-top: 200rpx;
margin-bottom: 50rpx;
}
.title {
font-size: 36rpx;
color: #8f8f94;
}
</style>
首先需要在当前activity注册一个BroadcastReceiver接受uni-app x发送的广播信息。
发送广播可以通过sendBroadcast(inte)
将信息传送到uni-app x。
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main_activity)
findViewById<View>(R.id.btn_goto).setOnClickListener {
startActivity(Intent(this@MainActivity, UniAppActivity::class.java))
ContextCompat.registerReceiver(this,broadcast, IntentFilter("ACTION_TO_NATIVE"),
ContextCompat.RECEIVER_EXPORTED)
}
}
private val broadcast = object : BroadcastReceiver() {
val handler = Handler(Looper.getMainLooper())
override fun onReceive(context: Context, intent: Intent) {
if(intent.action == "ACTION_TO_NATIVE") {
Toast.makeText(context, intent.getStringExtra("key"), Toast.LENGTH_SHORT).show()
handler.postDelayed({
val inte = Intent("ACTION_FROM_NATIVE")
inte.putExtra("key", "接受来自原生的广播")
sendBroadcast(inte)
}, 3000)
}
}
}
override fun onDestroy() {
super.onDestroy()
unregisterReceiver(broadcast)
}
}
注意:广播要在页面关闭的时候取消注册,避免可能出现崩溃的问题。
示例可以参考原生SDK中的app-comm
工程。
退出应用可以在uni-app x中调用uni.exit()
,整体退出uni-app x。