博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android--Facebook Login without LoginButton
阅读量:5127 次
发布时间:2019-06-13

本文共 4526 字,大约阅读时间需要 15 分钟。

1.引入facebook 工程lib,在Manifest中声明com.facebook.LoginActivity,facebook_app_id

2.调用方式:

1).初始化facebookLogin,设置回调

private FacebookLogin facebookLogin;facebookLogin = new FacebookLogin(this);        facebookLogin.setLoginCallBack(new FacebookLogin.LoginCallBack()        {            @Override            public void onPreLogin()            {                showLoadingDlg();            }                        @Override            public void onLoginSuccess(GraphUser user)            {                                dismissLoadingDlg();                Toast.makeText(getApplicationContext(), user.getName()+"========", Toast.LENGTH_LONG).show();            }                        @Override            public void onError()            {                dismissLoadingDlg();            }        });

 

2).重写onActivityResult方法

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

3).调用facebookLogin.login()方法,登录成功后会调用onLoginSuccess方法返回用户信息user

facebookLogin.login(); 

3.工具类FacebookLogin.java代码:

import android.app.Activity;import android.content.Context;import android.content.Intent;import com.facebook.Request;import com.facebook.Response;import com.facebook.Session;import com.facebook.SessionState;import com.facebook.Request.GraphUserCallback;import com.facebook.model.GraphUser;public class FacebookLogin{    private Activity activity;    public FacebookLogin(Activity activity)    {        this.activity = activity;    }    private LoginCallBack loginCallBack;    public void onActivityResult(int requestCode, int resultCode, Intent data)    {        onActivityResult(activity, requestCode, resultCode, data);    }    /**     * Login     */    public void login()    {        login(activity);    }    /**     * onActivityResult     * @param context     * @param requestCode     * @param resultCode     * @param data     */    private void onActivityResult(Context context, int requestCode, int resultCode, Intent data)    {        if (context instanceof Activity)        {            Session.getActiveSession().onActivityResult((Activity) context, requestCode, resultCode, data);        }    }    /**     * Login     * @param context     */    private void login(Context context)    {        Session.openActiveSession((Activity) context, true, new Session.StatusCallback()        {            // callback when session changes state            @Override            public void call(Session session, SessionState state, Exception exception)            {                if (session.isOpened())                {                    getUserInfo(session);                }            }        });        if (loginCallBack != null)        {            loginCallBack.onPreLogin();        }    }    private void getUserInfo(final Session session)    {        Request request = Request.newMeRequest(session, new GraphUserCallback()        {            @Override            public void onCompleted(GraphUser user, Response response)            {                try                {                    if (loginCallBack != null)                    {                        if (user != null)                        {                            loginCallBack.onLoginSuccess(user);                        }                        else                        {                            loginCallBack.onError();                        }                    }                }                catch (Exception e)                {                    e.printStackTrace();                    loginCallBack.onError();                }            }        });        request.executeAsync();    }    /**     * Logout from Facebook     */    public void logout()    {        Session session = Session.getActiveSession();        if (session != null)        {            if (!session.isClosed())            {                session.closeAndClearTokenInformation();            }        }        else        {            session = new Session(activity);            Session.setActiveSession(session);            session.closeAndClearTokenInformation();        }    }    public void setLoginCallBack(LoginCallBack loginCallBack)    {        this.loginCallBack = loginCallBack;    }    public interface LoginCallBack    {        void onPreLogin();        void onLoginSuccess(GraphUser user);        void onError();    }}

  

转载于:https://www.cnblogs.com/crane13/p/4079343.html

你可能感兴趣的文章
Vue路由编程式导航以及hash模式
查看>>
野派,阡陌人生路,泪断愁肠,滚滚红尘中,情留心房
查看>>
表单项
查看>>
JavaSE Map的使用
查看>>
VMWare9下基于Ubuntu12.10搭建Hadoop-1.2.1集群—整合Zookeeper和Hbase
查看>>
idea+maven+Struts2 之struts.xml中标签介绍
查看>>
Nodejs的介绍
查看>>
js 禁止表单提交的方法(文件上传)
查看>>
[ZT] 医学图像分析相关的会议
查看>>
159. Longest Substring with At Most Two Distinct Characters
查看>>
C# 杀掉后台进程
查看>>
Powershell按文件最后修改时间删除多余文件
查看>>
Bristol的第24篇密码学
查看>>
css 常用框架
查看>>
一个由request/response构成的static web server
查看>>
【Vue报错】Module build failed: Error: No parser and no file path given, couldn't infer a p arser....
查看>>
vs项目打不开
查看>>
递归,匿名函数,内置函数
查看>>
Linux应用总结(1):自动删除n天前日志
查看>>
NOIP2008T3 传球游戏
查看>>