我为表盘构建了一个配置活动。 Android Wear会显示并启动配置活动。
是否可以从配套应用启动配置活动?
我创建了一个活动,但无法传递Android Wear活动似乎在启动配置活动时传递的EXTRA_PEER_ID。
答案 0 :(得分:0)
我可以在How to detect if android device is paired with android wear watch的帮助下完成此操作
EXTRA_PEER_ID只是连接的磨损设备的nodeId
,可以使用下面的代码检索。
然后,您可以使用Intent.putExtra(WatchFaceCompanion.EXTRA_PEER_ID, nodeId);
并通过意图传递ID并启动配置活动。
代码:
private GoogleApiClient client;
private static final long CONNECTION_TIME_OUT_MS = 1000;
public void checkIfWearableConnected() {
retrieveDeviceNode(new Callback() {
@Override
public void success(String nodeId) {
Toast.makeText(LaunchActivity.this, "There was at least one wearable found", Toast.LENGTH_SHORT).show();
}
@Override
public void failed(String message) {
Toast.makeText(LaunchActivity.this, "There are no wearables found", Toast.LENGTH_SHORT).show();
}
});
}
private GoogleApiClient getGoogleApiClient(Context context) {
if (client == null)
client = new GoogleApiClient.Builder(context)
.addApi(Wearable.API)
.build();
return client;
}
private interface Callback {
void success(final String nodeId);
void failed(final String message);
}
private void retrieveDeviceNode(final Callback callback) {
final GoogleApiClient client = getGoogleApiClient(this);
new Thread(new Runnable() {
@Override
public void run() {
client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
NodeApi.GetConnectedNodesResult result =
Wearable.NodeApi.getConnectedNodes(client).await();
List<Node> nodes = result.getNodes();
if (nodes.size() > 0) {
String nodeId = nodes.get(0).getId();
callback.success(nodeId);
} else {
callback.failed("no wearables found");
}
client.disconnect();
}
}).start();
}