Facebook的SDK安卓的onSuccess调用但OnCancel不叫 [英] Facebook SDK Android: OnSuccess called but OnCancel not called

查看:919
本文介绍了Facebook的SDK安卓的onSuccess调用但OnCancel不叫的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了我的应用程序一个Facebook登录。成功登录后,的onSuccess()回调被调用。然而,当我从Facebook注销时,onCancel()回调不叫虽然注销成功。在日志中我看到了下面的行我点击注销后:

  01-24 17:22:07.801 952-952 /? W / SurfaceFlinger的:无法登录到二进制事件日志:溢出

我用的是API 23与Android 6.0和Facebook SDK 4.6.0试图在模拟器
以下是我的code:

 包com.example.ankur.pollutionandweathermonitor;进口android.Manifest;
进口android.content.Intent;
进口android.content.pm.PackageManager;
进口android.os.Bundle;
进口android.support.v4.app.ActivityCompat;
进口android.support.v4.content.ContextCompat;
进口android.support.v7.app.AppCompatActivity;
进口android.support.v7.widget.Toolbar;
进口android.util.Log;
进口android.view.Menu;
进口android.view.MenuItem;
进口android.widget.Toast;进口com.facebook.AccessToken;
进口com.facebook.CallbackManager;
进口com.facebook.FacebookCallback;
进口com.facebook.FacebookException;
进口com.facebook.FacebookSdk;
进口com.facebook.Profile;
进口com.facebook.login.LoginResult;
进口com.facebook.login.widget.LoginButton;公共类MainActivity扩展AppCompatActivity {
    // LoginButton和callbackManager是Facebook登录
    私人LoginButton loginButton;
    CallbackManager callbackManager;
    / *的firstName是存储的登录Facebook的用户*的名字/
    私有String的firstName;
    / *轮廓和的accessToken是获得的登录用户当前的会话令牌* /
    私人档案的个人资料;
    私人的accessToken的accessToken;
    / * permissionStatus是存储用户的ACCESS_FINE_LOCATION许可* /
    私人布尔permissionStatus;
    / * EXTRA_MESSAGE_FIRSTNAME是发送登录Facebook的用户的名字在意向* /
    公共最后静态字符串EXTRA_MESSAGE_FIRSTNAME =USERNAME;
    / * EXTRA_MESSAGE_LOCATION_PERMISSION是在意图发送ACCESS_FINE_LOCATION允许值* /
    公共最后静态字符串EXTRA_MESSAGE_LOCATION_PERMISSION =权限;
    / *在这个类生成的日志都通过标签LOGTAG访问* /
    私人字符串LOGTAG = MainActivity.class.getSimpleName();
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        //初始化的Facebook SDK进行登录
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        的setContentView(R.layout.activity_main);
        //调用CheckFacebookToken()函数来查看是否有一个登录的用户了。
        //该函数返回的名字,如果有一个用户登录,否则返回null
        的firstName = CheckFacebookToken();
        如果(名字!= NULL){
            //如果有活跃的当前会话,然后检查用户是否授予
            // ACCESS_FINE_LOCATION许可。此检查仅适用于Android> = 6.0
            //对于Android和LT; 6,权限可以放到AndroidMenifestFile
            CheckLocationPermission();
        }
        //创建Facebook的登录按钮
        loginButton =(LoginButton)findViewById(R.id.login_button);
        尝试{
            loginButton.setReadPermissions(user_friends);            loginButton.registerCallback(callbackManager,新FacebookCallback< LoginResult>(){
                @覆盖
                公共无效的onSuccess(LoginResult loginResult){
                    Log.v(LOGTAG,成功连接至Facebook);
                    //登录成功后,获取登录的用户的名字
                    的firstName = CheckFacebookToken();
                    //如果有活跃的当前会话,然后检查用户是否授予
                    // ACCESS_FINE_LOCATION许可。此检查仅适用于Android> = 6.0
                    //对于Android和LT; 6,权限可以放到AndroidMenifestFile
                    CheckLocationPermission();
                }                @覆盖
                公共无效onCancel(){
                    someCallback();
                    Log.v(LOGTAG,连接至Facebook取消);
                }                @覆盖
                公共无效onerror的(FacebookException除外){
                    Toast.makeText(getApplicationContext(),FB登录错误,Toast.LENGTH_LONG);
                    Log.v(LOGTAG,关于连接至Facebook错误);
                }
            });
        }赶上(例外五){
            Log.v(LOGTAG,在loginButton实错误);
            e.printStackTrace();
        }
        工具条工具栏=(栏)findViewById(R.id.toolbar);
        setSupportActionBar(工具栏);
    }
    //检查用户是否已授予的权限ACCESS_FINE_LOCATION。
    //此检查仅适用于Android> = 6.0。为Android 6;,权限可以放到AndroidMenifestFile
    保护无效CheckLocationPermission(){
        如果(ContextCompat.checkSelfPermission(这一点,Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED){
            ActivityCompat.requestPermissions(这一点,新的String [] {} Manifest.permission.ACCESS_FINE_LOCATION,100);
        }
        其他{
            //将用户重定向到活动DisplayInformation
            RedirectToDisplayInformation();
        }    }
    保护无效someCallback(){
        Log.v(LOGTAG,从Facebook注销);
    }
    //该函数返回CheckFacebookToken已登录的Facebook用户的名字,否则返回null
    保护字符串CheckFacebookToken(){
        //检查是否有一个活跃的Facebook会话
        字符串FNAME = NULL;
        尝试{
            的accessToken = AccessToken.getCurrentAccessToken();
            Log.v(LOGTAG,accessToken.getToken());        }赶上(例外五){
            e.printStackTrace();
        }
        //如果有一个活跃的Facebook会话,得到了名字和重定向用户到
        // DisplayInformation活动,通过意向发送用户名
        如果(的accessToken!= NULL){
            尝试{
                简介= Profile.getCurrentProfile();
                如果(配置文件!= NULL){
                    FNAME = profile.getFirstName();
                    Log.v(LOGTAG,这是一个活动会话登录的用户。+ FNAME);
                }
            }赶上(例外五){
                e.printStackTrace();
            }
        }
        其他{
            Log.v(LOGTAG,没有找到活动会话);
        }
        返回FNAME;
    }
    //功能RedirectToDisplayInformation将用户重定向到活动DisplayInformation
    //这样做的目的还发送的登录Facebook的用户的姓名
    保护无效RedirectToDisplayInformation(){
        Log.v(LOGTAG,成功调用该函数RedirectToDisplayInformation);
        意向意图=新意图(这一点,DisplayInformation.class);
        intent.putExtra(EXTRA_MESSAGE_LOCATION_PERMISSION,将String.valueOf(permissionStatus));
        intent.putExtra(EXTRA_MESSAGE_FIRSTNAME,名字);
        // startActivity(意向);
    }        //这个函数是登录尝试之后调用了Facebook的回调函数
    @覆盖
    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        super.onActivityResult(要求code,结果code,数据);
        callbackManager.onActivityResult(要求code,结果code,数据);
    }
    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.menu_main,菜单);
        返回true;
    }    @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        //处理动作栏项目点击这里。操作栏会
        //自动处理上点击主页/向上按钮,只要
        //你在AndroidManifest.xml中指定一个父活动。
        INT ID = item.getItemId();        // noinspection SimplifiableIfStatement
        如果(ID == R.id.action_settings){
            返回true;
        }        返回super.onOptionsItemSelected(项目);
    }
    //功能onRequestPermissionResult是一个回调PROMT用户允许或拒绝的
    //权限。此功能仅适用于Android&GT显著6
    @覆盖
    公共无效onRequestPermissionsResult(INT申请code,字符串权限[],INT [] grantResults){        Log.v(LOGTAGonRequestPermissionResult);        开关(要求code){
            案例100:{
                //如果请求被取消,结果数组是空的。
                如果(grantResults.length大于0
                        &功放;&安培; grantResults [0] == PackageManager.PERMISSION_GRANTED){
                    permissionStatus = TRUE;
                    Log.v(LOGTAG,许可授予状态是:+将String.valueOf(permissionStatus));
                }
                其他{
                    permissionStatus = FALSE;
                    Log.v(LOGTAG,权限被拒绝的状态是:+将String.valueOf(permissionStatus));                }
                RedirectToDisplayInformation();
                返回;
            }
        }
    }
}


