繁体   English   中英

如何在手表上远程从手机打开 Google Play(Wear OS)

[英]How to open Google Play from the phone remotely on the watch (Wear OS)

在手表(Wear OS)上从手机远程打开 Google Play 的正确方法是什么? 我正在尝试这个:

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);

但什么也没有发生。

最后一个参数是nodeId。 我将其保留为零,因为文档说:

nodeId String:应启动 Activity 的设备的 Wear OS 节点 ID。 如果 null,并且当前设备是手表,则活动将在配套手机设备上启动。 否则,活动将在所有连接的手表设备上启动。

资料来源: https://developer.android.com/reference/com/google/android/wearable/intent/RemoteIntent#startremoteactivity

我可以确定nodeId,但似乎很难做到。 另外在这种情况下,在所有连接的手表设备上启动活动就可以了。

这个例子(对不起,它是 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}")
                }
            }
        }

但是您示例中的主要内容是您没有检查 startRemoteActivity 的结果,它返回一个 ListenableFuture,因此您可以检查错误。 在上面的例子中,我使用了 .await() 扩展 function 来做同样的事情。

https中有更完整的例子://github.com/android/wear-os-samples/blob/d18c489ff415aa0fbb25c260e3aacdf50f7716e3/WearVerifyRemoteApp/Application/src/main/java/com/example/android/wearable/wear/wearverifyremo.app吨

过去两天我一直在与同样的问题作斗争。 下一个代码对我有用:


    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());
    }

由于某种原因,它不适用于 RemoteIntent.startRemoteActivity。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM