我试图通过从watch发送触发信号在Android中执行命令。我的手机部分如下所示。
package com.example.myapplication;
import android.app.Service;
import android.util.Log;
import com.google.android.gms.wearable.MessageApi;
import com.google.android.gms.wearable.MessageEvent;
public abstract class WearableListenerServiceForWear
extends Service implements MessageApi.MessageListener {
private static final String HELLO_WORLD_WEAR_PATH = "/hello-world-wear";
@Override
public void onMessageReceived(MessageEvent messageEvent) {
if (messageEvent.getPath().equals(HELLO_WORLD_WEAR_PATH)) {
try {
command
} catch (Exception ex) {
ex.printStackTrace();
}
;//null pointer check in case package name was not found
} else {
Log.e("TAG",
"Failed to send message with status code: ");
}
}
}
在手机部分我使用此代码发送消息:
private void sendMessage() {
if (mNode != null && mGoogleApiClient != null && mGoogleApiClient.isConnected()) {
Wearable.MessageApi.sendMessage(mGoogleApiClient, mNode.getId(), HELLO_WORLD_WEAR_PATH, null)
.setResultCallback(new ResultCallback<MessageApi.SendMessageResult>() {
@Override public void onResult(MessageApi.SendMessageResult sendMessageResult) {
if (!sendMessageResult.getStatus().isSuccess()) {
Log.e("TAG",
"Failed to send message with status code: " + sendMessageResult.getStatus()
.getStatusCode());
}
}
});
} else {
//Improve your code
}
}
没有错误显示。这两个应用程序在手表和手机上运行良好。但命令没有触发。代码有什么问题吗?