吐司不会消失

时间:2016-01-26 09:53:27

标签: android

我制作了一个应用程序,可以将智能手机上的消息发送到我的智能手表。

这是我创建一个可以发送给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) {
}
}

当我想向智能手表发送消息时,我使用以下两行代码:

Message message = new Message("Message", getApplication());
message.sendMessage();

我在智能手表应用程序上创建了此服务,以便从智能手机接收消息。 当我收到一条消息时,我会用该消息的文本显示Toast:

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();
}
}

智能手表正在接收消息并正确显示消息文本,但Toast.LENGTH_SHORT后消息不会消失。 我想知道我的代码中是否有任何问题(我没有任何无限循环)。 感谢。

5 个答案:

答案 0 :(得分:1)

确保你的线程被终止cus似乎你发送了许多看起来像一个的toast。

答案 1 :(得分:1)

在服务中?哈哈,这是你的解决方案:

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

答案 2 :(得分:0)

你也使用它:

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

答案 3 :(得分:0)

你也使用它:

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

或者如果它在课外,你需要获得你的活动上下文(在构造函数中传递它等)。

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

答案 4 :(得分:0)

尝试下一个代码:

Toast.makeText(getApplicationContext(), "Your Message", Toast.LENGTH_LONG).show();
相关问题