如何在附近的新播放服务中实现抽象类EndpointDiscoveryListener和ConnectionRequestListener?

时间:2017-02-21 03:25:40

标签: java android deprecated google-play-games google-nearby

我将附近的播放服务更新为版本&#;; 10.2.0',它将EndpointDiscoveryListener和ConnectionRequestListener从接口更改为抽象类,我使用EndpointDiscoveryListener扩展NearbyClient并声明内部类ConnectionRequestListener,现在我看到了AppIdentifier也被弃用,我在谷歌搜索了很多,但我找不到任何新的例子, 这是我从github playgameservices更改的代码:

public class NearbyClient extends Connections.EndpointDiscoveryListener implements
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        Connections.MessageListener {


    private class OnConnectionRequest extends Connections.ConnectionRequestListener {

    private NearbyClient mNearbyClient;

    OnConnectionRequest(NearbyClient nearbyClient)
    {
        this.mNearbyClient = nearbyClient;
    }

    @Override
    public void onConnectionRequest(final String remoteEndpointId, final String remoteEndpointName, byte[] payload) {
        Log.d(TAG, "onConnectionRequest:" + remoteEndpointId +
                ":" + remoteEndpointName);

        if (mIsHost) {
            // The host accepts all connection requests it gets.
            byte[] myPayload = null;
            Nearby.Connections.acceptConnectionRequest(mGoogleApiClient, remoteEndpointId,
                    myPayload, mNearbyClient).setResultCallback(new ResultCallback<Status>() {
                @Override
                public void onResult(Status status) {
                    Log.d(TAG, "acceptConnectionRequest:" + status + ":" + remoteEndpointId);
                    if (status.isSuccess()) {
                        Toast.makeText(mContext, "Connected to " + remoteEndpointName,
                                Toast.LENGTH_SHORT).show();

                        // Record connection
                        HeroParticipant participant = new HeroParticipant(remoteEndpointId, remoteEndpointName);
                        mConnectedClients.put(remoteEndpointId, participant);

                        // Notify listener
                        mListener.onConnectedToEndpoint(remoteEndpointId, remoteEndpointName);
                    } else {
                        Toast.makeText(mContext, "Failed to connect to: " + remoteEndpointName,
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });
        } else {
            // Clients should not be advertising and will reject all connection requests.
            Log.w(TAG, "Connection Request to Non-Host Device - Rejecting");
            Nearby.Connections.rejectConnectionRequest(mGoogleApiClient, remoteEndpointId);
        }
    }

}

其余代码与示例相同。 实施新版本的最佳方式是什么?
它告诉我&#34;不幸的是,谷歌播放服务已经停止了#34;当我想作为客户端连接时, 什么是弃用新版本?

1 个答案:

答案 0 :(得分:1)

在NearbyClient类的上下文中最简单的方法是向实现抽象类的类添加两个新字段,并简单地调用现有的onConnectionRequest和onEndpointFound / Lost。

当不再公开device id参数时,引入了10.2中的混淆。在大多数情况下,这是应用程序必须执行的无意义的簿记,因此现在在10.2中您不必跟踪设备ID!

private Connections.ConnectionRequestListener myConnectionRequestListener =
        new Connections.ConnectionRequestListener() {
            @Override
            public void onConnectionRequest(String remoteEndpointId, String
                    remoteEndpointName, byte[] bytes) {
                NearbyClient.this.onConnectionRequest(remoteEndpointId,
                        remoteEndpointName, bytes);
            }
        };
private Connections.EndpointDiscoveryListener myEndpointDiscoveryListener =
        new Connections.EndpointDiscoveryListener() {
            @Override
            public void onEndpointFound(String endpointId,
                                        String serviceId,
                                        String name) {
                NearbyClient.this.onEndpointFound(endpointId,serviceId,
                        name);
            }

            @Override
            public void onEndpointLost(String remoteEndpointId) {
                NearbyClient.this.onEndpointLost(remoteEndpointId);
            }
        };

我将在本周晚些时候试用8位艺术家来更新它以使用10.2。同时,如果你先让它工作,请随时提交拉取请求:)。