2014-02-07 62 views
2

我是Android應用程序的noob。我正在創建一個應用程序,它使用https://developers.facebook.com/docs/android/send-requests中定義的方法向我的Facebook好友發送應用程序請求。 現在我無法登錄和訪問我的應用程序。提供用戶名和密碼後,我登錄到Facebook和我的Facebook應用程序要求許可。按OK後,它不會重定向發送請求窗口,而是再次顯示登錄窗口,就好像我沒有登錄。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(); 
    } 

} 

回答

0

我發現,並回答我的問題,那就是:在

方法ensureOpenSession

, 與

更換條件
if (Session.getActiveSession() != null || Session.getActiveSession().isOpened()) 

和onSessionStateChanged方法,

取代

if (state.isOpened()) 

if (session.isOpened() && state.isOpened()) 
相關問題