简体   繁体   English

消息从可穿戴到电话

[英]Message from Wearable to Phone

I'm making an app to wear that simply send a message to the phone to show a Toast, and for now all is working, but i have a problem, it only sends messages when user interacts with the Wearable and I want that my app send a message automatically. 我正在制作一个要佩戴的应用程序,只需将消息发送到手机以显示Toast,现在一切正常,但是我有一个问题,它仅在用户与可穿戴设备互动时发送消息,并且我希望我的应用程序自动发送消息。

The problem is that if i call the method sendToast() in the onCreate method the Boolean variable nodeId inside the method sendToast() is null, but if I do click to the button once everithing is created it works perfectly. 问题是,如果我在onCreate方法中调用方法sendToast(),则方法sendToast()内的布尔变量nodeId为null,但是如果在创建完所有按钮后单击按钮,它将可以正常工作。

I suspect that if I call sendToast directly the Thread is not finished and in that precisly moment i don't have the node, but I don't know how to solve it. 我怀疑如果直接调用sendToast,则线程尚未完成,并且恰好在那一刻我没有该节点,但我不知道如何解决该问题。

I also tryed some tricks like those, but all failed so far: 我还尝试了一些类似的技巧,但到目前为止都失败了:

Call the method sendToast() inside the method setupWidgets(). 在方法setupWidgets()中调用方法sendToast()。

Simulate the click creating an instance of the button and using the method .callOnClick() or performClick() but since i'm using WatchViewStub(to work with round and squear wearables) i can't acces to it, it give me NullPointerException 模拟单击以创建按钮实例并使用.callOnClick()或performClick()方法,但是由于我使用的是WatchViewStub(用于圆形和方形可穿戴设备),我无法访问它,它给了我NullPointerException

Call a timer before calling the method to delay the execution. 在调用方法以延迟执行之前,先调用计时器。

Nothing works so far, anyone can help me? 到目前为止没有任何效果,有人可以帮助我吗?

Thank you so much!! 非常感谢!!

Here is my code: 这是我的代码:

public class Main extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        initApi();
        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {

            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                setupWidgets();
            }
        });
    }

    private void initApi() {
        client = getGoogleApiClient(this);
        retrieveDeviceNode();
    }

    private void setupWidgets() {
        clickButton = (Button) findViewById(R.id.btn_toast);
        clickButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                sendToast();
            }
        });
    }

    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() {
                client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
                NodeApi.GetConnectedNodesResult result =
                        Wearable.NodeApi.getConnectedNodes(client).await();
                List<Node> nodes = result.getNodes();
                if (nodes.size() > 0) {
                    nodeId = nodes.get(0).getId();
                }
                client.disconnect();
            }
        }).start();
    }

    private void sendToast() {
        if (nodeId != null) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    client.blockingConnect(CONNECTION_TIME_OUT_MS, TimeUnit.MILLISECONDS);
                    Wearable.MessageApi.sendMessage(client, nodeId, GET_CONTACTS, null);
                    client.disconnect();
                }
            }).start();
        }
    }
}

If you want to send a message from onCreate, you actually want to do this (this is not a compiling example, but should give you an idea): 如果要从onCreate发送消息,则实际上是要这样做(这不是一个编译示例,但应该可以给您一个提示):

NodeApi.getConnectedNodes(client).setResultCallback(new ResultCallback<..>(List<Node> nodes) {
    String nodeId = nodes.get(0).getId();
    Wearable.MessageApi.sendMessage(client, nodeId, GET_CONTACTS, null);
}, TIMEOUT);

So, basically instead of waiting on separate thread, add a callback and when it returns, send the message. 因此,基本上不必等待单独的线程,而应添加回调并在返回时发送消息。 This way you should be able to immediately schedule a message directly from onCreate . 这样,您应该能够立即直接从onCreate安排消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM