5

我試圖登錄到Facebook上,一旦用戶點擊一個按鈕,然後一旦成功登錄後,將創建新的活動。登錄Facebook的一個按鈕,打開一個新的活動

這是主要的活動到目前爲止的代碼!

import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.EditText; 
import android.widget.ImageButton; 
import android.widget.Toast; 

import com.facebook.LoggingBehavior; 
import com.facebook.Session; 
import com.facebook.SessionState; 
import com.facebook.Settings; 

public class MainActivity extends Activity { 

    @SuppressWarnings("unused") 
    private static final String URL_PREFIX_FRIENDS = "https://graph.facebook.com/me/friends?access_token="; 
    private Session.StatusCallback statusCallback = new SessionStatusCallback(); 
    private ImageButton fbImgButton; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     fbImgButton = (ImageButton)findViewById(R.id.imageView2); 

     Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS); 

     Session session = Session.getActiveSession(); 
     if(session == null) { 
      if(savedInstanceState != null) { 
       session = Session.restoreSession(this, null, statusCallback, savedInstanceState); 
      } 
      if(session== null) { 
       session = new Session(this); 
      } 
      Session.setActiveSession(session); 
      if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) { 
       session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback)); 
      } 
     } 
     updateView(); 

    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     Session.getActiveSession().addCallback(statusCallback); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     Session.getActiveSession().removeCallback(statusCallback); 
    } 

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

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     Session session = Session.getActiveSession(); 
     Session.saveSession(session, outState); 
    } 

    private void updateView() { 
     Session session = Session.getActiveSession(); 
     if(session.isOpened()) { 
      Toast.makeText(getApplicationContext(), "Logged in....", Toast.LENGTH_LONG).show(); 
      fbImgButton.setOnClickListener(new OnClickListener() { 
       public void onClick(View view) { loginfb(); } 
      }); 
     } else { 
      fbImgButton.setOnClickListener(new OnClickListener() { 
       public void onClick(View view) { onClickLogin(); } 
      }); 
     } 
    } 

    public void loginfb() { 
     //start the new activity here 
     //Toast.makeText(getApplicationContext(), "Works first", Toast.LENGTH_LONG).show(); 
     Intent intent = new Intent(MainActivity.this, SelectionFragment.class); 
     MainActivity.this.startActivity(intent); 

    } 

    private void onClickLogin() { 
     Session session = Session.getActiveSession(); 
     if (!session.isOpened() && !session.isClosed()) { 
      session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback)); 
     } else { 
      Session.openActiveSession(this, true, statusCallback); 
     } 
     Intent intent = new Intent(MainActivity.this, SelectionFragment.class); 
     MainActivity.this.startActivity(intent); 
    } 

    public void shareOnTwitter(View v) { 
     EditText widget40 = (EditText)findViewById(R.id.widget40); 
     Uri uri = Uri.parse("https://twitter.com/intent/tweet?text=" + " @coolfacts_fb " + widget40.getText()); 
     Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
     startActivity(intent); 
    } 

    public void nofb(View v) { 
     Intent intent = new Intent(v.getContext(), shareTw.class); 
     startActivityForResult(intent, 0); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 

    } 

    private class SessionStatusCallback implements Session.StatusCallback { 
     @Override 
     public void call(Session session, SessionState state, Exception exception) { 
      updateView(); 
     } 
    } 
} 

問題是,無論用戶當前狀態如何,只要我點擊Facebook登錄按鈕就去新的活動!

有人可以幫我嗎?

+0

是不是像有'session'不打開你是不是能夠進入'onClickLogin()'方法? –

+0

它去,因爲這些線正在努力onclicklogin()方法: 意向意圖=新意圖(MainActivity.this,SelectionFragment.class); MainActivity.this.startActivity(intent); – defiant

+0

@oDx在開始新活動之前是否最終獲得了Facebook身份驗證的工作? –

回答

1

在「onClickLogin()」方法結束時,在Session有機會打開之前調用startActivity()。會議在後臺開始,但新的活動將彈出它的頂部。

在您的代碼中,它看起來像您的updateView()方法是對已打開的Session作出反應的正確位置。如果Session狀態爲OPENED,則考慮在那裏調用startActivity()。

2

