java – 从可穿戴到手机发送消息,然后立即回复

前端之家收集整理的这篇文章主要介绍了java – 从可穿戴到手机发送消息,然后立即回复前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我一直在和 Android Wear Message API进行战斗,终于可以接受我需要一些帮助.

我的应用非常简单. Mobile部分由一个MainActivity组成(除了显示“Hello world”和一个可扩展WearableListenerService的服务之外,它只做了一个MainActivity,而Wear部分只是一个MainActivity,并且实现了MessageApi.MessageListener.

这个想法很简单:按下Wear装置上的按钮,该按钮向Mobile发送消息.当移动台接收到该消息时,它将显示具有发送者的消息路径(例如/移动)的Toast.这样做之后,Mobile应该使用我的reply()方法将消息发回Wear装置.所有我想要做的是记录此消息.

我可以实现第一部分完美的罚款.按下按钮后,手机会弹出一个Toast说“/ mobile”.然而,答复似乎只是迷失在了一个;没有错误,但也没有消息.

有人可以帮助我了解我在做错什么吗?我已将我的文件粘贴到下面.

This是我正在关注的教程.干杯!

穿:MainActivity.java

package org.thecosmicfrog.toastdroidmessageapitutorial;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.wearable.view.WatchViewStub;
import android.util.Log;
import android.view.View;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.MessageApi;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.Node;
import com.google.android.gms.wearable.NodeApi;
import com.google.android.gms.wearable.Wearable;

import java.util.List;
import java.util.concurrent.TimeUnit;

public class MainActivity extends Activity implements MessageApi.MessageListener {

    private final String LOG_TAG = MainActivity.class.getSimpleName();

    private static final long CONNECTION_TIME_OUT_MS = 100;
    private static final String MOBILE_PATH = "/mobile";

    private GoogleApiClient googleApiClient;
    private String nodeId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initGoogleApiClient();

        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                initWidgets();
            }
        });
    }

    private void initGoogleApiClient() {
        googleApiClient = getGoogleApiClient(this);
        retrieveDeviceNode();
    }

    private GoogleApiClient getGoogleApiClient(Context context) {
        return new GoogleApiClient.Builder(context)
                .addApi(Wearable.API)
                .build();
    }

    private void retrieveDeviceNode() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                if (googleApiClient != null && !(googleApiClient.isConnected() || googleApiClient.isConnecting()))
                    googleApiClient.blockingConnect(CONNECTION_TIME_OUT_MS,TimeUnit.MILLISECONDS);

                NodeApi.GetConnectedNodesResult result =
                        Wearable.NodeApi.getConnectedNodes(googleApiClient).await();

                List<Node> nodes = result.getNodes();

                if (nodes.size() > 0)
                    nodeId = nodes.get(0).getId();

                Log.v(LOG_TAG,"Node ID of phone: " + nodeId);

                googleApiClient.disconnect();
            }
        }).start();
    }

    private void initWidgets() {
        findViewById(R.id.button_toast).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendToast();
            }
        });
    }

    private void sendToast() {
        if (nodeId != null) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    if (googleApiClient != null && !(googleApiClient.isConnected() || googleApiClient.isConnecting()))
                        googleApiClient.blockingConnect(CONNECTION_TIME_OUT_MS,TimeUnit.MILLISECONDS);

                    Wearable.MessageApi.sendMessage(googleApiClient,nodeId,MOBILE_PATH,null).await();
                    googleApiClient.disconnect();
                }
            }).start();
        }
    }

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        Log.v(LOG_TAG,"In onMessageReceived()");

        if (messageEvent.getPath().equals("/wear")) {
            Log.v(LOG_TAG,"Success!");
        }
    }
}

手机:ListenerService.java

package org.thecosmicfrog.toastdroidmessageapitutorial;

import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.wearable.MessageEvent;
import com.google.android.gms.wearable.Wearable;
import com.google.android.gms.wearable.WearableListenerService;

import java.util.concurrent.TimeUnit;

public class ListenerService extends WearableListenerService {

    private final String LOG_TAG = ListenerService.class.getSimpleName();

    private static GoogleApiClient googleApiClient;

    private static final long CONNECTION_TIME_OUT_MS = 100;
    private static final String WEAR_PATH = "/wear";
    private String nodeId;

    @Override
    public void onMessageReceived(MessageEvent messageEvent) {
        if (messageEvent.getPath().equals("/mobile")) {
            nodeId = messageEvent.getSourceNodeId();
            Log.v(LOG_TAG,"Node ID of watch: " + nodeId);
            showToast(messageEvent.getPath());

            reply(WEAR_PATH);
        }
    }

    private void showToast(String message) {
        Toast.makeText(this,message,Toast.LENGTH_LONG).show();
    }

    private void reply(final String path) {
        googleApiClient = new GoogleApiClient.Builder(getApplicationContext())
                .addApi(Wearable.API)
                .build();

        Log.v(LOG_TAG,"In reply()");
        Log.v(LOG_TAG,"Path: " + path);

        if (googleApiClient != null && !(googleApiClient.isConnected() || googleApiClient.isConnecting()))
            googleApiClient.blockingConnect(CONNECTION_TIME_OUT_MS,TimeUnit.MILLISECONDS);

        Wearable.MessageApi.sendMessage(googleApiClient,path,null).await();
        googleApiClient.disconnect();
    }
}

Mobile MainActivity非常简单,所以为了清楚起见,我已经把它留下了.

解决方法@H_404_24@
您从不在您的Wear活动中调用 MessageApi.addListener(),因此您的MessageListener从未注册接收邮件.当您的活动被销毁时,您也应该致电 MessageApi.removeListener().

注意:两种方法都需要连接的GoogleApiClient.如果您在整个活动期间保留GoogleApiClient,而不是尝试在onDestroy()中连接/ removeListener()/ disconnect,则可能会使逻辑更容易.

猜你在找的Java相关文章