Search code examples
androidfacebookuserinfo

Fetching Facebook User Info and creating a Profile from it for Android SDK


I would like my facebook login button to automatically grab the user's facebook information and create a profile from it.

I have looked at the example provided by facebook, but I don't want to create separate fragments in a LinearLayout. I would just like to implement it into my RelativeLayout.

Can someone advise me on what to do? I searched everywhere and found deprecated answers and am new to the Facebook API :/

What method would I need to create to grab the user's information, and then send that information to a second activity class?

Here is some of the code I've written so far:

package com.example.app;

import com.facebook.*;
import com.facebook.model.GraphUser;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends Activity {    

@Override
/*
 * called when activity is first created
 */
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

/*
 * Create API call to facebook to grab user data and define a 
 * new callback to handle the response
 */
public void GrabFbUserInfo(View view){


    //Request call
    Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {

        @Override
        public void onCompleted(GraphUser user, Response response) {
            //If the response is successful
            if (session == Session.)

        }
    });

}

}

Here is the XML for activity_main which is contained inside RelativeLayout:

<com.facebook.widget.LoginButton
    android:id="@+id/fb_login_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="76dp"
    android:contentDescription="@string/loginButton"
    facebook:confirm_logout="false"
    facebook:fetch_user_info="true" />

Appreciate any advice!


Solution

  • Please try my library: https://github.com/antonkrasov/AndroidSocialNetworks

    With it's help it's really easy task.

    mSocialNetworkManager.getFacebookSocialNetwork().requestLogin(new OnLoginCompleteListener() {
        @Override
        public void onLoginSuccess(int socialNetworkID) {
            mSocialNetworkManager.getFacebookSocialNetwork().requestCurrentSocialPerson();
        }
    
        @Override
        public void onError(int socialNetworkID, String requestID, String errorMessage, Object data) {
    
        }
    });
    

    Check README for more info, please let me know if you will have any issues.