代码之家  ›  专栏  ›  技术社区  ›  user8412632

Android Firebase Facebook登录在应用程序重新打开时显示注销按钮

  •  3
  • user8412632  · 技术社区  · 8 年前
    public class MainActivity extends AppCompatActivity {
    
        private LoginButton facebookloginButton;
        private CallbackManager callbackManager;
        private FirebaseAuth mAuth;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mAuth = FirebaseAuth.getInstance();
    
            facebookloginButton=(LoginButton)findViewById(R.id.facebook_login_button);
            callbackManager=CallbackManager.Factory.create();
            facebookloginButton.setReadPermissions("email", "public_profile");
            facebookloginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    handleFacebookAccessToken(loginResult.getAccessToken());
                }
    
                @Override
                public void onCancel() {
    
                }
    
                @Override
                public void onError(FacebookException error) {
                    Toast.makeText(getApplicationContext(), "Error",Toast.LENGTH_LONG).show();
                }
            });
    
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            callbackManager.onActivityResult(requestCode, resultCode, data);
        }
    
        private void handleFacebookAccessToken(AccessToken token) {
    
    
            AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
            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
    
                                FirebaseUser user = mAuth.getCurrentUser();
                                Intent intent=new Intent(getApplicationContext(),NextActivity.class);
                                //  intent.putExtra("NAME",user.getDisplayName());
                                startActivity(intent);
                                finish();
    
                            } else {
                                // If sign in fails, display a message to the user.
    
                                Toast.makeText(getApplicationContext(), "Authentication failed.",
                                        Toast.LENGTH_SHORT).show();
    
                            }
    
                            // ...
                        }
                    });
        }
    
        @Override
        public void onStart() {
            super.onStart();
            // Check if user is signed in (non-null) and update UI accordingly.
            FirebaseUser currentUser = mAuth.getCurrentUser();
    //        currentUser.getDisplayName();
        }
    
    }
    

    但当我按下脸书的登录按钮时,它会显示几秒钟的注销按钮,然后将我重定向到NextActivity。当我关闭应用程序并再次打开它时,它会显示带有注销按钮的主要活动。

    2 回复  |  直到 8 年前
        1
  •  1
  •   Shaifali Rajput    8 年前

    您可以保存 AUTH Token 在你的应用程序中成功登录Facebook之后 Shared Preferences settings

    final static String PREFS_NAME = "AUTH" 
    
    public class MainActivity extends AppCompatActivity{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
            setContentView(R.layout.activity_main);
    
            // check if user is already logged in
            // i.e. auth token is present or not
            String token = settings.getString("auth_token", null);
            // means user is logged in token was found
            if (token != null) {
                AUTH_TOKEN = token;
                startActivity(new Intent(MainActivity.this, ProfileActivity.class));
            }
        }
    
            // YOUR REST OF THE CODE ....
    
    });
    
     @Override
    public void onComplete(@NonNull Task<AuthResult> task) {
        if (task.isSuccessful()) {
            // Sign in success, update UI with the signed-in user's information
    
            FirebaseUser user = mAuth.getCurrentUser();
    
            // SAVE THE USER DETAILS OR PART OF IT IN SHARED PREFS
            SharedPreferences.Editor editor = settings.edit();
            editor.putString("USER", user);
            editor.apply();
    
            Intent intent=new Intent(getApplicationContext(),NextActivity.class);
            //  intent.putExtra("NAME",user.getDisplayName());
            startActivity(intent);
            finish();
    
        } else {
            // If sign in fails, display a message to the user.
    
            Toast.makeText(getApplicationContext(), "Authentication failed.",
                    Toast.LENGTH_SHORT).show();
    
        }
    
        // ...
    }
    

    onCreate 方法检查 AUTH 文件如果存在,它将直接将用户发送到 nextActivity .

        2
  •  0
  •   Kartik Shandilya Golam Rabbani    8 年前

    因此,在创建您的LoginActivity时,请执行以下操作:

        SharedPreferences     settings=getSharedPreferences("prefs",0);
    boolean firstRun=settings.getBoolean("firstRun",false);
    if(firstRun==false)//if running for first time
    
    {
        //Set the firstRun value to true
        editor.putBoolean("firstRun",true);
        editor.commit();
       //Continue your login process
    
    }
    else
    {
        // launch NextActivity if app is opened not for the first time
        Intent i=new Intent(check.this, NextActivity.class);
        startActivity(I);
        finish();
    }