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

esyap4oy  于 2022-12-16  发布在  Android
关注(0)|答案(2)|浏览(204)

从手表(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,我将其保留为零,因为文档中说:

节点ID字符串:应在其中启动活动的设备的Wear OS节点ID。如果为空,并且当前设备是手表,则活动将在配套手机设备上启动。否则,活动将在所有连接的手表设备上启动。

图片来源:https://developer.android.com/reference/com/google/android/wearable/intent/RemoteIntent#startremoteactivity
我可以确定nodeId,但似乎很难做到。另外,在这种情况下,在所有连接的手表设备上启动活动将是好的。
是否可以使用DownloadManager下载文件?

gzszwxb4

gzszwxb41#

这个例子(抱歉是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()扩展函数来做同样的事情。
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();
        });
vm0i2vca

vm0i2vca2#

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

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一起工作。

相关问题