如何使用Facebook SDK 4.7实现的Facebook登录的机器人 [英] how to implement facebook login in android using facebook sdk 4.7

查看:176
本文介绍了如何使用Facebook SDK 4.7实现的Facebook登录的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图登录使用Android SDK中4.7的Facebook。 我曾尝试以下链接 http://www.theappguruz.com/blog/android-facebook-integration-tutorial http://www.androidhive.info/2012/03/android- Facebook的连接 - 教程/

I am trying to login with facebook using android sdk 4.7. I have tried the following link http://www.theappguruz.com/blog/android-facebook-integration-tutorial http://www.androidhive.info/2012/03/android-facebook-connect-tutorial/

推荐答案

这code工作对我来说,尝试一下,检查是否正在使用Facebook的SDK 4.7

This code works for me, try it out and check that you are using facebook sdk 4.7

package com.kushal.facebooklogin;

    import java.util.Arrays;
    import org.json.JSONException;
    import org.json.JSONObject;
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.TextView;
    import com.facebook.*;
    import com.facebook.login.LoginManager;
    import com.facebook.login.LoginResult;
    import com.facebook.login.widget.LoginButton;

    public class FacebookLogin extends FragmentActivity
    {
        private TextView tvfirst_name, tvlast_namee, tvfull_name, tvEmail;
        private CallbackManager callbackManager;
        LoginButton login_button;
        String email,name,first_name,last_name;

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            FacebookSdk.sdkInitialize(this.getApplicationContext());
            callbackManager = CallbackManager.Factory.create();

            setContentView(R.layout.main);

            tvfirst_name        = (TextView) findViewById(R.id.first_name);
            tvlast_namee        = (TextView) findViewById(R.id.last_name);
            tvfull_name         = (TextView) findViewById(R.id.full_name);
            tvEmail             = (TextView) findViewById(R.id.email);
            login_button        = (LoginButton) findViewById(R.id.login_button);

            login_button.setReadPermissions(Arrays.asList("public_profile","email"));
            login_button.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
            {
                @Override
                public void onSuccess(LoginResult loginResult)
                {
                    login_button.setVisibility(View.GONE);

                    GraphRequest graphRequest   =   GraphRequest.newMeRequest(loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback()
                    {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response)
                        {
                            Log.d("JSON", ""+response.getJSONObject().toString());

                            try
                            {
                                email       =   object.getString("email");
                                name        =   object.getString("name");
                                first_name  =   object.optString("first_name");
                                last_name   =   object.optString("last_name");

                                tvEmail.setText(email);
                                tvfirst_name.setText(first_name);
                                tvlast_namee.setText(last_name);
                                tvfull_name.setText(name);
                                LoginManager.getInstance().logOut();
                            }
                            catch (JSONException e)
                            {
                                e.printStackTrace();
                            }
                        }
                    });

                    Bundle parameters = new Bundle();
                    parameters.putString("fields", "id,name,first_name,last_name,email");
                    graphRequest.setParameters(parameters);
                    graphRequest.executeAsync();
                }

                @Override
                public void onCancel()
                {

                }

                @Override
                public void onError(FacebookException exception)
                {

                }
            });
        }

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

在XML设计如下

the xml design is as follow

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:facebook="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFF"
    android:gravity="center"
    android:orientation="vertical" >

    <com.facebook.login.widget.LoginButton
        android:id="@+id/login_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="5dp"
        />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:gravity="center_horizontal"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/first_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/last_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/full_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginLeft="10dp"
            android:textSize="18sp" />
    </LinearLayout>

</LinearLayout>

在mainefest文件如下:

the mainefest file is as follow:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.kushal.facebooklogin"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.NoTitleBar" >
        <activity
            android:name=".FacebookLogin"
            android:label="@string/app_name"
            android:windowSoftInputMode="adjustResize" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.facebook.FacebookActivity"
            android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
            android:label="@string/app_name"
            android:theme="@android:style/Theme.Translucent.NoTitleBar" />

        <meta-data
            android:name="com.facebook.sdk.ApplicationId"
            android:value="@string/app_id" />
    </application>

</manifest>

这篇关于如何使用Facebook SDK 4.7实现的Facebook登录的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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