如何在 android 中注销后清除 Facebook 字段(图像、姓名、性别)

Posted

技术标签:

【中文标题】如何在 android 中注销后清除 Facebook 字段(图像、姓名、性别)【英文标题】:How can i clear the Facebook fields(image, name, gender) after Logout in android 【发布时间】:2017-06-20 04:52:32 【问题描述】:

我无法清除 Facebook 的字段。我显示了图像、名称、性别。当我点击 Facebooklogout 按钮时,我想清除这三个字段。谁能帮我解决这个问题??

公共类 MainActivity 扩展 AppCompatActivity

private LoginButton loginButton;
private CallbackManager callbackManager;
private ProfilePictureView profilePictureView;
private LinearLayout infoLayout;
private TextView email;
private TextView gender;
private TextView username;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

/*We have to initialize before using any methods with sdkIntialise()*/
    FacebookSdk.sdkInitialize(MainActivity.this);

    loginButton = (LoginButton) findViewById(R.id.login_button);
    email = (TextView) findViewById(R.id.email);
    gender = (TextView) findViewById(R.id.gender);
    username = (TextView) findViewById(R.id.vname);

    infoLayout = (LinearLayout) findViewById(R.id.layout_info);
    infoLayout.setVisibility(View.GONE);
    profilePictureView = (ProfilePictureView) findViewById(R.id.vimage);
    loginButton.setReadPermissions(Arrays.asList("public_profile, email, user_birthday"));

    /*initialize an instance of CallbackManager*/
    callbackManager = CallbackManager.Factory.create();

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() 
        @Override
        public void onSuccess(LoginResult loginResult) 
            GraphRequest graphRequest = GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() 
                @Override
                public void onCompleted(JSONObject object, GraphResponse response) 
                    setProfileView(object);
                
            );

            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email,gender,birthday");
            graphRequest.setParameters(parameters);
            graphRequest.executeAsync();
        

        @Override
        public void onCancel() 

        

        @Override
        public void onError(FacebookException error) 
            Toast.makeText(MainActivity.this, "Error to Login Facebook", Toast.LENGTH_SHORT).show();
        
    );




@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
    super.onActivityResult(requestCode, resultCode, data);
    callbackManager.onActivityResult(requestCode, resultCode, data);


private void setProfileView(JSONObject object) 

    try 
        email.setText(object.getString("email"));
        gender.setText(object.getString("gender"));
        username.setText(object.getString("name"));

        profilePictureView.setPresetSize(ProfilePictureView.NORMAL);
        profilePictureView.setProfileId(object.getString("id"));
        infoLayout.setVisibility(View.VISIBLE);
     catch (JSONException e) 
        e.printStackTrace();
    

【问题讨论】:

看看这个答案***.com/questions/30039232/… 【参考方案1】:

您好,注销您可以使用以下代码: 获取当前访问令牌,然后调用 fb 注销函数。

> if (AccessToken.getCurrentAccessToken() != null) 
>       LoginManager.getInstance().logOut();  
> 

注销后,您必须刷新布局或启动相同的活动,您需要以下代码:

    Intent openMainActivity = new Intent(MainActivity.this, MainActivity.class);openMainActivity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(openMainActivity);

希望对你有帮助

【讨论】:

但我想清除 UI 上的字段,我正在显示 facebook 字段图像、姓名、电子邮件、ID。当我单击登录时,它显示所有这些字段。但是当我注销时,所需的信息仍然显示..我怎样才能清除这些字段? 通过带有标志 clear_task new_task 的意图重新启动您的活动

以上是关于如何在 android 中注销后清除 Facebook 字段(图像、姓名、性别)的主要内容,如果未能解决你的问题,请参考以下文章

使用 SAML 单次注销 (SLO) 调用注销 URL 后如何清除 Android 网络视图 cookie?

浏览历史记录清除网页后注销[重复]

如何在 Ktor-client for Android 中清除不记名令牌

node express,注销后如何清除cookie

如何以编程方式“重新启动”android应用程序[重复]

注销后清除 Firebase 持久性