Prompting the installation for your Wear OS companion app from your Android app

descriptionStandard

The following function is a example on how to use a RemoteActivityHelper to trigger connected Wear OS devices to open the Play Store at a desired package. This allows both the app author to increase exposure to their companion app but also allow the user to more easily access the companion applications.

An implementation of this can be found in the 1.10.02 (Android) update to my speech to text application: Transcribable

There’s a few ways to achieve something similar; all of which are depreciated with no clear replacement yet (please correct me…):

// Required import
import androidx.wear.remote.interactions.RemoteActivityHelper;


/**
 * Attempts to start a remote activity on connected Wear OS device(s) that opens the apps Wear OS Play Store listing.
 *
 * This method retrieves a list of connected Wear OS devices and attempts to launch
 * the Play Store listing for the current app (package name) on each node (device).
 *
 * @param context The context of the calling activity or application.
 *
 * @throws SecurityException when app doesn't have the necessary permissions.
 * @throws IllegalArgumentException when context is null.
 *
 * @see Wearable
 * @see RemoteActivityHelper
 */
private void startRemoteActivity(Context context) {
    // Assuming the package on the Playstore for your Wear OS app shares the same name
    // or the package as a string: com.oddineers.transcribable
    String package = "market://details?id=" + getPackageName(); 
    Executor executor = Executors.newSingleThreadExecutor();

    // Gets all connected wearos devices (nodes)
    Task> nodeListTask = Wearable.getNodeClient(context).getConnectedNodes();

    nodeListTask.addOnSuccessListener(nodes -> {
        if (!nodes.isEmpty()) {
            for (Node node : nodes) {
                // Create RemoteActivityHelper instance
                RemoteActivityHelper remoteActivityHelper = new RemoteActivityHelper(context, executor);

                // Create the intent to open the Play Store listing
                Intent intent = new Intent(Intent.ACTION_VIEW)
                        .addCategory(Intent.CATEGORY_BROWSABLE)
                        .setData(Uri.parse(package));

                // Start the remote activity on the Wear OS device
                ListenableFuture result = remoteActivityHelper.startRemoteActivity(intent, node.getId());
                result.addListener(() -> {
                    // Success message
                    Toast.makeText(context, "Remote activity started successfully", Toast.LENGTH_SHORT).show();
                }, ContextCompat.getMainExecutor(context));
            }
        }
    });

    // Failure message example
    nodeListTask.addOnFailureListener(nodes -> {    
        Toast.makeText(context, "No devices appear to be connected", Toast.LENGTH_SHORT).show();
    });
}
wearOSRemoteActivity.javaView rawView file on GitLab

Hopefully someone finds this helpful!

Leave a Reply

I'm pleased you have chosen to leave a comment and engage in a meaningful conversation. Please keep in mind that comments are moderated according to our privacy policy, links are set to nofollow.

Your email address will not be published. Required fields are marked *