我正在尝试实施GooglePlayGamesServices我试图遵循trivial quest and type a number examples,不同之处在于我想在许多不同的活动中更新分数和成就,所以我做了一个静态类来帮助它,如下所示:
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.games.Games;
/**
* Created by OSDON on 1/18/2017.
*/
public class PlayServicesUtilities
{
static boolean firsttime=true;
static GoogleApiClient mGoogleApiClient;
public static void SetScore(int score, Context context)
{
if (mGoogleApiClient!=null&& mGoogleApiClient.isConnected())
{
Games.Leaderboards.submitScore(mGoogleApiClient,context.getString(R.string.leaderboard_best_score),score);
}
}
public static Intent LeaderBoard(Context context)
{
if (mGoogleApiClient!=null&&mGoogleApiClient.isConnected())
return Games.Leaderboards.getLeaderboardIntent(mGoogleApiClient,context.getString(R.string.leaderboard_best_score));
return null;
}
public static Intent Achievements(){
if (mGoogleApiClient!=null&&mGoogleApiClient.isConnected())
return Games.Achievements.getAchievementsIntent(mGoogleApiClient);
return null;
}
}
我在我的主要活动中实现了api连接,如下所示:
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentSender;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutCompat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.games.Games;
import com.google.android.gms.games.Player;
import com.google.example.games.basegameutils.BaseGameUtils;
import java.util.Locale;
public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,GoogleApiClient.OnConnectionFailedListener
{
ImageButton playButton,settingsButton,leaderboard,achievements;
Button signOut;
boolean quitingApp=true;
FrameLayout frameLayout;
com.google.android.gms.common.SignInButton signIn;
private boolean mSignInClicked;
// Request code used to invoke sign in user interactions.
private static final int RC_SIGN_IN = 9001;
private static final int LEADERBOARD=1001;
// Are we currently resolving a connection failure?
private boolean mResolvingConnectionFailure = false;
// Set to true to automatically start the sign in flow when the Activity starts.
// Set to false to require the user to click the button in order to sign in.
private boolean mAutoStartSignInFlow = false;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//region PlayServices
// Create the Google Api Client with access to Games
//if (PlayServicesUtilities.mGoogleApiClient==null)
PlayServicesUtilities.mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Games.API)
.addScope(Games.SCOPE_GAMES)
.build();
new Runnable(){
@Override
public void run() {
signIn= (SignInButton) findViewById(R.id.button_sign_in);
signOut=(Button)findViewById(R.id.button_sign_out);
signIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mSignInClicked = true;
PlayServicesUtilities.mGoogleApiClient.connect();
}
});
signOut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mSignInClicked = false;
if (PlayServicesUtilities.mGoogleApiClient!=null&&PlayServicesUtilities.mGoogleApiClient.isConnected()){
Games.signOut(PlayServicesUtilities.mGoogleApiClient);
PlayServicesUtilities.mGoogleApiClient.disconnect();
}
showSignInBar();
}
});
}
}.run();
InitiateScoresAchievementsUnlocking(dataProvider.getScore(User.CurrentUser.getId()),getBaseContext());
//endregion
frameLayout= (FrameLayout) findViewById(R.id.main_popupFrame);
leaderboard= (ImageButton) findViewById(R.id.main_leaderBoard);
leaderboard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
Intent intent=PlayServicesUtilities.LeaderBoard(getBaseContext());
if (intent!=null)
{
quitingApp=false;
startActivityForResult(intent,LEADERBOARD);
}
else
alertView(getString(R.string.you_need_to_sign_in));
}
});
achievements= (ImageButton) findViewById(R.id.main_achievement);
achievements.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
SoundEffects.ButtonClick(getBaseContext());
Intent intent=PlayServicesUtilities.Achievements();
if (intent!=null){
quitingApp=false;
startActivityForResult(intent,LEADERBOARD);
}
else
alertView(getString(R.string.you_need_to_sign_in));
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
mSignInClicked = false;
mResolvingConnectionFailure = false;
if (resultCode == RESULT_OK) {
PlayServicesUtilities.mGoogleApiClient.connect();
} else
{
BaseGameUtils.showActivityResultError(this,requestCode,resultCode, R.string.signin_other_error);
}
}
if (requestCode==LEADERBOARD)
{
}
}
private void alertView(String message )
{
final Dialog alertDialog=new Dialog(MainActivity.this);
alertDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
alertDialog.setContentView(R.layout.alert_dialog);
alertDialog.show();
TextView textView= (TextView) alertDialog.findViewById(R.id.alert_dialog_text);
textView.setText(message);
Button button= (Button) alertDialog.findViewById(R.id.alert_dialog_ok);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
alertDialog.dismiss();
}
});
}
//region playServices
// Shows the "sign in" bar (explanation and button).
private void showSignInBar()
{
findViewById(R.id.sign_in_bar).setVisibility(View.VISIBLE);
findViewById(R.id.sign_out_bar).setVisibility(View.GONE);
}
// Shows the "sign out" bar (explanation and button).
private void showSignOutBar() {
findViewById(R.id.sign_in_bar).setVisibility(View.GONE);
findViewById(R.id.sign_out_bar).setVisibility(View.VISIBLE);
}
@Override
public void onConnected(@Nullable Bundle bundle)
{
showSignOutBar();
PlayServicesUtilities.mGoogleApiClient.connect();
if (PlayServicesUtilities.firsttime&& PlayServicesUtilities.mGoogleApiClient.isConnected()){
Player p = Games.Players.getCurrentPlayer(PlayServicesUtilities.mGoogleApiClient);
String displayName;
if (p == null) {
displayName = "";
} else {
displayName = p.getDisplayName();
}
if (!displayName.equals("")){
Toast.makeText(getBaseContext(),getString(R.string.hello).replace("[0]",displayName),Toast.LENGTH_SHORT).show();
PlayServicesUtilities.firsttime=false;
}
}
}
@Override
public void onConnectionSuspended(int i) {
PlayServicesUtilities.mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult)
{
if (mResolvingConnectionFailure)
{
return;
}
if (mSignInClicked || mAutoStartSignInFlow)
{
mAutoStartSignInFlow = false;
mSignInClicked = false;
mResolvingConnectionFailure = BaseGameUtils.resolveConnectionFailure(this,PlayServicesUtilities.mGoogleApiClient,connectionResult, RC_SIGN_IN, getString(R.string.signin_other_error));
}
showSignInBar();
}
@Override
protected void onStop() {
super.onStop();
if (quitingApp&&PlayServicesUtilities.mGoogleApiClient.isConnected()) {
PlayServicesUtilities.mGoogleApiClient.disconnect();}
}
@Override
protected void onStart() {
super.onStart();
if(PlayServicesUtilities.firsttime)
{
PlayServicesUtilities.mGoogleApiClient.connect();
PlayServicesUtilities.firsttime=false;
}
}
//endregion
void InitiateScoresAchievementsUnlocking(int currentScore,Context context)
{
String achievements[]=context.getResources().getStringArray(R.array.achievements);
int achievementsNeededScore[]=context.getResources().getIntArray(R.array.achievements_needed_scores);
if (PlayServicesUtilities. mGoogleApiClient.isConnected())
for (int i=0;i<achievements.length;i++)
{
if (currentScore>=achievementsNeededScore[i])
{
Games.setViewForPopups(PlayServicesUtilities.mGoogleApiClient,frameLayout);
Games.Achievements.unlock(PlayServicesUtilities.mGoogleApiClient,achievements[i]);
}
}
}
}
问题是:
1-(IN EMULATOR)当我启动应用程序并注销playservices并登录并且如果我请求引导板alertView(getString(R.string.you_need_to_sign_in));
被触发
2-(IN REAL DEVICE)甚至没有登录播放服务它显示一个快速错误对话框“无法登录...”并且活动重启
答案 0 :(得分:0)
正如文档中所建议的那样 - Troubleshooting Issues in Your Android Game:
如果您无法在自己的游戏中签名,请先确保您已按照create your client IDs和configure the games services的说明进行操作。如果您仍然遇到登录错误,请检查以下项目以确保您的游戏设置正确。
您可能需要检查上面的文档以验证您遇到问题的位置。
希望这有帮助。