Android Facebook login Using Facebook sdk3.0

First we check Facebook session in open or not Useing this

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCurrContext = this;
setContentView(R.layout.fb_activity_main);
facebook_button = (Button) findViewById(R.id.LikeFB_Button);

Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
        fb_session = Session.openActiveSessionFromCache(mCurrContext);

If Your Facebook session in null and not open state click this button to login facebook

          Button button =(Button) findViewById(R.id.button_share);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (fb_session != null && fb_session.isOpened()) {
makeMeRequest(fb_session);
Log.i("Facebook Login State == >", "Facebook Login State");
} else {
if (fb_session == null) {
fb_session = new Session(mCurrContext);          
       }
Session.setActiveSession(fb_session);
ConnectToFacebook();
  Log.i("Facebook not Login State == >", "Facebook Not login State");
}
}
});
   }

This methods used for Login to Facebook Session

  private void ConnectToFacebook(){
Session session = Session.getActiveSession();
        if (!session.isOpened() && !session.isClosed()) {
        Log.i("ConnectToFacebook  if == >", "ConnectToFacebook if");
        OpenRequest newSession = new Session.OpenRequest(this);
        newSession.setCallback(callback);
        session.openForRead(newSession);          
        try {
        Session.OpenRequest request = new Session.OpenRequest(this);
        request.setPermissions(Arrays.asList("email"));
        } catch (Exception e) {
        e.printStackTrace();
        }
        } else {
        Log.i("ConnectToFacebook  else == >", "ConnectToFacebook else");
            Session.openActiveSession(this, true, callback);        
        }
}

private Session.StatusCallback callback = new Session.StatusCallback() {
       public void call(final Session session, final SessionState state, final Exception exception) {
           onSessionStateChange(session, state, exception);
       }
   };
 
   private void onSessionStateChange(final Session session, SessionState state, Exception exception) {
       if (session != null && session.isOpened()) {
        makeMeRequest(session);
       }
   }
 
   private void makeMeRequest(final Session session) {
     Request request = Request.newMeRequest(session, new Request.GraphUserCallback() {
    public void onCompleted(GraphUser user, Response response) {
    try{
      //UserInfoDisplay(user);
    //FrieddsInfoDisplay(session);
       } catch (Exception e) {
    e.printStackTrace();
    }
    }
    });              
       request.executeAsync();
   }

NOTE:

InManifestfile you Implement facebook login activity like this

  <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/app_id" />

And your App id create create in  res/values/string
  Like this

<resources>   
    <string name="app_id">FACEBOOKAPPLICATIONID</string> 
</resources> 

3 comments:

Anonymous said...

Hola! I've been following your weblog for a while now and finally got the courage to go ahead and give you a shout out from Lubbock Tx!

Just wanted to say keep up the great job!


Also visit my blog - homepage ()

Anonymous said...

java.lang.NullPointerException: Argument 'applicationId' cannot be null

Unknown said...

Hi, I am using a simple FBlogin to get user email details but my issue is that it works fine when native FB app is not installed & if fb app is installed then fb login function doesn't work if the user is not added in my fb app tester or admin list.It is only fetching data if I have added the user in list. My app is live at fb but I am still facing this issue. I have tried stackoverflow but still not getting my answers.It would be helpful if you guide me on this. My hashkey generate process is same but FB SDk is official one and nott the github one.Thanks.

Select DateRange UsingRangePicker.

  /* * This Method is for select range from picker. * */ private fun selectDateRangeUsingRangePicker () { pageNumber = 1 val displ...