Android--Facebook Login without LoginButton

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

<activity
            android:name="com.facebook.LoginActivity"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

<meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/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();
    }
}

  

posted @ 2014-11-06 16:58  crane13  阅读(653)  评论(0编辑  收藏  举报