2012-07-02 49 views
0

我在我的android應用程序中有一個Facebook登錄選項。如何在使用android-facebook api時忽略安裝在設備上的Facebook android應用程序?

當我的設備上沒有安裝Facebook應用程序時,此登錄工作正常。但是,當安裝facebook應用程序時,會在少數情況下產生問題。

那麼,如何告訴我的應用程序的Facebook登錄,以忽略如果Facebook應用程序已安裝並以前面的方式繼續(假設Facebook應用程序未安裝)?

這是當我點擊fbLoginButton稱爲活動:

public class Example extends Activity { 

    public static final String APP_ID = myAppIdHere; 


    private LoginButton mLoginButton; 
    private TextView mText; 
    private Button mRequestButton; 
    private Button mPostButton; 
    private Button mDeleteButton; 
    private Button mUploadButton; 
    public static Activity ExampleActivity; 

    private Facebook mFacebook; 
    private AsyncFacebookRunner mAsyncRunner; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     ExampleActivity = this; 
     if (APP_ID == null) { 
      Util.showAlert(this, "Warning", "Facebook Applicaton ID must be " + 
        "specified before running this example: see Example.java"); 
     } 

     setContentView(R.layout.facebook); 
     mLoginButton = (LoginButton) findViewById(R.id.login); 
     mFacebook = new Facebook(APP_ID); 
     mAsyncRunner = new AsyncFacebookRunner(mFacebook); 

     SessionStore.restore(mFacebook, this); 
     SessionEvents.addAuthListener(new SampleAuthListener()); 
     SessionEvents.addLogoutListener(new SampleLogoutListener()); 
     mLoginButton.init(this, mFacebook); 

    } 
    @Override 
    protected void onActivityResult(int requestCode, int resultCode, 
            Intent data) { 

     mFacebook.authorizeCallback(requestCode, resultCode, data); 
    } 

    public class SampleAuthListener implements AuthListener { 

     public void onAuthSucceed() { 

     } 

     public void onAuthFail(String error) { 

     } 
    } 

    public class SampleLogoutListener implements LogoutListener { 
     public void onLogoutBegin() { 

     } 

     public void onLogoutFinish() { 

    } 
} 

這是LoginButton類:

public class LoginButton extends ImageButton { 

    public static Facebook mFb; 
    public static String facebookID; 
    public static String firstName; 
    public static String lastName; 
    public static String email = ""; 
    public static String sex; 
    private Handler mHandler; 
    private SessionListener mSessionListener = new SessionListener(); 
    private String[] mPermissions = new String[] { "read_stream", "email" }; 
    private Activity mActivity; 

    public LoginButton(Context context) { 
     super(context); 
    } 

