我正试图获得一个移动应用程序,以显示一个配套应用程序在一个连接的磨损设备。
目标
当一个简单的倒计时计时器被启动时,一条消息被发送到连接的穿戴设备上,并且它们显示与手机相同的倒计时。
问题
当磨损设备处于环境模式时,startActivity()不会导致磨损设备退出环境模式,也不会显示倒计时
接收消息但不显示App的代码..。
对于磨损应用程序从移动接收消息,它是实现为一个服务
class DataLayerListenerService : WearableListenerService() {
private lateinit var messageClient : MessageClient
private val scope = CoroutineScope(SupervisorJob() + Dispatchers.Main.immediate)
override fun onCreate() {
messageClient = Wearable.getMessageClient(this)
messageClient.addListener {
Log.d(TAG, "service OnMessageReceived ${it.path}")
if (it.path.equals(START_ACTIVITY_PATH, true)) {
Log.d(TAG, "service OnMessageReceived -> startActivity()")
startActivity(
Intent(this, MainWearActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
)
}
}
}
override fun onDestroy() {
super.onDestroy()
messageClient.removeListener { }
scope.cancel()
}
companion object {
private const val TAG = "MainWearActivity"
private const val START_ACTIVITY_PATH = "/start-activity"
}
}然后,将WearableListenerService包含在磨损报表文件中。
<service
android:name=".DataLayerListenerService"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<data
android:host="*"
android:pathPrefix="/start-activity"
android:scheme="wear" />
</intent-filter>
</service>因此,只要磨损装置不处于环境模式,代码就能很好地工作。当收到消息时,指定的应用程序将被带到前台(它甚至不需要在以前运行)
不幸的是,如果磨损设备处于Ambient模式,那么当日志显示正在接收的消息时,设备不会退出Ambient模式,并且没有显示指定的应用程序
我试过不同的意图,但到目前为止,还没有人把磨损装置从环境模式中拿出来
Intent(this, MainWearActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)为了完整,移动设备上的代码:
private fun startWearableActivity() {
lifecycleScope.launch {
try {
val connectedNodes = nodeClient.connectedNodes.await()
writeToLog("startWearableActivity() found ${connectedNodes.count()} connected nodes")
connectedNodes.map { node ->
writeToLog("startWearableActivity() - Trying to sendMessage to node ${node.displayName}")
async {
// TODO : add countdown value to the byteArrayOf()
messageClient.sendMessage(node.id, START_ACTIVITY_PATH, byteArrayOf()).await()
}
}.awaitAll()
writeToLog("startWearableActivity() - All activity start requests sent successfully")
} catch (cancellationException: CancellationException) {
writeToLog("startWearableActivity() - Start requests cancelled normally")
} catch (exception: Exception) {
writeToLog("startWearableActivity() - Activity start requests failed!!! Exception : $exception")
}
}
}当运行日志时,显示“所有活动启动请求都已成功发送”。
看来唯一的解决办法就是阻止磨损应用程序进入环境模式
window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON)那么,移动设备是否有可能向磨损设备发送消息,并使其退出环境模式?
发布于 2022-09-15 15:21:20
这里有记录。
我想你需要使用WearableListenerService。
abstract class WearDataService : WearableListenerService() {
override fun onMessageReceived(p0: MessageEvent) {
//
}
} <service
android:name="my.WearDataService"
android:exported="true">
<intent-filter>
<action android:name="com.google.android.gms.wearable.MESSAGE_RECEIVED" />
<data
android:host="*"
android:pathPrefix="/proto/"
android:scheme="wear" />
</intent-filter>
</service>https://stackoverflow.com/questions/73726665
复制相似问题