简体   繁体   中英

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)? 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. I left it as zero because the documentation says:

nodeId String: Wear OS node id for the device where the activity should be started. If null, and the current device is a watch, the activity will start on the companion phone device. Otherwise, the activity will start on all connected watch devices.

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

I could determine the nodeId, but it seems difficult to do. Plus in this case, starting the activity on all connected watch devices would be fine.

This example (sorry it's Kotlin) should work


            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. In the example above, I'm using the.await() extension function which does the same thing.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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