[英]How to open Google Play from the phone remotely on the watch (Wear OS)
What is the correct way to remotely open Google Play from the phone on the watch (Wear OS)?在手表(Wear OS)上从手机远程打开 Google Play 的正确方法是什么? I am trying this:我正在尝试这个:
Intent intentOnWatch = new Intent(Intent.ACTION_VIEW)
.addCategory(Intent.CATEGORY_BROWSABLE)
.setData(Uri.parse("https://play.google.com/store/apps/details?id=\" + getPackageName()"));
RemoteIntent.startRemoteActivity(getApplicationContext(), intentOnWatch, null, null);
But nothing happens.但什么也没有发生。
The last parameter is the nodeId.最后一个参数是nodeId。 I left it as zero because the documentation says:我将其保留为零,因为文档说:
nodeId String: Wear OS node id for the device where the activity should be started. nodeId String:应启动 Activity 的设备的 Wear OS 节点 ID。 If null, and the current device is a watch, the activity will start on the companion phone device.如果 null,并且当前设备是手表,则活动将在配套手机设备上启动。 Otherwise, the activity will start on all connected watch devices.否则,活动将在所有连接的手表设备上启动。
Source: https://developer.android.com/reference/com/google/android/wearable/intent/RemoteIntent#startremoteactivity资料来源: https://developer.android.com/reference/com/google/android/wearable/intent/RemoteIntent#startremoteactivity
I could determine the nodeId, but it seems difficult to do.我可以确定nodeId,但似乎很难做到。 Plus in this case, starting the activity on all connected watch devices would be fine.另外在这种情况下,在所有连接的手表设备上启动活动就可以了。
This example (sorry it's Kotlin) should work这个例子(对不起,它是 Kotlin)应该可以工作
val remoteActivityHelper =
RemoteActivityHelper(application, Dispatchers.IO.asExecutor())
val nodes = Wearable.getNodeClient(application).connectedNodes.await()
val nodeId = nodes.firstOrNull { it.displayName == "XXX" }?.id
if (nodeId == null) {
Toast.makeText(application, "No connected wear watch", Toast.LENGTH_SHORT).show()
} else {
try {
remoteActivityHelper.startRemoteActivity(
Intent(Intent.ACTION_VIEW)
.addCategory(Intent.CATEGORY_BROWSABLE)
.setData(
Uri.parse("https://www.bbc.co.uk/sounds/play/${programme.code}")
),
).await()
} catch (e: Exception) {
toaster.showToast("Unable to open mobile app: ${e.message}")
}
}
}
But the main thing in your example is that you are not checking the result of startRemoteActivity, it returns a ListenableFuture, so you could check for an error.但是您示例中的主要内容是您没有检查 startRemoteActivity 的结果,它返回一个 ListenableFuture,因此您可以检查错误。 In the example above, I'm using the.await() extension function which does the same thing.在上面的例子中,我使用了 .await() 扩展 function 来做同样的事情。
There are more complete examples in https://github.com/android/wear-os-samples/blob/d18c489ff415aa0fbb25c260e3aacdf50f7716e3/WearVerifyRemoteApp/Application/src/main/java/com/example/android/wearable/wear/wearverifyremoteapp/MainMobileActivity.kt https中有更完整的例子://github.com/android/wear-os-samples/blob/d18c489ff415aa0fbb25c260e3aacdf50f7716e3/WearVerifyRemoteApp/Application/src/main/java/com/example/android/wearable/wear/wearverifyremo.app吨
I've been fighting with exatly the same problem last two days.过去两天我一直在与同样的问题作斗争。 Works for me the next code:下一个代码对我有用:
Intent intent = new Intent(Intent.ACTION_VIEW) .addCategory(Intent.CATEGORY_BROWSABLE) .setData(Uri.parse(PLAY_STORE_APP_URI)); for (Node node : nodesWithoutApp) { RemoteActivityHelper remoteActivityHelper = new RemoteActivityHelper(this, Executors.newSingleThreadExecutor()); remoteActivityHelper.startRemoteActivity(intent, node.getId()); }
It didn't work with RemoteIntent.startRemoteActivity for some reason.由于某种原因,它不适用于 RemoteIntent.startRemoteActivity。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.