解决方案

FacebookCallback onCancel()将会被调用,只有当您关闭登录对话框而不继续日志。

当你注销,档案和的accessToken将成为空,但onCancel不会被调用。

希望这有助于:)

I have implemented a facebook login for my app. After the successful login, the onSuccess() callback is called. However, When I logout from facebook, the onCancel() callback is not called though the logout is successful. In the logs I see the following line after I click the logout:

 01-24 17:22:07.801 952-952/? W/SurfaceFlinger: couldn't log to binary event log: overflow

I am using the API 23. Tried it on the Emulator with Android 6.0 and Facebook SDK 4.6.0 The following is my code:

package com.example.ankur.pollutionandweathermonitor;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

public class MainActivity extends AppCompatActivity {
    //LoginButton and callbackManager are for facebook login
    private LoginButton loginButton;
    CallbackManager callbackManager;
    /*firstName is to store the firstname of the logged in facebook user*/
    private String firstName;
    /*profile and accessToken are for to get the current session tokens of the logged in users*/
    private Profile profile;
    private AccessToken accessToken;
    /*permissionStatus is to store the ACCESS_FINE_LOCATION permission of the user*/
    private boolean permissionStatus;
    /*EXTRA_MESSAGE_FIRSTNAME is to send the firstname of the logged in facebook user in the intent*/
    public final static String EXTRA_MESSAGE_FIRSTNAME = "USERNAME";
    /*EXTRA_MESSAGE_LOCATION_PERMISSION is to send the ACCESS_FINE_LOCATION permission value in the intent*/
    public final static String EXTRA_MESSAGE_LOCATION_PERMISSION = "PERMISSION";
    /*Logs generated in this class are accessed through the Tag LOGTAG*/
    private String LOGTAG = MainActivity.class.getSimpleName();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //Initialize the facebook SDK for login
        FacebookSdk.sdkInitialize(getApplicationContext());
        callbackManager = CallbackManager.Factory.create();
        setContentView(R.layout.activity_main);
        //Call the CheckFacebookToken() function to see if there is a logged in user already.
        //The function returns the firstname if there is a logged in user, else returns null
        firstName = CheckFacebookToken();
        if(firstName != null) {
            //If there is an active current Session, then check if the user has granted the
            // ACCESS_FINE_LOCATION permission. This check is only for Android >= 6.0
            //For Android <6, the permission can be put in the AndroidMenifestFile
            CheckLocationPermission();
        }
        //create the facebook login Button
        loginButton = (LoginButton) findViewById(R.id.login_button);
        try{
            loginButton.setReadPermissions("user_friends");

            loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    Log.v(LOGTAG, "successfully connected to facebook");
                    //After the login is successful, get the firstname of the logged in user
                    firstName = CheckFacebookToken();
                    //If there is an active current Session, then check if the user has granted the
                    // ACCESS_FINE_LOCATION permission. This check is only for Android >= 6.0
                    //For Android <6, the permission can be put in the AndroidMenifestFile
                    CheckLocationPermission();
                }

