Android Wearable.API已弃用。我应该用什么呢?

时间:2018-01-04 16:46:07

标签: android

我正在使用以下内容:

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

由于Wearable.API已被弃用?什么是合适的替代品?

2 个答案:

答案 0 :(得分:0)

我找到了一些有用的东西

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 :(得分: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对象。