我尝试了很多,但是我一直在运行我的应用程序..在启动画面之后,即使用户已经通过google account.once用户登录,Google登录活动也会打开,我想跳过登录活动。
任何 建议将不胜感激。请您使用适当的代码回答。
这是我的SplashScreen代码:
public class MainActivity extends AppCompatActivity {
protected int _splashTime = 3000;
ImageView imageViewLogo, imageViewText;
Animation animationSlideUp;
Animation animationFadeIn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageViewLogo = findViewById(R.id.imageViewLogo);
imageViewText = findViewById(R.id.imageViewText);
animationSlideUp = AnimationUtils.loadAnimation(this,R.anim.slide_up_anim);
imageViewLogo.startAnimation(animationSlideUp);
animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_anim);
imageViewText.startAnimation(animationFadeIn);
Thread splashTread;
splashTread = new Thread() {
@Override
public void run() {
try {
synchronized (this) {
wait(_splashTime);
}
} catch (InterruptedException e) {
} finally {
finish();
//login value is no, so start loginactivity
Intent i = new Intent();
i.setClass(MainActivity.this, LoginActivity.class);
startActivity(i);
//stop();
}
}
};
splashTread.start();
}
}
这是我的登录代码:
public class LoginActivity extends AppCompatActivity {
//a constant for detecting the login intent result
private static final int RC_SIGN_IN = 234;
//Tag for the logs optional
private static final String TAG = "AMOLEDify";
//creating a GoogleSignInClient object
GoogleSignInClient mGoogleSignInClient;
//And also a Firebase Auth object
FirebaseAuth mAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_page);
//first we intialized the FirebaseAuth object
mAuth = FirebaseAuth.getInstance();
//Then we need a GoogleSignInOptions object
//And we need to build it as below
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getString(R.string.default_web_client_id))
.requestEmail()
.build();
//Then we will get the GoogleSignInClient object from GoogleSignIn class
mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
//Now we will attach a click listener to the sign_in_button
//and inside onClick() method we are calling the signIn() method that will open
//google sign in intent
findViewById(R.id.sign_in_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
signIn();
}
});
}
@Override
protected void onStart() {
super.onStart();
if (mAuth.getCurrentUser() != null) {
finish();
startActivity(new Intent(this, SignInProfile.class));
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//if the requestCode is the Google Sign In code that we defined at starting
if (requestCode == RC_SIGN_IN) {
//Getting the GoogleSignIn Task
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
//Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account = task.getResult(ApiException.class);
//authenticating with firebase
firebaseAuthWithGoogle(account);
} catch (ApiException e) {
Toast.makeText(LoginActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
//getting the auth credential
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
//Now using firebase we are signing in the user here
mAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d(TAG, "signInWithCredential:success");
FirebaseUser user = mAuth.getCurrentUser();
Toast.makeText(LoginActivity.this, "User Signed In", Toast.LENGTH_SHORT).show();
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
Toast.makeText(LoginActivity.this, "Authentication failed.",
Toast.LENGTH_SHORT).show();
}
}
});
}
//this method is called on click
private void signIn() {
//getting the google signin intent
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
//starting the activity for result
startActivityForResult(signInIntent, RC_SIGN_IN);
}
}
答案 0 :(得分:0)
您可以通过简单地检入已经登录的用户SplashActivity
来完成此操作:
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if(account != null){
// user is logged in
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
else {
//user is not logged in
Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
答案 1 :(得分:0)
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn(mGoogleApiClient);
if (opr.isDone()) {
Log.d("Already signed in", "Got cached sign-in");
GoogleSignInResult result = opr.get();
startActivity(new Intent(this, SignInProfile.class));
//result(result);
} else {
opr.setResultCallback(new ResultCallback<GoogleSignInResult>() {
@Override
public void onResult(GoogleSignInResult googleSignInResult) {
//result(googleSignInResult);
}
});
}
将此代码放入onStart函数并检查是否已登录