2016-05-07 136 views
2

我在我的Android應用程序中使用LoginButton進行了Facebook登錄集成。現在當我登錄時,我的登錄按鈕會自動更改爲註銷。我不希望這樣,因爲我有另一個註銷活動。我應該如何防止這一點。以下是我的代碼。Facebook登錄整合在Android登錄按鈕更改登錄後登出

public class FacebookLogin extends AppCompatActivity { 

CallbackManager callbackManager; 
RelativeLayout relativeLayout; 
Bundle bundle; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    facebookSDKInitialize(); 
    setContentView(R.layout.activity_facebook_login); 
    relativeLayout =(RelativeLayout)findViewById(R.id.relative_layout); 

    bundle = new Bundle(); 

    if (AppStatus.getInstance(this).isOnline()) { 

     LoginButton loginButton = (LoginButton) findViewById(R.id.login_button); 
     loginButton.setTextSize(16); 
     loginButton.setReadPermissions("email"); 
     getLoginDetails(loginButton); 



    } else { 


     Snackbar snackbar = Snackbar 
       .make(relativeLayout, "No internet connection!", Snackbar.LENGTH_INDEFINITE) 
       .setAction("RETRY", new View.OnClickListener() { 
        @Override 
        public void onClick(View view) { 
         Intent intent = new Intent(FacebookLogin.this, FacebookLogin.class); 
         startActivity(intent); 

        } 
       }); 

     // Changing message text color 
     snackbar.setActionTextColor(Color.RED); 

     // Changing action button text color 
     View sbView = snackbar.getView(); 
     TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text); 
     textView.setTextColor(Color.YELLOW); 
     snackbar.show(); 
    } 



} 

/* 
Initialize the facebook sdk. 
And then callback manager will handle the login responses. 
*/ 
protected void facebookSDKInitialize() { 

    FacebookSdk.sdkInitialize(getApplicationContext()); 
    callbackManager = CallbackManager.Factory.create(); 
} 

/* 
Register a callback function with LoginButton to respond to the login result. 
*/ 

protected void getLoginDetails(LoginButton login_button){ 

    // Callback registration 
    login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
     @Override 
     public void onSuccess(LoginResult login_result) { 
      getUserInfo(login_result); 

     } 

     @Override 
     public void onCancel() { 
      // code for cancellation 
     } 

     @Override 
     public void onError(FacebookException exception) { 
      // code to handle error 
     } 
    }); 
} 

/* 
To get the facebook user's own profile information via creating a new request. 
When the request is completed, a callback is called to handle the success condition. 
*/ 
protected void getUserInfo(LoginResult login_result){ 

    GraphRequest data_request = GraphRequest.newMeRequest(
      login_result.getAccessToken(), 
      new GraphRequest.GraphJSONObjectCallback(){ 
       @Override 
       public void onCompleted(
         JSONObject json_object, 
         GraphResponse response) { 

        try { 
         final String name =json_object.getString("name"); 
         final String email =json_object.getString("email"); 
         bundle.putString("userName",name); 
         bundle.putString("userEmail",email); 

        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 

        Intent intent = new Intent(FacebookLogin.this,HomePage.class); 
        intent.putExtras(bundle); 
        intent.putExtra("jsondata",json_object.toString()); 
        startActivity(intent); 
        finish(); 
       } 
      }); 
    Bundle permission_param = new Bundle(); 
    permission_param.putString("fields", "id,name,email,picture.width(120).height(120)"); 
    data_request.setParameters(permission_param); 
    data_request.executeAsync(); 

} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    callbackManager.onActivityResult(requestCode, resultCode, data); 

    Log.e("data", data.toString()); 
} 

@Override 
protected void onResume() { 
    super.onResume(); 

    // Logs 'install' and 'app activate' App Events. 
    AppEventsLogger.activateApp(this); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 

    // Logs 'app deactivate' App Event. 
    AppEventsLogger.deactivateApp(this); 
} 
} 
+1

定製FB按鈕,設置FB:logout_text = 「」 http://stackoverflow.com/questions/16314651/customize-android-facebook-login-button?rq=1 –

回答

2

有應該是一個非常簡單的解決方案,只需在XML其visibility設置爲gone

然後做出自己的按鈕,setOnclickListener就可以了,然後你的按鈕,裏面隱藏Facebook的登錄按鈕, 我假設你的Facebook按鈕的名稱btnFacebook 這麼寫這個的onclicklistener您的按鈕btnFacebook.performClick();

內,不要忘了在你的註銷按鈕使用此

LoginManager.getInstance().logOut(); 
+0

@Shivam做你的問題就解決了 –

+0

除了我可以使用新的自定義按鈕進行登錄之外,沒有爲我工作。 –

+0

什麼都不起作用, –