There are many changes in new sdk release by facebook for app
integration.New Facebook sdk has simplified many things from older versions.
So what we have to do to integrate facebook in our app with new sdk
follows :
Download the facebook sdk from this link :
Include latest Facebook sdk in your workspace.Some libraries you may
have to include in order to make sdk run.that will be ;
1. android-support-v4.jar
2. bolts-android-1.1.2.jar (this can be found from older sdk libs
folder).
clean the imported sdk and all set now.
now create your android project and include facebook sdk in your project
and finally your project structure will go like this :
now from code side .
1. MainActivity.java
public class MainActivity extends ActionBarActivity { Button mFacebookloginbtn; private CallbackManager callbackManager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initiViews(); } private void initiViews() { mFacebookloginbtn=(Button)findViewById(R.id.Facebookloginbtn); FacebookSdk.sdkInitialize(MainActivity.this.getApplicationContext()); callbackManager = CallbackManager.Factory.create(); LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback() { @Override public void onSuccess(LoginResult loginResult) { Log.e("loginresult", loginResult.toString()); if(Profile.getCurrentProfile()!=null){ Log.e("Pofile",Profile.getCurrentProfile().getName()+Profile.getCurrentProfile().getFirstName()+Profile.getCurrentProfile().getLastName()+Profile.getCurrentProfile().getId()); }else{ Profile.fetchProfileForCurrentAccessToken(); Log.e("Pofile",Profile.getCurrentProfile().getName()+Profile.getCurrentProfile().getFirstName()+Profile.getCurrentProfile().getLastName()+Profile.getCurrentProfile().getId()); } } @Override public void onCancel() { } @Override public void onError(FacebookException exception) { } }); mFacebookloginbtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { LoginManager.getInstance().logInWithReadPermissions(MainActivity.this,Arrays.asList("public_profile", "user_friends")); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); callbackManager.onActivityResult(requestCode, resultCode, data); }
1. activity_main.xml
Output Screen :
Things to be noticed
here are :
1.LoginManager.getInstance().logInWithReadPermissions(MainActivity.this,
Arrays.asList("public_profile",
"user_friends"));
: this lone
will make us start with login in facebook.
Define your app
permissions here read,write,publish etc.
2. FacebookSdk.sdkInitialize(MainActivity.this.getApplicationContext());
: This line
will initialize our facebook sdk and Initialize our
sdk for our app.
3. You have
to implement a callback named LoginManager in
order to get the
login response here . Which goes like this :
LoginManager.getInstance().registerCallback(callbackManager,new
FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult
loginResult) {
}
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException
exception) {
}
});
Here you have to pass
a CallbackManager interface which can be
initialize like this
:
private CallbackManager callbackManager;
callbackManager =CallbackManager.Factory.create();
4. Now if we
want some info regarding user then we can get like
this on login success
callback :
if(Profile.getCurrentProfile()!=null){
Log.e("Profile<><><>",
Profile.getCurrentProfile().getName()+Profile.getCurrentProfile().getFirstName()+Profile.getCurrentProfile().getLastName()+Profile.getCurrentProfile().getId());
}else{
Profile.fetchProfileForCurrentAccessToken();
Log.e("Profile<><><>",
Profile.getCurrentProfile().getName()+Profile.getCurrentProfile().getFirstName()+Profile.getCurrentProfile().getLastName()+Profile.getCurrentProfile().getId());
}
Here we have to use
Profile class define in facebook sdk to fetch the
information.
5. Resultcode
inside onActivityResult after login success will
be : 64206
Which can be used if
we've to handle multiple conditions inside
onActivityResult
Cheers!!!
No comments:
Post a Comment