如何在手表上远程打开Google从phone播放(佩戴OS)

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

从手表phone远程打开Google播放的正确方法是什么(佩戴OS)?我正在尝试这个:

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: Wear OS 设备的节点 ID,activity 应该在其中启动。如果为空,并且当前设备是手表,activity 将在配套 phone 设备上启动。否则,activity 将在所有连接的手表设备上启动。

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

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

这个例子(抱歉是 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 的结果,它 returns 一个 ListenableFuture,因此您可以检查错误。在上面的例子中,我使用了 .await() 扩展函数,它做同样的事情。

https://github.com/android/wear-os-samples/blob/d18c489ff415aa0fbb25c260e3aacdf50f7716e3/WearVerifyRemoteApp/Application/src/main/java/com/example/android/wearable/wear/wearverifyremoteapp/MainMobileActivity.kt

中有更完整的示例

我不确定 Java 的确切实现,这里混杂了 Task 和 Future API,这真的很混乱。也许

        RemoteActivityHelper remoteActivityHelper = new RemoteActivityHelper(application, executor);

        NodeClient client = Wearable.getNodeClient(application);
        client.getConnectedNodes().addOnSuccessListener(nodes -> {
                if (nodes.size() > 0) {
                    String nodeId = nodes.get(0).getId();
                    ListenableFuture<Void> result = remoteActivityHelper.startRemoteActivity(
                            new Intent(Intent.ACTION_VIEW)
                                    .addCategory(Intent.CATEGORY_BROWSABLE)
                                    .setData(
                                            Uri.parse("https://www.bbc.co.uk/sounds/play/${programme.code}")
                                    )
                            , nodeId);
                    result.addListener(() -> {
                        try {
                            result.get();
                        } catch (Exception e) {
                            Toast.makeText(application, "Failed " + e, Toast.LENGTH_SHORT).show();
                        }
                    }, executor);
                } else {
                    Toast.makeText(application, "No connected wear watch", Toast.LENGTH_SHORT).show();
                }
        }).addOnFailureListener(failure -> {
            Toast.makeText(application, "Unable to open mobile app: ${e.message}", Toast.LENGTH_SHORT).show();
        });

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


    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 一起使用。