在我的应用中,我使用了Firebase身份验证来使用Google帐户登录。当用户登录时,MainActivity
应该会启动,但这会启动一次并仍显示login
活动。
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:name = ".BFD"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".login"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"/>
<activity
android:name=".ContactSetup"
android:parentActivityName=".ContextSetup" />
<activity
android:name=".EditContacts"
android:parentActivityName=".EditContacts" />
<activity
android:name=".HowTo"
android:parentActivityName=".MainActivity" />
<activity
android:name=".AboutDevs"
android:parentActivityName=".MainActivity" />
<activity
android:name=".RecordHistory"
android:parentActivityName=".MainActivity" />
<activity
android:name=".CustomText"
android:parentActivityName=".ContextSetup" />
<activity
android:name=".ContextSetup"
android:parentActivityName=".MainActivity" />
</application>
login.java如下:
public class login extends AppCompatActivity {
private SignInButton signInButton;
private static final int RC_SIGN_IN = 1;
private GoogleApiClient mGoogleApiClient;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
//FirebaseApp.initializeApp(getApplicationContext());
mAuth = FirebaseAuth.getInstance();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() != null){
startActivity(new Intent(login.this, MainActivity.class));
}
}
};
signInButton = findViewById(R.id.SignIn);
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
mGoogleApiClient = new GoogleApiClient.Builder(this)
.enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
// Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
}
})
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
signIn();
}
});
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
private void signIn() {
Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);
startActivityForResult(signInIntent, RC_SIGN_IN);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
// Google Sign In failed, update UI appropriately
Log.w("Signing in: ", "Google sign in failed", e);
// ...
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
//Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
//updateUI(user);
} else {
// If sign in fails, display a message to the user.
//Log.w(TAG, "signInWithCredential:failure", task.getException());
//Snackbar.make(findViewById(R.id.main_layout), "Authentication Failed.", Snackbar.LENGTH_SHORT).show();
//updateUI(null);
}
// ...
}
});
}
}
我在这里做错了什么阻止了主要活动的开始。 任何建议/意见将不胜感激:))
答案 0 :(得分:0)
登录成功后,您是否更新了您的活动?我看到你的updateUI方法是评论。当用户成功登录时,不会给出任何语句。
还要根据您的项目检查您的Auth客户端ID。
如果真有帮助你,请回复我。
答案 1 :(得分:-1)
使用addAuthStateListener(AuthStateListener)和removeAuthStateListener(AuthStateListener)注册或取消注册侦听器。 资料来源:https://firebase.google.com/docs/reference/android/com/google/firebase/auth/FirebaseAuth.AuthStateListener
所以我猜mAuth.addAuthStateListener(mAuthListener); 是初始化authStateListener后你需要的。
另外在您设置用户的onComplete中, 我认为那是你应该打电话给你的新活动的地方。我建议将用户保存在实例中或将其传递给intent。取决于您的需求。
我没有尝试这个,但onComplete和authStateListener中的简单调试和断点会指向正确的方向。
祝你好运,希望我帮助过。 在附注中,类和活动应以大写字母LoginActivity开头(是活动名称的约定)