Android 用户登录后如何将LoginActivity从SplashScreenActivity重定向到MainActivity
我试了很多次,但每次我都在运行我的应用。。启动屏幕后,即使用户已经通过google帐户登录,google登录活动也会打开。一旦用户登录,我想跳过登录活动 任何 如有建议,将不胜感激。请您用适当的代码回答 以下是我的SplashScreen代码: 这是我的登录码:Android 用户登录后如何将LoginActivity从SplashScreenActivity重定向到MainActivity,android,Android,我试了很多次,但每次我都在运行我的应用。。启动屏幕后,即使用户已经通过google帐户登录,google登录活动也会打开。一旦用户登录,我想跳过登录活动 任何 如有建议,将不胜感激。请您用适当的代码回答 以下是我的SplashScreen代码: 这是我的登录码: 您只需在用户已登录时签入您的SplashActivity,即可实现此目的: GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this); if(
您只需在用户已登录时签入您的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);
}
将此代码放入onStart函数并检查是否已登录发布代码请在第一次访问所有数据时使用SharedPerference或Sqlite数据库创建会话。是否使用Firebase登录?在developer.android.com上阅读有关SharedReferences的信息,您将找到解决方案并始终尝试搜索询问问题前的Stackoverflow我正在使用google登录
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);
}
}
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);
}
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);
}
});
}