                @Override
                public void onCancel() {
                    someCallback();
                    Log.v(LOGTAG, " connection to facebook cancelled");
                }

                @Override
                public void onError(FacebookException exception) {
                    Toast.makeText(getApplicationContext(), "Fb Login Error", Toast.LENGTH_LONG);
                    Log.v(LOGTAG, "Error on  connection to facebook");
                }
            });
        }catch (Exception e){
            Log.v(LOGTAG, "Error in the loginButton facebook");
            e.printStackTrace();
        }
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
    //check if the user has granted the ACCESS_FINE_LOCATION permission.
    // This check is only for Android >= 6.0. For Android <6, the permission can be put in the AndroidMenifestFile
    protected void CheckLocationPermission(){
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 100);
        }
        else{
            //Redirects the user to the Activity DisplayInformation
            RedirectToDisplayInformation();
        }

    }
    protected void someCallback(){
        Log.v(LOGTAG, "logged out from facebook");
    }
    //The function CheckFacebookToken returns the firstname of the logged in facebook user, else returns null
    protected String CheckFacebookToken(){
        //Check if there is an active facebook session
        String fName = null;
        try{
            accessToken = AccessToken.getCurrentAccessToken();
            Log.v(LOGTAG, accessToken.getToken());

        } catch (Exception e){
            e.printStackTrace();
        }
        //If there is an Active facebook session, get the firstname and redirect user to the
        //DisplayInformation Activity, send the username through the intent
        if(accessToken != null) {
            try {
                profile = Profile.getCurrentProfile();
                if(profile != null) {
                    fName = profile.getFirstName();
                    Log.v(LOGTAG, "There is an active session. The logged in user is  " + fName);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        else {
            Log.v(LOGTAG, "No Active sessions found");
        }
        return fName;
    }
    //The function RedirectToDisplayInformation redirects the user to the Activity DisplayInformation
    //The intent also sends the firstname of the logged in facebook user
    protected void RedirectToDisplayInformation(){
        Log.v(LOGTAG, "successfully called the function RedirectToDisplayInformation");
        Intent intent = new Intent(this, DisplayInformation.class);
        intent.putExtra(EXTRA_MESSAGE_LOCATION_PERMISSION, String.valueOf(permissionStatus));
        intent.putExtra(EXTRA_MESSAGE_FIRSTNAME,firstName );
        //startActivity(intent);
    }

        //This function is to call the callback functions of facebook after the login attempt
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    //The function onRequestPermissionResult is a callback to promt the user to either allow or deny the
    //permissions. This function is significant only for Android >6
    @Override
    public void onRequestPermissionsResult(int requestCode,String permissions[], int[] grantResults) {

        Log.v(LOGTAG, "onRequestPermissionResult");

        switch (requestCode) {
            case 100: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    permissionStatus = true;
                    Log.v(LOGTAG, "Permission Granted. Status is: " + String.valueOf(permissionStatus));
                }
                else {
                    permissionStatus = false;
                    Log.v(LOGTAG, "Permission Denied. Status is: " + String.valueOf(permissionStatus));

                }
                RedirectToDisplayInformation();
                return;
            }
        }
    }
}

解决方案

FacebookCallback onCancel() will be called only when you dismiss the login dialog without continuing log in.

When you logout, Profile and AccessToken would become null but onCancel will not be called.

Hope this helps :)

这篇关于Facebook的SDK安卓的onSuccess调用但OnCancel不叫的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