Android Wearable.API已被弃用,我应该使用什么替代品?

7

我正在使用以下内容:

GoogleApiClient mApiClient = new GoogleApiClient.Builder(this)
            .addApi( Wearable.API )
...

自 Wearable.API 被弃用后,什么是适当的替代方案?

你找到解决方案了吗? - Rajesh Koshti
我还没有。我仍在使用已弃用的方法。(懒惰) - John Smith
2个回答

0

我在这里找到了答案: https://developer.android.com/training/wearables/data-layer/migrate-to-googleapi

将 Wear 应用迁移到 GoogleApi 从 Google Play 服务的 11.8.0 版本开始,Wear OS 应用应该迁移离开 GoogleApiClient 类,并改为使用基于 GoogleApi 类的客户端对象。

使用 GoogleApi 更容易设置异步操作。例如,在 Tasks API 的介绍中所述,您可以获取一个 Task 对象而不是 PendingResult 对象。


-1
我找到了一些好东西,很有帮助。
private class StartWearableActivityTask extends AsyncTask<Void, Void, Void> {

    final String key;

    public StartWearableActivityTask(String msg){
        key = msg;
    }


    @Override
    protected Void doInBackground(Void... args) {
        Collection<String> nodes = getNodes();
        for (String node : nodes) {
            sendStartActivityMessage(node,key);
        }
        return null;
    }
}

@WorkerThread
private Collection<String> getNodes() {
    HashSet<String> results = new HashSet<>();

    Task<List<Node>> nodeListTask =
            Wearable.getNodeClient(getApplicationContext()).getConnectedNodes();

    try {
        // Block on a task and get the result synchronously (because this is on a background
        // thread).
        List<Node> nodes = Tasks.await(nodeListTask);

        for (Node node : nodes) {
            results.add(node.getId());

        }

    } catch (ExecutionException exception) {
        Log.e(TAG, "Task failed: " + exception);

    } catch (InterruptedException exception) {
        Log.e(TAG, "Interrupt occurred: " + exception);
    }

    return results;
}

@WorkerThread
private void sendStartActivityMessage(String node,String event) {

    Task<Integer> sendMessageTask =
            Wearable.getMessageClient(this).sendMessage(node, event, new byte[0]);

    try {
        // Block on a task and get the result synchronously (because this is on a background
        // thread).
        Integer result = Tasks.await(sendMessageTask);


    } catch (ExecutionException exception) {
        Log.e(TAG, "Task failed: " + exception);

    } catch (InterruptedException exception) {
        Log.e(TAG, "Interrupt occurred: " + exception);
    }
}

1
一些解释? - Thecave3

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接