go3d
go3d

Reputation: 463

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.

Is it possible to download a file using DownloadManager?

Upvotes: 3

Views: 1516

Answers (2)

Yuri Schimke
Yuri Schimke

Reputation: 13488

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'm not sure about the exact implementation for Java, it's really messy with the Task and Future APIs mixed here. Maybe

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

Upvotes: 2

OlegMalakhov
OlegMalakhov

Reputation: 53

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.

Upvotes: 1

Related Questions