我正在使用Google Play游戏服务开发简单的转牌游戏。为了构建Google客户端,我使用此代码段:
public class App extends Application {
private GoogleApiClient mGoogleApiClient;
public AppCompatActivity activity;
public synchronized GoogleApiClient getGoogleApiClient(AppCompatActivity activity, GoogleApiClient.ConnectionCallbacks callbacks , GoogleApiClient.OnConnectionFailedListener listener){
this.activity = activity;
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this.activity, listener)
.addConnectionCallbacks(callbacks)
.setViewForPopups(activity.getWindow().getDecorView().getRootView())
.addApi(Games.API).addScope(Games.SCOPE_GAMES)
.build();
return mGoogleApiClient;
}
它工作正常,我可以从我的帐户和测试人员连接 现在我想在另一个用户发送邀请时向客户端发送特殊通知。我做的第一步是将ConnectionListner注册到客户端:
public class LoginActivity extends AppCompatActivity implements View.OnClickListener,OnInvitationReceivedListener,GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener {
...
GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//Get google api client
mGoogleApiClient = ((App) getApplication()).getGoogleApiClient(LoginActivity.this, this, this);
...
}
在Client API Connect上,我使用代码
注册invitationListner @Override
public void onConnected(@Nullable Bundle bundle) {
Log.d(TAG,"connesso");
Games.Invitations.registerInvitationListener(mGoogleApiClient, this);
}
为了邀请其他用户,我使用Api示例中的代码:
final ArrayList<String> invitees = data
.getStringArrayListExtra(Games.EXTRA_PLAYER_IDS);
Log.d(TAG,"Invites find:"+invitees.get(0).toString());
// get automatch criteria
Bundle autoMatchCriteria = null;
int minAutoMatchPlayers = data.getIntExtra(
Multiplayer.EXTRA_MIN_AUTOMATCH_PLAYERS, 1);
int maxAutoMatchPlayers = data.getIntExtra(
Multiplayer.EXTRA_MAX_AUTOMATCH_PLAYERS, 1);
if (minAutoMatchPlayers > 0) {
autoMatchCriteria = RoomConfig.createAutoMatchCriteria(
minAutoMatchPlayers, maxAutoMatchPlayers, 0);
} else {
autoMatchCriteria = null;
}
TurnBasedMatchConfig tbmc = TurnBasedMatchConfig.builder()
.addInvitedPlayers(invitees)
.setAutoMatchCriteria(autoMatchCriteria).build();
// Start the match
Games.TurnBasedMultiplayer.createMatch(mGoogleApiClient, tbmc).setResultCallback(...);});
对于接收用户的邀请,只需覆盖标准功能:
@Override
public void onInvitationReceived(Invitation invitation) {
Log.d(TAG,"invito arrivato");
}
用户选择器工作正常;我确定这一点,因为在智能手机上的Google Play游戏应用中,我看到了请求。但是从未调用过Received Log的邀请。
我在哪里做错了?提前致谢