2015-12-29 91 views
2

我有一個Facebook登錄代碼,但問題是它具有相同佈局的登錄按鈕和配置文件頁面的詳細信息。我想在成功登錄時進入不同的佈局。對此事的任何幫助將不勝感激。Facebook登錄不同佈局

MainActivity 

public class MainActivity extends Activity { 
    CallbackManager callbackManager; 
    Button share,details; 
    ShareDialog shareDialog; 
    LoginButton login; 
    ProfilePictureView profile; 
    Dialog details_dialog; 
    TextView details_txt; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     FacebookSdk.sdkInitialize(getApplicationContext()); 
     setContentView(R.layout.activity_main); 

     callbackManager = CallbackManager.Factory.create(); 
     login = (LoginButton)findViewById(R.id.login_button); 
     profile = (ProfilePictureView)findViewById(R.id.picture); 
     shareDialog = new ShareDialog(this); 
     share = (Button)findViewById(R.id.share); 
     details = (Button)findViewById(R.id.details); 
     login.setReadPermissions("public_profile email"); 
     share.setVisibility(View.INVISIBLE); 
     details.setVisibility(View.INVISIBLE); 
     details_dialog = new Dialog(this); 
     details_dialog.setContentView(R.layout.dialog_details); 
     details_dialog.setTitle("Details"); 
     details_txt = (TextView)details_dialog.findViewById(R.id.details); 
     details.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       details_dialog.show(); 
      } 
     }); 


     if(AccessToken.getCurrentAccessToken() != null){ 
      RequestData(); 
      share.setVisibility(View.VISIBLE); 
      details.setVisibility(View.VISIBLE); 
     } 
     login.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       if(AccessToken.getCurrentAccessToken() != null) { 
        share.setVisibility(View.INVISIBLE); 
        details.setVisibility(View.INVISIBLE); 
        profile.setProfileId(null); 
       } 
      } 
     }); 
     share.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       ShareLinkContent content = new ShareLinkContent.Builder().build(); 
       shareDialog.show(content); 

      } 
     }); 
     login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 

       if(AccessToken.getCurrentAccessToken() != null){ 
        RequestData(); 
        share.setVisibility(View.VISIBLE); 
        details.setVisibility(View.VISIBLE); 
       } 
      } 

      @Override 
      public void onCancel() { 

      } 

      @Override 
      public void onError(FacebookException exception) { 
      } 
     }); 

    } 
    public void RequestData(){ 
     GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() { 
      @Override 
      public void onCompleted(JSONObject object,GraphResponse response) { 

       JSONObject json = response.getJSONObject(); 
       try { 
        if(json != null){ 
         String text = "<b>Name :</b> "+json.getString("name")+"<br><br><b>Email :</b> "+json.getString("email")+"<br><br><b>Profile link :</b> "+json.getString("link"); 
         details_txt.setText(Html.fromHtml(text)); 
         profile.setProfileId(json.getString("id")); 
        } 

       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
     Bundle parameters = new Bundle(); 
     parameters.putString("fields", "id,name,link,email,picture"); 
     request.setParameters(parameters); 
     request.executeAsync(); 
    } 

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



} 
+0

上登錄成功獲得所需的所有數據,並傳遞它捆綁到其他活動 –

+0

對不起我新的。我應該在哪裏添加意圖?在login.registerCallback()? –

+0

回調的成功 –

回答

1

你必須GraphRequest的onCompleted方法是這樣成功的數據抓取後,通過您的Intent

public void RequestData(){ 
    GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() { 
     @Override 
     public void onCompleted(JSONObject object,GraphResponse response) { 

      JSONObject json = response.getJSONObject(); 
      try { 
       if(json != null){ 
        String text = "<b>Name :</b> "+json.getString("name")+"<br><br><b>Email :</b> "+json.getString("email")+"<br><br><b>Profile link :</b> "+json.getString("link"); 
        details_txt.setText(Html.fromHtml(text)); 
        profile.setProfileId(json.getString("id")); 
// Pass your Intent Here 
       } 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
    Bundle parameters = new Bundle(); 
    parameters.putString("fields", "id,name,link,email,picture"); 
    request.setParameters(parameters); 
    request.executeAsync(); 
       @Override 
        public void onError(FacebookException error) { 
         Log.d("Error ", "Error"); 
         // here you have can handle if any error occur 
        } 

        @Override 
        public void onCancel() { 
         Log.d("cancle ", "cancle"); 
         // This method is called if user interrupt data fetching 
        } 
} 
+0

謝謝Kapil。我按照你的建議添加了我的意圖,並且效果很好。但是在更改佈局之前一秒或兩秒鐘,它會在同一頁面上顯示「註銷」按鈕,然後意圖更改佈局。我可以做些什麼來消除這種情況? @Kapil。 –

+0

是點擊登錄按鈕(login.setOnClickListener)將其可見性設置爲不可見login.setVisibility(View.INVISIBLE); –

+0

我可以做到這一點,但整個佈局仍然可見幾秒鐘。我希望Facebook訪問頁面消失時,由於意圖,第一個佈局也會更改爲新的佈局。目前它在第一頁上停留2秒,然後更改爲新的版面。 @Kapil。 –

相關問題