onMessageReceived仅在WearabeListenerService中调用。 如果我不使用服务,只需将我的Activity注册为MessageApi监听器 不调用onMessageReceived。 我将听众注册如下:
if (!mConnection.isConnected()) {
phoneConnectionError();
} else {
Wearable.MessageApi.addListener(mConnection, this).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Log.d("Viera","Add listener success: "+status.isSuccess());
}
});
}
状态总是成功。 我做错了什么?
编辑:根据要求,这里有更多活动代码:
Edit2:添加了更多代码
public class MainActivity extends WearableActivity
implements ResultCallback<MessageApi.SendMessageResult>, MessageApi.MessageListener {
private BoxInsetLayout mContainerView;
private String nodeId;
private GoogleApiClient mConnection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);
mContainerView = (BoxInsetLayout) findViewById(R.id.container);
mConnection = new GoogleApiClient.Builder(this)
.addApi(Wearable.API)
.build();
setAmbientEnabled();
retrieveDeviceNode();
}
@Override
public void onMessageReceived(MessageEvent messageEvent) {
Log.d("Viera","Recieved: "+messageEvent.getPath());
if (messageEvent.getPath().equals("tvException")) {
tvException();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
Wearable.MessageApi.removeListener(mConnection, this);
mConnection.disconnect();
}
private void afterNodeSearch() {
if (nodeId == null || !mConnection.isConnected()) {
phoneConnectionError();
} else {
Wearable.MessageApi.addListener(mConnection, this).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Log.d("Viera","Add listener success: "+status.isSuccess());
}
});
//... some other stuff here
}
}
// I know I should to this useig an other API but I know I am connected to the right node
// because the Id's here and on mobile device matches
private void retrieveDeviceNode() {
findViewById(R.id.loading).setVisibility(View.VISIBLE);
findViewById(R.id.content).setVisibility(View.GONE);
new Thread(new Runnable() {
@Override
public void run() {
mConnection.blockingConnect();
NodeApi.GetConnectedNodesResult result =
Wearable.NodeApi.getConnectedNodes(mConnection).await();
List<Node> nodes = result.getNodes();
if (nodes.size() > 0) {
nodeId = nodes.get(0).getId();
}
afterNodeSearch();
}
}).start();
}
@Override
public void onResult(MessageApi.SendMessageResult sendMessageResult) {
if (!sendMessageResult.getStatus().isSuccess()) {
phoneConnectionError();
} else {
Log.d("Viera", "Send successfull!");
}
}
public void onClick(View v) {
if (nodeId == null) {
retrieveDeviceNode();
} else if (!mConnection.isConnected()) {
connect();
} else {
String command = v.getTag().toString();
sendCommand(COMMAND, command);
}
}
private final static String COMMAND = "onCommand", START_APP = "startApp";
private void sendCommand(final String command, final String msg) {
if (nodeId != null) {
new Thread(new Runnable() {
@Override
public void run() {
if (mConnection.isConnected()) {
byte[] msgBytes = null;
if (msg != null) msg.getBytes();
Wearable.MessageApi.sendMessage(mConnection, nodeId, command, msgBytes).setResultCallback(MainActivity.this);
} else {
phoneConnectionError();
}
}
}).start();
}
}
}
答案 0 :(得分:1)
好的,我终于找到了问题:
我使用的是错误的钥匙!可穿戴设备和移动应用程序的签名是不同的!
它发现,当发送消息时,我总是会在WerableListenerService中被通知,即使是使用Wearable.MessageApi.addListener注册的监听器中不同的签名但不是。
这意味着我的代码是正确的,但我的建筑错了!
答案 1 :(得分:0)
根据您提供的代码,您正在构建一个GoogleApiClient(您称之为mConnection),但您从未连接过它(即从未调用mConnection.connect())。此外,在构建GoogleApiClient时,还需要定义两个侦听器(可能是您的活动);一个用于接收成功的连接回调,一个用于接收故障。在成功连接的那个中,有一个onConnected()回调,您应该使用它来添加MessageApi监听器等。如果您在GitHub仓库中查看Wear的任何示例项目,您将看到所有这些都可以做到。