gpt4 book ai didi

java - 如何在新的play-services-nearby中实现抽象类Endpoint Discovery Listener和Connection RequestListener?

转载 作者:行者123 更新时间:2023-11-30 00:42:19 25 4
gpt4 key购买 nike

我将我的 play-services-nearby 更新到版本“10.2.0”,它将 EndpointDiscoveryListener 和 ConnectionRequestListener 从接口(interface)更改为抽象类,我用 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);
}
}

}

其余代码与示例相同。实现新版本的最佳方式是什么?
当我想作为客户端连接时,它显示“不幸的是,Google Play 服务已停止”,什么是弃用新版本?

最佳答案

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

10.2 中的混淆是在不再公开设备 ID 参数时引入的。在大多数情况下,这是应用程序必须执行的毫无意义的簿记,因此现在在 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);
}
};

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

关于java - 如何在新的play-services-nearby中实现抽象类Endpoint Discovery Listener和Connection RequestListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42358078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com