0

我按照本教程http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/将 Facebook 连接到我的 Android 应用程序。我没有很多按钮,而是有一个用于登录和注销的按钮。

1)我第一次运行课程时,我可以登录然后成功注销。当我点击同一个按钮再次登录时,登录页面不会出现,但是会出现 Toast 文本“LOGGING IN”,这是loginToFacebook()我的if-else函数之后。因此,我认为,它应该像我第一次运行课程一样运行 facebook 登录页面。但是没有出现登录页面。

我做错了什么?我该怎么办?

2)以及如何String fbLoggedIn在登录后显示用户名而不是文本“CONNECTED!!” ?

public class FacebookActivity extends Activity{

private static String APP_ID = "";
private Facebook facebook;
private AsyncFacebookRunner mAsyncRunner;
String FILENAME = "AndroidSSO_data";
private SharedPreferences mPrefs;
private Button backButton;
private String name = "CONNECTED!!";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings_share);

    facebook = new Facebook(APP_ID);
    mAsyncRunner = new AsyncFacebookRunner(facebook);

    RelativeLayout fbButton = (RelativeLayout) findViewById(R.id.fbLayout);
    fbButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (!facebook.isSessionValid())
            {
            System.out.println("Not Connected. Clicked and Login.");    
            loginToFacebook();
            Toast.makeText(getApplicationContext(), "LOGGING IN", Toast.LENGTH_LONG).show();}

            else
            {
            System.out.println("Connected. Logged Out.");   
            logoutFromFacebook();
            Toast.makeText(getApplicationContext(), "LOGGED OUT", Toast.LENGTH_LONG).show();}
        }
    });     
}

public void loginToFacebook() {
    mPrefs = getPreferences(MODE_PRIVATE);
    String access_token = mPrefs.getString("access_token", null);
    long expires = mPrefs.getLong("access_expires", 0);

    if (access_token != null) {
        facebook.setAccessToken(access_token);
    }

    if (expires != 0) {
        facebook.setAccessExpires(expires);
    }

    if (!facebook.isSessionValid()) {   

        facebook.authorize(this, new String[] { "email", "publish_stream" }, new DialogListener() {

                    @Override
                    public void onCancel() {
                        // Function to handle cancel event
                    }

                    @Override
                    public void onComplete(Bundle values) {
                        // Function to handle complete event
                        // Edit Preferences and update facebook acess_token
                        SharedPreferences.Editor editor = mPrefs.edit();
                        editor.putString("access_token", facebook.getAccessToken());
                        editor.putLong("access_expires", facebook.getAccessExpires());
                        editor.commit();   

                        TextView fbUser = (TextView) findViewById(R.id.fbUser);
                        fbUser.setVisibility(View.VISIBLE);
                        String fbLoggedIn = name;
                        fbUser.setText(fbLoggedIn); 
                        Toast.makeText(getApplicationContext(), "LOGGED IN AS " + name, Toast.LENGTH_LONG).show();         
                    }

                    @Override
                    public void onError(DialogError error) {
                        // Function to handle error
                    }

                    @Override
                    public void onFacebookError(FacebookError fberror) {
                        // Function to handle Facebook errors
                    }

                });
    }
}

public void getProfileInformation() {
    mAsyncRunner.request("me", new RequestListener() {
        @Override
        public void onComplete(String response, Object state) {
            Log.d("Profile", response);
            String json = response;
            try {
                JSONObject profile = new JSONObject(json);
                // getting name of the user
                final String name = profile.getString("name");
                // getting email of the user
                final String email = profile.getString("email");

                runOnUiThread(new Runnable() {

                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "Name: " + name + "\nEmail: " + email, Toast.LENGTH_LONG).show();
                    }

                });

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onIOException(IOException e, Object state) { }

        @Override
        public void onFileNotFoundException(FileNotFoundException e, Object state) { }

        @Override
        public void onMalformedURLException(MalformedURLException e, Object state) { }

        @Override
        public void onFacebookError(FacebookError e, Object state) { }
    });
}

//logout from Facebook
public void logoutFromFacebook() {
    mAsyncRunner.logout(this, new RequestListener() {
        @Override
        public void onComplete(String response, Object state) {
            Log.d("Logout from Facebook", response);
            if (Boolean.parseBoolean(response) == true) {
                // User successfully Logged out
         }

        }

        @Override
        public void onIOException(IOException e, Object state) { }

        @Override
        public void onFileNotFoundException(FileNotFoundException e, Object state) { }

        @Override
        public void onMalformedURLException(MalformedURLException e, Object state) { }

        @Override
        public void onFacebookError(FacebookError e, Object state) { }
    });
 }

为了更清楚起见,我将我的 if-else 设置为这样(从上面的完整代码中提取)。

    RelativeLayout fbButton = (RelativeLayout) findViewById(R.id.fbLayout);
    fbButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (!facebook.isSessionValid())
            {
            System.out.println("Not Connected. Clicked and Login.");    
            loginToFacebook();
            Toast.makeText(getApplicationContext(), "LOGGING IN", Toast.LENGTH_LONG).show();}

            else
            {
            System.out.println("Connected. Logged Out.");   
            logoutFromFacebook();
            Toast.makeText(getApplicationContext(), "LOGGED OUT", Toast.LENGTH_LONG).show();}
        }
    }); 
4

1 回答 1

0

这是 Facebook 希望您使用他们的 API 采取的行为。您的登录方法会检查您是否已经登录过一次并且 Facebook 会话是否处于活动状态。如果会话处于活动状态,则它会设置 facebook 对象的属性并将其连接到 sences 后面,这样登录页面就不会再次显示。

所以你没有做错任何事,它应该以这种方式发生。

于 2012-11-21T16:08:49.620 回答