[英]Google Play Games
今天是個好日子。
我正在嘗試在正在開發的游戲中實現成就。
我已經在Google Play控制台上設置了所有內容,獲取了應用ID,並在清單中添加了以下內容
<meta-data
android:name="com.google.android.gms.games.APP_ID"
android:value="@string/app_id" />
並寫了下面的方法
GoogleApiAvailability googleApiAvailability = GoogleApiAvailability.getInstance();
int temp = googleApiAvailability.isGooglePlayServicesAvailable(this);
if ( temp != 0)
return;
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN)
.requestEmail()
.build();
GoogleSignIn.getClient(this, gso);
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
PlayersClient player = Games.getPlayersClient(this, account);
當我運行它時,我得到了我的帳戶,但是當它運行Games.getPlayersClient(this, account);
,我得到了我的帳戶Games.getPlayersClient(this, account);
我收到以下錯誤:
java.lang.IllegalStateException:游戲API需要https://www.googleapis.com/auth/games_lite函數。
任何人都知道可能有什么問題嗎?
提前致謝。
我認為您應該檢查:
GoogleSignIn.hasPermissions(account, Games.SCOPE_GAMES_LITE).
如果該帳戶沒有權限,則應使用
GoogleSignIn.getClient(this, gso).silentSignIn or GoogleSignIn.getClient(this, gso).getSignInIntent()
與startActivityForResult一起接收GAMES_LITE范圍的帳戶。
對於空帳戶, GoogleSignIn.hasPermissions還返回false,這也可能是getLastSignedInAccount的結果。
例:
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if (GoogleSignIn.hasPermissions(account, Games.SCOPE_GAMES_LITE)) {
onSignIn(account);
} else {
signInClient
.silentSignIn()
.addOnCompleteListener(
this,
task -> {
if (task.isSuccessful()) {
onSignIn(task.getResult());
} else {
resetSignedIn();
}
});
}
您是否已在清單中正確添加了對Google Play服務的依賴關系? 在此處的“常見錯誤”部分
“ 4.在為Android開發時,請將Play Games SDK作為庫項目而不是獨立的JAR包含在內。請確保在您的Android項目中將Google Play服務SDK引用為庫項目,否則在您的Android項目中應用程序找不到Google Play服務資源。要了解如何設置Android項目以使用Google Play服務,請參閱設置Google Play服務 。”
另外,您的清單中有嗎?
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
您的gradle文件中是否有依賴項?
compile "com.google.android.gms:play-services-games:${gms_library_version}" compile "com.google.android.gms:play-services-auth:${gms_library_version}"
在顯示排行榜時,我遇到了同樣的問題,發現Oleh的解決方案有助於解決該問題。 要求正確的范圍是關鍵。 在onCreate上構建GoogleSignIn客戶端的代碼是:
// Build a GoogleSignInClient with the options specified by gso.
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(clientId)
.requestScopes(Games.SCOPE_GAMES_LITE)
.build();
mGoogleSignInClient = GoogleSignIn.getClient(HomeActivity.this, gso);
稍后,在顯示排行榜時,我會這樣做:
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if (null != account) {
// check permissions, show Leaderboard when allowed to
boolean hasGamesLitePermissions = GoogleSignIn.hasPermissions(account, Games.SCOPE_GAMES_LITE);
if (hasGamesLitePermissions) {
Games.getLeaderboardsClient(this, account)
.getAllLeaderboardsIntent()
.addOnSuccessListener(this)
.addOnFailureListener(this);
}
我的活動還為登錄過程實現了兩個偵聽器:
public final class HomeActivity implements
OnSuccessListener<Intent>,
OnFailureListener
(來自com.google.android.gms.tasks包)
最后,在onSuccess中,我可以展示排行榜
public void onSuccess(Intent intent) {
startActivityForResult(intent, RC_LEADERBOARD_UI);
}
在我的情況下,onFailure只會向用戶顯示錯誤。 但是請確保同時擁有兩個偵聽器,以便在調試時不會錯過任何有用的細節。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.