I have made an application that sends messages from my smartphone to my smartwatch.
This is the class where I create a Message that I can send to smartwatch.
public class Message implements GoogleApiClient.ConnectionCallbacks{
private String message;
private Application app;
public Message(String message, Application app){
this.message=message;
this.app = app;
}
public void sendMessage() {
new Thread( new Runnable() {
@Override
public void run() {
GoogleApiClient clientApi = new GoogleApiClient.Builder(aplicacao.getApplicationContext())
.addApiIfAvailable( Wearable.API )
.build();
clientApi.connect();
NodeApi.GetConnectedNodesResult nodes = Wearable.NodeApi.getConnectedNodes( clientApi ).await();
if(nodes.getNodes().isEmpty())
Log.w("No signal!","No signal!");
else {
for (Node node : nodes.getNodes()) {
Wearable.MessageApi.sendMessage(clientApi, node.getId(), message, message.getBytes()).await();
}
}
clientApi.disconnect();
}
}).start();
}
@Override
public void onConnected(Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
}
When I want to send a Message to smartwatch I use this two lines of code:
Message message = new Message("Message", getApplication());
message.sendMessage();
I made this service on my smartwatch application to receive messages from smartphone. When I receive a message I show a Toast with the text of that message:
public class ReceiveMessages extends WearableListenerService {
@Override
public void onMessageReceived(MessageEvent event) {
String message = event.getPath();
showMessages(message);
}
private void showMessages(String message) {
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();
}
}
The smartwatch is receiving the message and shows the text of the message correctly, it does not disappear after Toast.LENGTH_SHORT
.
I want to know whether there is any problem in my code (I don't have any infinite loop).
Thanks.