即使应用程序被终止,如何从手机应用程序启动Wear OS应用程序以开始锻炼?

问题描述 投票:0回答:1

我正在开发一个与 Wear OS 应用程序交互的 Android 应用程序。我想直接从手机应用程序在 Wear OS 应用程序上开始锻炼课程,即使 Wear OS 应用程序当前已被终止。具体来说,当用户在手机上发起锻炼时,它应该自动在 Wear OS 应用程序上打开并开始锻炼。
手机(开始锻炼)->wearos app:触发打开开始锻炼enter image description here

到目前为止我尝试过的:

  1. 数据层API:我研究了用于手机和Wear OS之间通信的数据层API,但我还没有找到在Wear OS应用程序被终止时启动它的方法。

  2. WearableListenerService:我已经实现了

    WearableListenerService
    来接收Wear OS应用程序上的消息,但我需要有关如何触发应用程序打开并开始锻炼的指导,即使它没有运行。

    // 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")
            }
    
android wear-os
1个回答
0
投票

我 80% 确定你可以使用数据层 api 来启动应用程序,即使它被杀死了,但无论如何......

另一种选择是使用远程活动助手在手表上启动意图。

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()); 
        }
    });
}
© www.soinside.com 2019 - 2024. All rights reserved.