简体   繁体   English

如何检测手表中是否已安装我的Android应用的Wear应用

[英]How to detect if the Wear app of my Android app is already installed in the watch

Is it somehow possible to detect if the Wear mini app inside an Android app is already installed in the watch? 是否可以通过某种方式检测手表中是否已安装Android应用程序中的Wear mini应用程序?

I have an app which cannot be used on the phone until the Wear part is installed in the watch, so I want to block all interaction until then. 我有一个在手表中安装了Wear部件之前无法在手机上使用的应用程序,因此我想在此之前阻止所有交互。

What about app updates, is it possible to detect if the Wear part was already updated? 关于应用程序更新,是否可以检测磨损部分是否已更新?

EDIT: 编辑:

It looks like the Data API and even Message API calls are buffered and delivered after the app is installed. 安装该应用程序后,似乎会缓冲并传递数据API甚至消息API调用。 This however does not solve the issue with app updates. 但是,这不能解决应用程序更新的问题。 That is solvable with the accepted answer. 用公认的答案可以解决。

AFAIK, there is no out-of-the-box solution to do it. AFAIK,没有开箱即用的解决方案。

If your Wear app does not have activities (and therefore no means to be started by user), what you can do is send something like IS_INSTALLED message to Wear periodically while handheld app is in foreground until Wear won't put it version number into data layer. 如果您的Wear应用没有活动(因此无法由用户启动),您可以做的是在掌上应用处于前台时定期向Wear发送类似IS_INSTALLED消息,直到Wear不会将其版本号放入数据中层。 On application update you can check for version number in data layer and if it's lower than current version - repeat the procedure. 在应用程序更新时,您可以检查数据层中的版本号,以及它是否低于当前版本-重复此过程。

This approach will as well solve problem with Wear device not being connected (or out-of-range which is essentially the same). 这种方法也将解决Wear设备未连接(或超出范围,基本相同)的问题。

One Solution is to use CapabilityClient( https://developers.google.com/android/reference/com/google/android/gms/wearable/CapabilityClient ). 一种解决方案是使用CapabilityClient( https://developers.google.com/android/reference/com/google/android/gms/wearable/CapabilityClient )。 First you can detect whether the Wearable and phone are connected or not using NodeClient( https://developers.google.com/android/reference/com/google/android/gms/wearable/NodeClient ). 首先,您可以使用NodeClient( https://developers.google.com/android/reference/com/google/android/gms/wearable/NodeClient )检测可穿戴设备和手机是否已连接。 Below I have mentioned the code to detect whether the watch is connected to phone or not in android. 下面我提到了用于检测手表是否已在Android中连接到手机的代码。

    Task<List<Node>> nodesTask = Wearable.getNodeClient(MainMobileActivity.this)
            .getConnectedNodes();
    nodesTask.addOnSuccessListener(new OnSuccessListener<List<Node>>() {

        @Override
        public void onSuccess(List<Node> nodes) {
            nodeSize = nodes.size();
            for (Node node : nodes) {
                Wearable.getMessageClient(MainMobileActivity.this)
                        .sendMessage(node.getId(), MESSAGE_PATH, "Hello from AndroidWear".getBytes());
            }

            Log.d("Hello" , "Message sent to Cordova");
        }
    });

So, nodeSize tells how many nodes/watches are connected. 因此,nodeSize告诉您连接了多少个节点/监视。

Wearable.getMessageClient(MainMobileActivity.this)
                        .sendMessage(node.getId(), MESSAGE_PATH, "Hello from AndroidWear".getBytes());

This piece of code helps to send the message from phone to watch. 这段代码有助于从手机发送消息以进行观看。 Now coming to detect whether the watch has the application or not. 现在开始检测手表是否具有该应用程序。 Below is the mentioned code for it. 下面是提到的代码。

    Task<CapabilityInfo> capabilityTask = Wearable.getCapabilityClient(this)
            .getCapability(CAPABILITY_WEAR_APP, CapabilityClient.FILTER_REACHABLE);
    capabilityTask.addOnSuccessListener(new OnSuccessListener<CapabilityInfo>() {
        @Override
        public void onSuccess(CapabilityInfo capabilityInfo) {
            mWearNodesWithApp = capabilityInfo.getNodes();

        }
    });

So, if mWearNodesWithApp comes as 0 it shows that app is not installed and if it shows 1 it means application is installed. 因此,如果mWearNodesWithApp为0,则表示未安装该应用程序;如果显示1,则表示已安装该应用程序。

CAPABILITY_WEAR_APP should be of String type and should have the value which you mentioned in wear.xml of wear application and not of phone. CAPABILITY_WEAR_APP应该是String类型,并且应该具有您在Wear应用程序而不是手机的wear.xml中提到的值。 Do remember to mention the same applicationId for both Phone and Wear application. 切记为Phone和Wear应用程序提及相同的applicationId。

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

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