facebook成功登录但未重定向到我的应用程序

时间:2014-02-07 09:21:41

标签: android facebook facebook-login

我是Android应用中的菜鸟。我正在创建一个应用程序,使用https://developers.facebook.com/docs/android/send-requests中定义的方法向我的Facebook好友发送应用请求。 现在我无法登录并访问我的应用程序。提供用户名和密码后,我登录到Facebook,我的Facebook应用程序请求许可。按下确定后,它不会重定向到发送请求窗口,而是再次向我显示登录窗口,就像我没有登录一样。

这是我的完整代码。请帮帮我。

invite_friends.xml

<Button
android:id="@+id/btn_fb_invite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:layout_marginTop="54dp"
android:background="@drawable/button_click_color_white"
android:text="@string/btn_fb_invite"
/>  

Invite.java

public class Invite extends FragmentActivity {

    private Button pickFriendsButton;
    private UiLifecycleHelper lifecycleHelper;
    boolean pickFriendsWhenSessionOpened;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final ActionBar actionBar = getActionBar();
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayUseLogoEnabled(false);
        actionBar.hide();
        setContentView(R.layout.invite_friends);

        pickFriendsButton = (Button) findViewById(R.id.btn_fb_invite);
        pickFriendsButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                //onClickPickFriends();

                if (ensureOpenSession()) {

                    sendRequestDialog();
                }
                else {
                    pickFriendsWhenSessionOpened = true;

                }
            }
        });

        lifecycleHelper = new UiLifecycleHelper(this, new Session.StatusCallback() {
            @Override
            public void call(Session session, SessionState state, Exception exception) {
                Toast.makeText(getApplicationContext(), "lifecycle helper", Toast.LENGTH_SHORT).show();
                onSessionStateChanged(session, state, exception);
            }
        });
        lifecycleHelper.onCreate(savedInstanceState);

       // ensureOpenSession();


    }



    public void contactInvite(View view)
    {
        Intent intent = new Intent(getApplicationContext(),FindPhnFriends.class);
        startActivity(intent);
    }


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

        AppEventsLogger.activateApp(this);
    }


    private boolean ensureOpenSession() 
    {Toast.makeText(this, "ensure open session method", Toast.LENGTH_SHORT).show();
        if (Session.getActiveSession() == null || !Session.getActiveSession().isOpened()) 
        {Toast.makeText(this, "no active session or active session is opened", Toast.LENGTH_SHORT).show();
            Session.openActiveSession(Invite.this, true, new Session.StatusCallback() 
            {
                @Override
                public void call(Session session, SessionState state, Exception exception) 
                {Toast.makeText(getApplicationContext(), "open active session", Toast.LENGTH_SHORT).show();
                    onSessionStateChanged(session, state, exception);

                }
            });
            return false;
        }
        return true;
    }

    private void onSessionStateChanged(Session session, SessionState state, Exception exception) {
        if (state.isOpened()) {
            pickFriendsWhenSessionOpened = false;
            Toast.makeText(getApplicationContext(), "session state changed", Toast.LENGTH_SHORT).show();
            sendRequestDialog();
        }
        else
        {
            Toast.makeText(this, "session state not changed", Toast.LENGTH_SHORT).show();
        }
    }



    private void sendRequestDialog() {
        Bundle params = new Bundle();
        params.putString("message", "Learn how to make your Android apps social");

        WebDialog requestsDialog = (
            new WebDialog.RequestsDialogBuilder(this,
                Session.getActiveSession(),
                params))
                .setOnCompleteListener(new OnCompleteListener() {

                    @Override
                    public void onComplete(Bundle values,
                        FacebookException error) {
                        if (error != null) {
                            if (error instanceof FacebookOperationCanceledException) {
                                Toast.makeText(getApplicationContext(), 
                                    "Request cancelled", 
                                    Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(getApplicationContext(), 
                                    "Network Error", 
                                    Toast.LENGTH_SHORT).show();
                            }
                        } else {
                            final String requestId = values.getString("request");
                            if (requestId != null) {
                                Toast.makeText(getApplicationContext(), 
                                    "Request sent",  
                                    Toast.LENGTH_SHORT).show();
                            } else {
                                Toast.makeText(getApplicationContext(), 
                                    "Request cancelled", 
                                    Toast.LENGTH_SHORT).show();
                            }
                        }   
                    }

                })
                .build();
        requestsDialog.show();
    }

}

1 个答案:

答案 0 :(得分:0)

我发现并回答了我的问题,现在是:

在ensureOpenSession方法中, 用

替换条件
if (Session.getActiveSession() != null || Session.getActiveSession().isOpened())

并在onSessionStateChanged方法中,

替换

if (state.isOpened())

if (session.isOpened() && state.isOpened())