Steve Bui
Steve Bui

Reputation: 53

How to launch Wear OS app from Phone app to start a workout even when the app is killed?

I'm developing an Android application that interacts with a Wear OS app. I want to start a workout session on the Wear OS app directly from the phone app, even if the Wear OS app is currently killed. Specifically, when a user initiates a workout on the phone, it should automatically open and start the workout on the Wear OS app.
Phone( start a workout) -> wearos app: trigger open start a workoutenter image description here

What I have tried so far:

  1. Data Layer API: I've looked into the Data Layer API for communication between the phone and Wear OS, but I haven't found a way to launch the Wear OS app when it is killed.

  2. WearableListenerService: I've implemented a WearableListenerService to receive messages on the Wear OS app, but I need guidance on how to trigger the app to open and start a workout, even if it is not running.

    // Code to send a message to Wear OS app
    val node = nodes[0]
    val payload = "start_workout".toByteArray()
    Wearable.getMessageClient(context).sendMessage(node.id, "/start_workout", payload)
            .addOnSuccessListener {
                Log.d("TAG", "Message sent successfully")
            }
            .addOnFailureListener {
                Log.d("TAG", "Failed to send message")
            }
    

Upvotes: 1

Views: 162

Answers (2)

claired4l
claired4l

Reputation: 64

You can try to start the activity as an Intent in your WearableListenerService.

class MyListenerService : WearableListenerService() {
override fun onMessageReceived(messageEvent: MessageEvent) {
    if (messageEvent.path == ACTION_PATH) {
        val startIntent = Intent(this, MainActivity::class.java)
        startIntent.addFlags(
            Intent.FLAG_ACTIVITY_NEW_TASK
        )

        startActivity(startIntent)
    }
}

Upvotes: 0

dect
dect

Reputation: 563

I'm 80% sure you can use the data layer api to launch the app even if it is killed, but anyway...

Another option is to use a remote Activity Helper to launch an intent on the watch.

private Node NODES;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    setContentView(R.layout.main);

    new Thread(() -> {
        Task<List<Node>> nodeListTask = Wearable.getNodeClient(getApplicationContext()).getConnectedNodes();

        try {
            NODES = Tasks.await(nodeListTask);
        } catch (Exception ignore) {}
    }).start();

    findViewById(R.id.button_id_here).setOnClickListener((l) -> {
        // the variable "i" would be the intent of you exercise app

        RemoteActivityHelper remoteActivityHelper = new RemoteActivityHelper(this, Executors.newSingleThreadExecutor());

        for(Node n : NODES) {
    remoteActivityHelper.startRemoteActivity(i, n.getId()); 
        }
    });
}

Oh, and you will need these dependencies:

implementation 'com.google.android.gms:play-services-wearable:18.0.0'
implementation 'androidx.wear:wear-remote-interactions:1.0.0

Upvotes: 1

Related Questions