    public LoginButton(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public LoginButton(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public void init(final Activity activity, final Facebook fb) { 
     init(activity, fb, new String[] {}); 
    } 

    public void init(final Activity activity, final Facebook fb, 
      final String[] permissions) { 
     mActivity = activity; 
     mFb = fb; 
     mPermissions = new String[] { "read_stream", "email" }; 
     ; 
     mHandler = new Handler(); 

     setBackgroundColor(Color.TRANSPARENT); 
     setAdjustViewBounds(true); 

     if(mFb.isSessionValid()){ 
      getUser(); 
      SessionEvents.onLogoutBegin(); 
      AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(mFb); 
      asyncRunner.logout(getContext(), new LogoutRequestListener()); 
     }else{ 

      setImageResource(R.drawable.login_button); 
      drawableStateChanged(); 
     } 

     SessionEvents.addAuthListener(mSessionListener); 
     SessionEvents.addLogoutListener(mSessionListener); 
     setOnClickListener(new ButtonOnClickListener()); 
    } 

    private final class ButtonOnClickListener implements OnClickListener { 

     public void onClick(View arg0) { 
      if (mFb.isSessionValid()) { 
       getUser(); 
       SessionEvents.onLogoutBegin(); 
       AsyncFacebookRunner asyncRunner = new AsyncFacebookRunner(mFb); 
       asyncRunner.logout(getContext(), new LogoutRequestListener()); 
      } else { 
       mFb.authorize(mActivity, mPermissions, 
         new LoginDialogListener()); 
      } 
     } 
    } 

    private final class LoginDialogListener implements DialogListener { 
     public void onComplete(Bundle values) { 

      SessionEvents.onLoginSuccess(); 
     } 

     public void onFacebookError(FacebookError error) { 

      SessionEvents.onLoginError(error.getMessage()); 
     } 

     public void onError(DialogError error) { 

      SessionEvents.onLoginError(error.getMessage()); 
     } 

     public void onCancel() { 

      SessionEvents.onLoginError("Action Canceled"); 
     } 

    } 

    private class LogoutRequestListener extends BaseRequestListener { 
     public void onComplete(String response, final Object state) { 

      mHandler.post(new Runnable() { 
       public void run() { 

       SessionEvents.onLogoutFinish(); 
       } 
      }); 
     } 
    } 

    private class SessionListener implements AuthListener, LogoutListener { 

     public void onAuthSucceed() { 

      setImageResource(R.drawable.logout_button); 
      SessionStore.save(mFb, getContext()); 
     } 

     public void onAuthFail(String error) { 
     } 

     public void onLogoutBegin() { 
     } 

     public void onLogoutFinish() { 

      SessionStore.clear(getContext()); 
      setImageResource(R.drawable.login_button); 
     } 
    } 

    public static void getUser() { 

     try { 
      JSONObject json = Util.parseJson(mFb.request("me")); 
      facebookID = json.getString("id"); 
      firstName = json.getString("first_name"); 
      lastName = json.getString("last_name"); 
      email = json.getString("email"); 
      sex = json.getString("gender"); 
      String mFirstName = firstName; 
      String mLastName = lastName; 
      String mEmail = email; 
      String mSex = sex; 
      Log.d("User Details", "You are logged in : " + facebookID 
        + mFirstName + "." + mLastName + " Email is: " + mEmail 
        + "-" + mSex); 

      SharedPreferences.Editor editor = SignUpActivity.settings.edit(); 

      // Set "hasLoggedIn" to true 
      editor.putBoolean("hasLoggedIn", true); 
      editor.putString("fbEmail", email); 
      editor.putString("fbId", facebookID); 
      editor.putString("fbSex", sex); 
      editor.putString("fbFirstname", firstName); 
      editor.putString("fbLastname", lastName); 

      // Commit the edits! 
      editor.commit(); 
     } catch (Exception e) { 
      // TODO: handle exception 
      Log.d("Exception in getUser", "+++++" + e.toString()); 
     } 
    } 
} 

這是SessionEvents類:

public class SessionEvents { 

    private static LinkedList<AuthListener> mAuthListeners = new LinkedList<AuthListener>(); 
    private static LinkedList<LogoutListener> mLogoutListeners = new LinkedList<LogoutListener>(); 

    public static void addAuthListener(AuthListener listener) { 

     mAuthListeners.add(listener); 
    } 

    public static void removeAuthListener(AuthListener listener) { 

     mAuthListeners.remove(listener); 
    } 

    public static void addLogoutListener(LogoutListener listener) { 

     mLogoutListeners.add(listener); 
    } 

    public static void removeLogoutListener(LogoutListener listener) { 

     mLogoutListeners.remove(listener); 
    } 

    public static void onLoginSuccess() { 

     LoginButton.getUser(); 
     for (AuthListener listener : mAuthListeners) { 
      listener.onAuthSucceed(); 
     } 
    } 

    public static void onLoginError(String error) { 

     for (AuthListener listener : mAuthListeners) { 
      listener.onAuthFail(error); 
     } 
    } 

    public static void onLogoutBegin() { 

     for (LogoutListener l : mLogoutListeners) { 
      l.onLogoutBegin(); 
     } 
    } 

    public static void onLogoutFinish() { 

     for (LogoutListener l : mLogoutListeners) { 
      l.onLogoutFinish(); 
     } 
    } 

    public static interface AuthListener { 

     public void onAuthSucceed(); 

     public void onAuthFail(String error); 
    } 

    public static interface LogoutListener { 

     public void onLogoutBegin(); 

     public void onLogoutFinish(); 
    } 

} 

這是SessionStore類:

public class SessionStore extends Activity{ 

    private static final String TOKEN = "access_token"; 
    private static final String EXPIRES = "expires_in"; 
    private static final String KEY = "facebook-session"; 

    public static boolean save(Facebook session, Context context) { 

     Context contextObj = Example.ExampleActivity; 
     Intent i=new Intent(contextObj , SignUpActivity.class); 
     contextObj.startActivity(i); 

     Editor editor = 
      context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit(); 
     editor.putString(TOKEN, session.getAccessToken()); 
     editor.putLong(EXPIRES, session.getAccessExpires()); 
     return editor.commit(); 
    } 

    public static boolean restore(Facebook session, Context context) { 

     SharedPreferences savedSession = 
      context.getSharedPreferences(KEY, Context.MODE_PRIVATE); 
     session.setAccessToken(savedSession.getString(TOKEN, null)); 
     session.setAccessExpires(savedSession.getLong(EXPIRES, 0)); 
     return session.isSessionValid(); 
    } 

    public static void clear(Context context) { 

     Editor editor = 
      context.getSharedPreferences(KEY, Context.MODE_PRIVATE).edit(); 
     editor.clear(); 
     editor.commit(); 
    } 

} 

這是BaseRequestListener類:

public abstract class BaseRequestListener implements RequestListener { 

    public void onFacebookError(FacebookError e, final Object state) { 
     Log.e("Facebook", e.getMessage()); 
     e.printStackTrace(); 
    } 

    public void onFileNotFoundException(FileNotFoundException e, 
      final Object state) { 
     Log.e("Facebook", e.getMessage()); 
     e.printStackTrace(); 
    } 

    public void onIOException(IOException e, final Object state) { 
     Log.e("Facebook", e.getMessage()); 
     e.printStackTrace(); 
    } 

    public void onMalformedURLException(MalformedURLException e, 
      final Object state) { 
     Log.e("Facebook", e.getMessage()); 
     e.printStackTrace(); 
    } 
} 
+1

毫無疑問這將是更好的解決比避免這個問題? Facebook應用程序安裝時發生了什麼? – Rawkode

+0

當我點擊我的應用程序中的Facebook登錄按鈕...它帶我到一個fbLoginButton ...點擊這個需要我到Facebook應用程序登錄屏幕(如果fb應用程序已安裝,否則將採取對話)。 ...當我輸入我的FB憑據,並打開去...它要求允許應用程序使用我的信息,我點擊好吧...然後它帶我回到fbLoginButton ....我再次點擊它並一次又一次地做同樣的過程......在第三次或第四次嘗試之後,它會登錄到我的應用程序中....所以最近怎麼了? –

+0

如果你想我可以粘貼代碼和圖像 –

回答

2

請更新下面的登錄按鈕點擊收聽的代碼。

mFb.authorize(mActivity, mPermissions, Facebook.FORCE_DIALOG_AUTH, 
      new LoginDialogListener()); 

代替

mFb.authorize(mActivity, mPermissions, new LoginDialogListener()); 

,看看下面的鏈接瞭解更多信息

Facebook Login Issue

+0

事實上,我想張貼在牆壁上和所有的東西....我只是想從他/她輸入的憑證中獲得用戶詳細信息 –

+0

請參閱我編輯的答案。 –

+0

這是做什麼?? .....它會顯示對話框而不是fb應用登錄屏幕? –

相關問題