2014-01-31 80 views
0

我使用3.6來實現Facebook登錄。從官方文件來看,它非常抽象,不容易遵循。所以我在網上找到了一些教程。但是,當我使用該教程中的代碼時,它說該方法已被棄用。雖然它有效,但我擔心官方稍後會停止該功能。那麼,是否有任何方法可以實現登錄/發佈到牆上/獲取最新sdk的個人信息?由於最新的方法來實現登錄通過facebook sdk在android

登錄代碼:

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    lblEmail = (TextView) findViewById(R.id.t); 

    LoginButton authButton = (LoginButton) findViewById(R.id.authButton); 
    authButton.setOnErrorListener(new OnErrorListener() { 

     @Override 
     public void onError(FacebookException error) { 
      Log.i(TAG, "Error " + error.getMessage()); 
     } 
    }); 
    // set permission list, Don't foeget to add email 
    authButton.setReadPermissions(Arrays.asList("basic_info", "email")); 
    // session state call back event 
    authButton.setSessionStatusCallback(new Session.StatusCallback() { 

     @Override 
     public void call(Session session, SessionState state, 
       Exception exception) { 

      if (session.isOpened()) { 
       Log.i(TAG, "Access Token" + session.getAccessToken()); 
       Request.executeMeRequestAsync(session, 
         new Request.GraphUserCallback() { 
          @Override 
          public void onCompleted(GraphUser user, 
            Response response) { 
           if (user != null) { 
            Log.i(TAG, "User ID " + user.getId()); 
            Log.i(TAG, 
              "Email " 
                + user.asMap().get(
                  "email")); 
            lblEmail.setText(user.asMap() 
              .get("email").toString()); 
           } 
          } 
         }); 
      } 

     } 
    }); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    Session.getActiveSession().onActivityResult(this, requestCode, 
      resultCode, data); 
} 

在代碼Request.executeMeRequestAsync是貶低

回答

1

嘗試這樣FACEBOOK LOGIN NEW SDK

Button button =(Button) findViewById(R.id.button_share); 
button.setOnClickListener(new OnClickListener() { 
@Override 
public void onClick(View v) {  
if (fb_session != null && fb_session.isOpened()) { 
    makeMeRequest(fb_session);  
    Log.i("Facebook Login State == >", "Facebook Login State");  
} else { 
    if (fb_session == null) { 
    fb_session = new Session(mCurrContext);    
     } 
    Session.setActiveSession(fb_session); 
    ConnectToFacebook(); 
    Log.i("Facebook not Login State == >", "Facebook Not login State"); 
}   
} 
}); 
}