我正在开发一个与 Wear OS 应用程序交互的 Android 应用程序。我想直接从手机应用程序在 Wear OS 应用程序上开始锻炼课程,即使 Wear OS 应用程序当前已被终止。具体来说,当用户在手机上发起锻炼时,它应该自动在 Wear OS 应用程序上打开并开始锻炼。
手机(开始锻炼)->wearos app:触发打开开始锻炼
数据层API:我研究了用于手机和Wear OS之间通信的数据层API,但我还没有找到在Wear OS应用程序被终止时启动它的方法。
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")
}
我 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());
}
});
}