試試這個:

public class MainActivity extends Activity { 

    @SuppressWarnings("unused") 
    private static final String URL_PREFIX_FRIENDS = "https://graph.facebook.com/me/friends?access_token="; 
    private Session.StatusCallback statusCallback = new SessionStatusCallback(); 
    private ImageButton fbImgButton; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     fbImgButton = (ImageButton)findViewById(R.id.imageView2); 

     Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS); 

     // Add Session Callback Here 
     Session.getActiveSession().addCallback(statusCallback); 

     Session session = Session.getActiveSession(); 
     if(session == null) { 
      if(savedInstanceState != null) { 
       session = Session.restoreSession(this, null, statusCallback, savedInstanceState); 
      } 
      if(session== null) { 
       session = new Session(this); 
      } 
      Session.setActiveSession(session); 
      if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) { 
       session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback)); 
      } 

      // Get Avtive Session again in case of session is null 
      Session session = Session.getActiveSession(); 
     } 

    // Dont call updateView() here as we have called this from session callback already. 
    // updateView(); 

    } 

    @Override 
    public void onStart() { 
     super.onStart(); 
     Session.getActiveSession().addCallback(statusCallback); 
    } 

    @Override 
    public void onStop() { 
     super.onStop(); 
     Session.getActiveSession().removeCallback(statusCallback); 
    } 

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

    @Override 
    protected void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 
     Session session = Session.getActiveSession(); 
     Session.saveSession(session, outState); 
    } 

    // Minor changes made with updateView() Method flow 
    private void updateView() { 
     Session session = Session.getActiveSession();  
     fbImgButton.setOnClickListener(new OnClickListener() { 
      public void onClick(View view) {  
      if(session.isOpened()){ 
       Toast.makeText(getApplicationContext(), "loginfb...", Toast.LENGTH_LONG).show(); 
       loginfb(); 
      } 
      else{ 
       Toast.makeText(getApplicationContext(), "onClickLogin...", Toast.LENGTH_LONG).show(); 
       onClickLogin(); 
      } 
      } 
     }); 
    } 

    public void loginfb() { 
     //start the new activity here 
     //Toast.makeText(getApplicationContext(), "Works first", Toast.LENGTH_LONG).show(); 
     Intent intent = new Intent(MainActivity.this, SelectionFragment.class); 
     MainActivity.this.startActivity(intent); 

    } 

    private void onClickLogin() { 
     Session session = Session.getActiveSession(); 
     if (!session.isOpened() && !session.isClosed()) { 
      session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback)); 
     } else { 
      Session.openActiveSession(this, true, statusCallback); 
     } 
     Intent intent = new Intent(MainActivity.this, SelectionFragment.class); 
     MainActivity.this.startActivity(intent); 
    } 

    public void shareOnTwitter(View v) { 
     EditText widget40 = (EditText)findViewById(R.id.widget40); 
     Uri uri = Uri.parse("https://twitter.com/intent/tweet?text=" + " @coolfacts_fb " + widget40.getText()); 
     Intent intent = new Intent(Intent.ACTION_VIEW, uri); 
     startActivity(intent); 
    } 

    public void nofb(View v) { 
     Intent intent = new Intent(v.getContext(), shareTw.class); 
     startActivityForResult(intent, 0); 

    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 

    } 

    private class SessionStatusCallback implements Session.StatusCallback { 
     @Override 
     public void call(Session session, SessionState state, Exception exception) { 
      updateView(); 
     } 
    } 
} 

檢查本作的onCreate()和在onStart()的區別。它清楚地顯示onCreate()在onStart()之前調用,因此我們還需要從onCreate()方法添加會話回調。

Difference between onCreate() and onStart()

注意:您可以讓您的UpdateView()方法,因爲它也是,有什麼不對邏輯。

希望這可以幫助你。

謝謝。

+0

當我添加Session session = Session.getActiveSession();到oncreate()方法,它使應用程序強制關閉 – defiant

+0

我將如何重新創建這個,這樣當這個活動啓動它只記錄一個用戶,如果他們點擊無論登錄按鈕是什麼? – ChuckKelly

+0

@oDx在嘗試添加到不存在的會話之前,您必須先檢查它是否先返回null。 –

相關問題