我在使用Firebase实现谷歌登录时遇到了一个非常严重的问题。
我可以在模拟器(Android 7.0, x86_64 SDK)中登录,但是如果我将我的项目上传到手机(Android 6.0)上,我就无法使用Google登录。
我所看到的唯一一件事情就是应用程序关闭。在极少数情况下,如果我快速双击登录按钮,它会成功登录,但无法获得进一步使用的凭据。
可能出了什么问题?
LoginActivity中的代码如下:
谷歌登录实例
在build.gradle的底部。
LoginActivity中的代码如下:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == FirebaseConstants.LOG_IN_REQUEST){
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i);
}
if(requestCode == FirebaseConstants.LOG_OUT_REQUEST){
TastyToast.makeText(this, "Logged out successfully", TastyToast.LENGTH_LONG, TastyToast.SUCCESS);
}
}
这里是在 onCreate 方法中的代码(按钮点击):
btnGoogleSignIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(NetworkUtil.isNetworkAvailable(LoginActivity.this)){
googleLogin.logIn(LoginActivity.this);
}else{
TastyToast.makeText(LoginActivity.this, "Network is unavailable", TastyToast.LENGTH_LONG, TastyToast.ERROR);
}
}
});
谷歌登录实例
public class GoogleLogin extends Observable implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{
private final GoogleApiClient googleApiClient;
private final WeakReference<Activity> activityWeakReference;
private static GoogleLogin googleLogin;
public GoogleLogin(final Activity activity) {
activityWeakReference = new WeakReference<>(activity);
// Configure Google Sign In
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(activity.getString(R.string.default_web_client_id))
.requestEmail()
.build();
googleApiClient = new GoogleApiClient.Builder(activity)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
}
@Override
public void onConnected(@Nullable Bundle bundle) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
public static GoogleLogin getInstance(Activity activity) {
if (null == googleLogin) {
googleLogin = new GoogleLogin(activity);
}
return googleLogin;
}
public void logIn(Activity activity){
Context c = activityWeakReference.get().getApplicationContext();
googleApiClient.connect();
if(googleApiClient.isConnected()){
Intent i = new Intent(c, MainActivity.class);
googleApiClient.connect();
activity.startActivity(i); // this wont start an activity
}else if(!googleApiClient.isConnected()){
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
googleApiClient.connect();
// this wont start an activity either
activity.startActivityForResult(signInIntent, FirebaseConstants.LOG_IN_REQUEST);
}
}
public void signOut(final Activity activity){
if(googleApiClient.isConnected()) {
Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(
new ResultCallback<Status>() {
@Override
public void onResult(Status status) {
Intent i = new Intent(activity, LoginActivity.class);
activity.startActivityForResult(i, FirebaseConstants.LOG_OUT_REQUEST);
}
});
}
}
public void disconnect(Activity activity){
activity.getApplicationContext();
if(googleApiClient.isConnected()){
googleApiClient.disconnect();
Intent i = new Intent(activity, LoginActivity.class);
}
}
}
没有你们的帮助,我无法解决这个问题。我真的需要让它正常工作!
build.gradle中的依赖关系
compile 'com.google.firebase:firebase-messaging:9.6.1'
compile 'com.google.firebase:firebase-database:9.6.1'
compile 'com.google.firebase:firebase-core:9.6.1'
compile 'com.google.firebase:firebase-auth:9.6.1'
compile 'com.google.android.gms:play-services-auth:9.6.1'
compile 'com.google.firebase:firebase-ads:9.6.1'
在build.gradle的底部。
apply plugin: 'com.google.gms.google-services'