Created
April 8, 2016 15:13
-
-
Save codeasashu/b5e94e799f4ee1bdef8160c33ff2ecd2 to your computer and use it in GitHub Desktop.
Facebook login in SDK v 4.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" | |
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" | |
android:paddingRight="@dimen/activity_horizontal_margin" | |
android:paddingTop="@dimen/activity_vertical_margin" | |
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> | |
<com.facebook.login.widget.LoginButton | |
android:id="@+id/login_button" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_gravity="center_horizontal" | |
android:layout_marginTop="30dp" | |
android:layout_marginBottom="30dp" /> | |
</RelativeLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Necessary imports | |
... | |
//Facebook imports | |
import com.facebook.AccessToken; | |
import com.facebook.AccessTokenTracker; | |
import com.facebook.CallbackManager; | |
import com.facebook.FacebookSdk; | |
import com.facebook.Profile; | |
import com.facebook.login.widget.LoginButton; | |
public class MainActivity extends ActionBarActivity { | |
CallbackManager callbackManager; | |
LoginButton loginButton; | |
AccessTokenTracker accessTokenTracker; | |
//Constants | |
public static int SPLASH_TIME_OUT = 2000; | |
String TAG = "MainActivity"; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
FacebookSdk.sdkInitialize(getApplicationContext()); | |
callbackManager = CallbackManager.Factory.create(); | |
setContentView(R.layout.activity_main); | |
loginButton = (LoginButton) findViewById(R.id.login_button); | |
//Init Access token Tracker | |
//This method tracks the tokens and updates login/logout status | |
updateWithToken(AccessToken.getCurrentAccessToken()); | |
//Token Tracker | |
accessTokenTracker = new AccessTokenTracker() { | |
@Override | |
protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken, AccessToken newAccessToken) { | |
updateWithToken(newAccessToken); | |
} | |
}; | |
} | |
private void updateWithToken(AccessToken currentAccessToken) { | |
if (currentAccessToken != null) { | |
new Handler().postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
//Use Intent to forward to screen where user should be, once logged in | |
Log.e(TAG,"Logged in"); | |
Profile userProfile = Profile.getCurrentProfile(); | |
Log.e(TAG,userProfile.getName()); | |
} | |
}, SPLASH_TIME_OUT); | |
} else { | |
new Handler().postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
//User is logged out now!! Handle in your way | |
Log.e(TAG,"Logged out"); | |
} | |
}, SPLASH_TIME_OUT); | |
} | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
super.onActivityResult(requestCode, resultCode, data); | |
//This line below is really necessary | |
callbackManager.onActivityResult(requestCode, resultCode, data); | |
} | |
... //Other Methods | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment