0

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.

starkshang
  • 8,228
  • 6
  • 41
  • 52
tomss
  • 361
  • 3
  • 13

5 Answers5

1

Be sure your Thread is terminated cus it seems you are sending many toasts that appear like one.

Daniel Givoni
  • 665
  • 6
  • 12
1

TOAST IN SERVICE? HAHA THIS IS YOUR SOLUTION:

new Handler(Looper.getMainLooper()).post(new Runnable() {
            @Override
            public void run() {
                    Toast.makeText(getApplicationContext(), "Lumos Maxima", Toast.LENGTH_LONG).show();
            }
        });
Lucas
  • 431
  • 3
  • 7
0

You use this also:

Toast.makeText(Activityname.this, "message", Toast.LENGTH_SHORT).show();
0

You use this also:

Toast.makeText(Activityname.this, "message", Toast.LENGTH_SHORT).show();

Or if it is outside the class, you'll need to get your activity context (pass it in the constructor etc.)

How to use Toast when I cant use "this" as context

Community
  • 1
  • 1
ʍѳђઽ૯ท
  • 16,646
  • 7
  • 53
  • 108
0

Try next code:

Toast.makeText(getApplicationContext(), "Your Message", Toast.LENGTH_LONG).show();
gio
  • 4,950
  • 3
  • 32
  • 46