androidcomm.md 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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
## uni-app x 与 android 原生工程通信

### 广播方式

实现实时通讯可以通过安卓原生的广播通信的方式。

#### uni-app x项目

首先需要在当前页面注册一个BroadcastReceiver接受android原生应用发送的广播信息。

发送广播可以通过`UTSAndroid.getUniActivity()?.sendBroadcast(intent)`将信息传送到原生。

```uvue
<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'
	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()
			UTSAndroid.getUniActivity()?.registerReceiver(this.receiver!, new 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>
```

#### android原生项目

首先需要在当前activity注册一个BroadcastReceiver接受uni-app x发送的广播信息。

发送广播可以通过`sendBroadcast(inte)`将信息传送到uni-app x。

```kotlin
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))
            registerReceiver(broadcast, IntentFilter("ACTION_TO_NATIVE"))
        }
    }

    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`示例。