如何解决首次登录Google Play后,欢迎弹出窗口无法正常工作
在应用程序中首次登录Google Play后,欢迎弹出窗口无法正常工作。但是当我之后自动登录时,它可以正常工作。
onActivityResult函数中的以下语法无法正常工作。
Games.getGamesClient(getContext(),mSignedInAccount).setViewForPopups(getWindow()。getDecorView()。findViewById(android.R.id.content));
我要使弹出窗口即使在首次登录后也能正常工作。
private GoogleSignInClient mGoogleSignInClient = null;
private GoogleSignInAccount mSignedInAccount = null;
private GamesClient mGamesClient = null;
private GoogleSignInOptions mSignInOptions =null;
Boolean bGoogleLogin = false;
private static final int RC_SIGN_IN = 9001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.setEnableVirtualButton(false);
super.onCreate(savedInstanceState);
mSignInOptions = GoogleSignInOptions.DEFAULT_GAMES_SIGN_IN;
mGoogleSignInClient = GoogleSignIn.getClient(this,new GoogleSignInOptions.Builder(mSignInOptions).build());
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode,resultCode,data);
bGoogleLogin = true;
if (requestCode == RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
mSignedInAccount = result.getSignInAccount();
Toast.makeText(getApplicationContext(),"Signed In",Toast.LENGTH_SHORT).show();
Games.getGamesClient(getContext(),mSignedInAccount).setViewForPopups(getWindow().getDecorView().findViewById(android.R.id.content));
} else {
String message = result.getStatus().getStatusMessage();
if (message == null || message.isEmpty()) {
message = "Error";
}
Toast.makeText(getApplicationContext(),"Sign In Failed,please check internet connection and restart the app to use achievements.",Toast.LENGTH_LONG).show();
}
}
}
@Override
protected void onResume() {
super.onResume();
this.signInSilently();
}
public void signInSilently() {
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if (GoogleSignIn.hasPermissions(account,mSignInOptions.getScopeArray())) {
// Already signed in.
// The signed in account is stored in the 'account' variable.
Toast.makeText(getApplicationContext(),"Signed In Already",Toast.LENGTH_SHORT).show();
mSignedInAccount = account;
Games.getGamesClient(getContext(),mSignedInAccount).setViewForPopups(getWindow().getDecorView().findViewById(android.R.id.content));
} else {
mGoogleSignInClient.silentSignIn().addOnCompleteListener(
this,new OnCompleteListener<GoogleSignInAccount>() {
@Override
public void onComplete(@NonNull Task<GoogleSignInAccount> task) {
if (task.isSuccessful()) {
// The signed in account is stored in the task's result.
GoogleSignInAccount googleSignInAccount = task.getResult();
if (mSignedInAccount != googleSignInAccount) {
mSignedInAccount = googleSignInAccount;
Toast.makeText(getApplicationContext(),"Silent Signed In",Toast.LENGTH_SHORT).show();
}
} else {
if(bGoogleLogin == false) {
startActivityForResult(mGoogleSignInClient.getSignInIntent(),RC_SIGN_IN);
}
}
}
});
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。