如何在android app中注销后清除facebook用户数据

时间:2013-08-18 23:00:49

标签: android

Following this

使用facebook sdk 3.0,这里成功登录后能够获得facebook用户的电子邮件并在textview中显示但是在注销后如何清除数据。这里在注销后附加屏幕截图。screenshot after logout

这里放置代码

public class MainActivity extends Activity {

    private String TAG = "MainActivity";
    private TextView lblEmail;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lblEmail = (TextView) findViewById(R.id.lblEmail);

        LoginButton authButton = (LoginButton) findViewById(R.id.authButton);
        authButton.setOnErrorListener(new OnErrorListener() {

            @Override
            public void onError(FacebookException error) {
                Log.i(TAG, "Error " + error.getMessage());
            }
        });
        // set permission list, Don't foeget to add email
        authButton.setReadPermissions(Arrays.asList("basic_info", "email"));
        // session state call back event
        authButton.setSessionStatusCallback(new Session.StatusCallback() {

            @Override
            public void call(Session session, SessionState state,
                    Exception exception) {

                if (session.isOpened()) {
                    Log.i(TAG, "Access Token" + session.getAccessToken());
                    Request.executeMeRequestAsync(session,
                            new Request.GraphUserCallback() {
                                @Override
                                public void onCompleted(GraphUser user,
                                        Response response) {
                                    if (user != null) {
                                        Log.i(TAG, "User ID " + user.getId());
                                        Log.i(TAG,
                                                "Email "
                                                        + user.asMap().get(
                                                                "email"));
                                        lblEmail.setText(user.asMap()
                                                .get("email").toString());
                                    }
                                }
                            });
                }

            }
        });
    }

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

}

`

2 个答案:

答案 0 :(得分:0)

按照此链接修复此问题

`private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) {
        userInfoTextView.setVisibility(View.VISIBLE);
    } else if (state.isClosed()) {
        userInfoTextView.setVisibility(View.INVISIBLE);
    }
}`

here

答案 1 :(得分:0)

使用以下代码。设置一个按钮,然后单击该按钮关闭并清除以logoutFacebook()方法完成的令牌信息,之后您必须到达signIn屏幕,因此您必须使用launchSignIn()方法您将导航到signIn屏幕并使用清除任务          Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK

package com.example.integration;

import com.facebook.Session;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class ThirdActivity extends Activity{

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

        Button logoutBtn = (Button) findViewById(R.id.logout_btn);
        logoutBtn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                logout();
            }
        });
        Toast.makeText(ThirdActivity.this, "In Third Activity", Toast.LENGTH_SHORT).show();
    }

    public void logout(){
        logoutFacebook(ThirdActivity.this);
        launchSignIn(ThirdActivity.this);
    }

    private void launchSignIn(Context context) {
        if (context != null) {
            // Launch LoginActivity
            Intent loginActivity = new Intent(context, MainActivity.class);
            loginActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TASK);
            context.startActivity(loginActivity);
        }

    }

    private void logoutFacebook(Context context) {
        Session session = Session.getActiveSession();
        if (session != null) {
            if (!session.isClosed()) {
                session.closeAndClearTokenInformation();
                // clear your preferences if saved
            }
        } else {
            session = new Session(context);
            Session.setActiveSession(session);
            session.closeAndClearTokenInformation();
            // clear your preferences if saved
        }

    }   

}