android studio接入facebook登录和分享

1.后台新建facebook ID
2.AndroidManifext.xml文件中添加

    
    
    
    

3.build.gradle文件加入

dependencies {
//fackbook 分享
compile ‘com.facebook.android:facebook-android-sdk:[5,6)’
}

4.onCreator方法中初始化
FtFaceBookLogin.getInstance().initFaceBook(activity);
5.调用登录
public static String loginFackBook() {//fackbook登录
if(((FtPay)Manager.getInstance().ftPay).activity!=null) {
FtFaceBookLogin.getInstance().faceBookLogin(((FtPay) Manager.getInstance().ftPay).activity);
}
return null;
}

6.登录回调添加

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);

FtFaceBookLogin.getInstance().setOnActivityResult(requestCode, resultCode, data);//fackbook 登录回调

}

7.新建FtFaceBookLogin.java文件
package com.ftaro.adapter;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;

import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;

import org.json.JSONObject;

import java.util.Arrays;

//fackbook 登录
public class FtFaceBookLogin {
private static CallbackManager mFaceBookCallBack;
private static FtFaceBookLogin sInstance;
private AccessToken mAccessToken;
private FtFaceBookLogin() {
}
/**
* 单例
*
* @return
*/
public static FtFaceBookLogin getInstance() {
if (sInstance == null) {
synchronized (FtFaceBookLogin.class) {
if (sInstance == null) {
sInstance = new FtFaceBookLogin();

            }
        }
    }
    return sInstance;
}
/**
 * 开始
 *
 * @param context
 */
public void faceBookLogin(Context context) {
    LoginManager.getInstance()
            .logInWithReadPermissions((Activity) context,
                    Arrays.asList("public_profile"));
}
/**
 * 初始化
 */
public void initFaceBook(final Context context) {
    mFaceBookCallBack = CallbackManager.Factory.create();
    if (mFaceBookCallBack != null) {
        LoginManager.getInstance().registerCallback(mFaceBookCallBack, new FacebookCallback() {
            @Override
            public void onSuccess(LoginResult loginResult) {//登录成功
                if (loginResult != null) {
                    Log.d("fackbook_login_success",loginResult.toString());
                    mAccessToken = AccessToken.getCurrentAccessToken();
                    Log.e("token", "token :" + mAccessToken.getToken() + "," + "user_id" + mAccessToken.getUserId());
                    Manager.thirdName2=mAccessToken.getUserId();
                    GraphRequest request = GraphRequest.newMeRequest(mAccessToken, new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            if (object != null) {
                                String id = object.optString( "id" ) ;   //比如:1565455221565
                                String name = object.optString( "name" ) ;  //比如:Zhang San
                                String gender = object.optString("gender") ;  //性别:比如 male (男)  female (女)
                                String emali = object.optString("email") ;  //邮箱:比如:[email protected]
                                //获取用户头像
                                JSONObject object_pic = object.optJSONObject( "picture" ) ;
                                JSONObject object_data = object_pic.optJSONObject( "data" ) ;
                                String photo = object_data.optString( "url" )  ;
                                //获取地域信息
                                String locale = object.optString( "locale" ) ;   //zh_CN 代表中文简体

                            }
                        }
                    }) ;



                }
            }

            @Override
            public void onCancel() {//登录取消
                Log.d("fackbook_login_onCancel","onCancel");
                Manager.thirdName2="1";
            }

            @Override
            public void onError(FacebookException error) {//登录失败
                Log.d("fackbook_login_onError","onError");
                Manager.thirdName2="1";
            }
        });

    }
}
/**
 * 设置登录结果回调
 *
 * @param requestCode 请求码
 * @param resultCode  结果码
 * @param data        数据
 */
public void setOnActivityResult(int requestCode, int resultCode, Intent data) {
    if (mFaceBookCallBack != null) {
        mFaceBookCallBack.onActivityResult(requestCode, resultCode, data);
    }
}

}

8.分享新建FtShare.java
package com.ftaro.adapter;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.onekeyshare.OnekeyShare;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.share.Sharer;
import com.facebook.share.model.ShareLinkContent;
import com.facebook.share.widget.ShareDialog;
import com.ftaro.tool.Share;
import com.ftaro.adapter.Manager;

public class FtShare implements Share {
public String shareTitle = “”;
public String shareText = “”;
private Activity activity;
private CallbackManager callbackManager;
private ShareDialog shareDialog;
public FtShare(Activity activity) {
this.activity = activity;
}
public void setText(String title, String text) {
shareText = text;
shareTitle = title;
callbackManager = CallbackManager.Factory.create();
shareDialog = new ShareDialog(activity);
// this part is optional
shareDialog.registerCallback(callbackManager, new FacebookCallback() {
@Override
public void onSuccess(Sharer.Result result) {
//分享成功的回调,在这里做一些自己的逻辑处理
//Manager.notifyShare(0);
}
@Override
public void onCancel() {
//Manager.notifyShare(2);
}
@Override
public void onError(FacebookException error) {
//Manager.notifyShare(1);
}
});
}
public void toShare() {
////////////fackbook 分享 /////////////////////
//这里分享一个链接,更多分享配置参考官方介绍:https://developers.facebook.com/docs/sharing/android
if(ShareDialog.canShow(ShareLinkContent.class)) {
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentUrl(Uri.parse(“https://play.google.com/store/apps/details?id=com.sinyuee.kingwar.google”))//https://developers.facebook.com
.build();
shareDialog.show(linkContent);
}
Manager.notifyShare(0);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if(callbackManager!=null) {
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
}

你可能感兴趣的:(Android,java)