簡體   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