2

我正在开发一个连接到 Facebook 的 Android 应用程序。

这是我的代码:

public class FBLoginActivity extends Activity
{
    /**
     * Log's tag.
     */
    private static final String TAG = "FBLoginActivity";
    /**
     * Widget to let user selects if he/she accepts conditions or not.
     */
    private ToggleButton mTglBtnAcceptConditions;
    /**
     * Represents the button to Login into Facebook.
     */
    private ImageButton mFBLoginButton;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fblogin);

        getWidgetsReferences();
    }

    @Override
    protected void onResume()
    {
        Log.v(TAG, "onResume");
        super.onResume();

        Session session = Session.getActiveSession();
        if ((session != null) && (session.isOpened()))
        {
            // Disable Facebook login button because user is logged.
            mFBLoginButton.setEnabled(false);
            // Show a message to the user saying that is logged.
            Toast.makeText(this, 
                    getString(R.string.toast_message_your_are_logged_fb),
                    Toast.LENGTH_LONG).show();
        }
    }
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        Log.v(TAG, "onActivityResult");
        super.onActivityResult(requestCode, resultCode, data);

        Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
    }

    /**
     * This method handles when user does click on Facebook's Login button.
     * @param view
     */
    public void onFBLoginClick(View view)
    {
        openActiveSession(this, true, statusCallback);
    }

    /**
     * Status CallBack for Facebook Open Session.
     */
    Session.StatusCallback statusCallback = new Session.StatusCallback() 
    {
        @Override
        public void call(final Session session, SessionState state, Exception exception) 
        {
            Log.v(TAG, "call");
            if(session.isOpened())
            {
                Log.v(TAG, "isOpened");
                // Show a dialog saying that we are storing user data into the
                // phone.
                final ProgressDialog savingDialog = new ProgressDialog(FBLoginActivity.this);
                savingDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                savingDialog.setMessage(getString(R.string.dialog_message_saving_your_fb_data));
                savingDialog.setCancelable(false);
                savingDialog.show();

                Request.executeMeRequestAsync(session, new Request.GraphUserCallback() 
                {
                    @Override
                    public void onCompleted(GraphUser user, Response response) 
                    {
                        Log.v(TAG, "onCompleted");
                        if(user != null)
                        {
                            // Get Facebook's ID, name, gender, birthday and email.
                            String facebookId = user.getId();
                            String name = user.getName();
                            String gender = user.getProperty("gender").toString();
                            String birthday = user.getBirthday();
                            String email = user.getProperty("email").toString();

                            // Save facebook data on Shared Preferences
                            saveUserData(facebookId, name, gender, birthday, email);

                            // Save Facebook access token on Shared preferences.
                            saveAccessToken(session.getAccessToken());

                            // Close saving user's data dialog.
                            savingDialog.dismiss();

                            // Get and save Facebook Profile Picture on internal
                            // storage.
                            GetFacebookProfilePicture task = 
                                    new GetFacebookProfilePicture(FBLoginActivity.this);
                            task.execute(user.getId());
                        }
                    }
                });
            }
        }
    };

    /**
     * Open a Facebook's session to get user personal data. On the OpenRequest 
     * we set "user_birthday" and "email" permissions. 
     * 
     * @param activity
     * @param allowLoginUI
     * @param statusCallback

     * @return Session opened with desired permissions.
     */
    private static Session openActiveSession(Activity activity,
            boolean allowLoginUI, Session.StatusCallback statusCallback)
    {
        OpenRequest openRequest = new OpenRequest(activity);
        openRequest.setPermissions(Arrays.asList("user_birthday", "email"));
        openRequest.setCallback(statusCallback);

        Session session = new Session.Builder(activity).build();

        if(SessionState.CREATED_TOKEN_LOADED.equals(session.getState()) || allowLoginUI)
        {
            Session.setActiveSession(session);
            session.openForRead(openRequest);

            return session;
        }

        return null;
    }
}

此代码适用于 Android 模拟器和我的旧 HTC Desire。但是当我在三星 Galaxy S3 等最新设备上测试它时,它不起作用。

调试代码,我这里加了一个断点:if(session.isOpened())对象session是:

{Session state:OPENING, token:{AccessToken token:ACCESS_TOKEN_REMOVED permissions:[]}, appId:XXXXXXXXXXX}

发生了什么?我认为问题在于代码在最快的设备上不起作用。

你知道我在这里做的另一种方法吗?

4

3 回答 3

1

我要回答我自己的问题:

这里的问题不是我在哪里测试我的应用程序。问题是,在 Samsumg Galaxy S3 上,测试人员使用他自己的 Facebook 帐户进行测试,而我没有停用 SandBox。

而且我犯了另一个错误:在 Facebook 开发人员的门户上,我没有添加发布令牌。

简而言之,检查是否已在 facebook 开发者门户上停用SandBox,以及是否添加了调试和发布令牌。

于 2013-07-21T10:57:49.573 回答
0

我认为您不是通过提供应用程序 ID 来创建会话。试试这个

Session session = SessionClass.createSession(getApplicationContext());
OpenRequest openRequest = new OpenRequest(activity);
        openRequest.setPermissions(Arrays.asList("user_birthday", "email"));
        openRequest.setCallback(statusCallback);
        session.openForRead(openRequest);

public static Session createSession(Context context) {
    Session activeSession = Session.getActiveSession();
    if (activeSession == null || activeSession.getState().isClosed()) {
        activeSession = new Session.Builder(context).setApplicationId("255105497966505").build();
        Session.setActiveSession(activeSession);
    }
    return activeSession;
}
于 2013-07-14T08:03:29.040 回答
0
Also Try UiLifeCycleHelper class.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);

        this.uiHelper = new UiLifecycleHelper(this, callback);
        this.uiHelper.onCreate(savedInstanceState);
}

    @Override
    public void onResume() {
        super.onResume();
        // For scenarios where the main activity is launched and user
        // session is not null, the session state change notification
        // may not be triggered. Trigger it if it's open/closed.
        Session session = Session.getActiveSession();
        if (session != null && (session.isOpened() || session.isClosed()) ) {
            onSessionStateChange(session, session.getState(), null);
        }
        uiHelper.onResume();
    }

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

    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        uiHelper.onDestroy();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }
于 2013-07-14T10:21:33.583 回答