The Facebook SDK for Android enables people to sign into your app with Facebook Login. When people log into your app with Facebook they can grant permissions to your app so you can retrieve information or perform actions on Facebook on their behalf.
For an example project that illustrates how to integrate Facebook Login into an Android app, see the FBLoginSample on GitHub.
Follow the steps below to add Facebook Login to your app.
buildscript { repositories {}}: dependencies{} section to depend on the latest version of the Facebook Login SDK: If you use version 5.15 or later of the Facebook SDK for Android, you don't need to to add an activity or intent filter for Chrome Custom Tabs. This functionality is included in the SDK.
After you integrate Facebook Login, certain App Events are automatically logged and collected for Events Manager, unless you disable Automatic App Event Logging. In particular, when launching an app in Korea, please note that Automatic App Event Logging can be disabled. For details about what information is collected and how to disable automatic app event logging, see Automatic App Event Logging.
FacebookActivity to your Android manifest. /app/res/values/strings.xml file.string elements with the names facebook_app_id, fb_login_protocol_scheme and facebook_client_token, and set the values to your App ID and Client Token. For example, if your app ID is 1234 and your client token is 56789 your code looks like the following: /app/manifest/AndroidManifest.xml file. meta-data elements to the application element for your app ID and client token: application element: <activity android:name="com.facebook.FacebookActivity"
android:configChanges=
"keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name" />
<activity
android:name="com.facebook.CustomTabActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
uses-permission element to the manifest after the application element: uses-permission element to the manifest after the application element: You may directly set the auto-logging of App Events to “true” or “false” by setting the AutoLogAppEventsEnabled flag in the AndroidManifest.xml file.
LoginButton from the SDK. The LoginButton is a UI element that wraps functionality available in the LoginManager. When someone clicks on the button, the login is initiated with the permissions set in the LoginManager. Facebook Login requires advanced public_profile permission, to be used by external users. The button follows the login state, and displays the correct text based on someone's authentication state. CallbackManager.Factory.create. Login button and register a callback in your onCreate() or onCreateView() method. Properties you can customize includes LoginBehavior, DefaultAudience, ToolTipPopup.Style and permissions on the LoginButton. For example:
private static final String EMAIL = "email";
loginButton = (LoginButton) findViewById(R.id.login_button);
loginButton.setReadPermissions(Arrays.asList(EMAIL));
// If you are using in a fragment, call loginButton.setFragment(this);
// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});LoginManager or LoginButton. If you register the callback with LoginButton, don't need to register the callback on Login manager. onCreate() method: callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});LoginResult parameter has the new AccessToken, and the most recently granted or declined permissions. registerCallback for login to succeed, you can choose to follow current access token changes with the AccessTokenTracker class described below. onActivityResult method, call callbackManager.onActivityResult to pass the login results to the LoginManager via callbackManager. If you are using AndroidX activities or fragments, you don't have to override onActivityResult.
onActivityResult to the callbackManager. LoginManager sets the current AccessToken and Profile for that person. The FacebookSDK saves this data in shared preferences and sets at the beginning of the session. You can see if a person is already logged in by checking AccessToken.getCurrentAccessToken() and Profile.getCurrentProfile(). AccessToken.getCurrentAccessToken with the SDK from cache or from an app book mark when your app launches from a cold start. You should check its validity in your Activity's onCreate method: queries element in your /app/manifest/AndroidManifest.xml file. The following code shows how to enable express login. LoginManager.getInstance().retrieveLoginStatus(this, new LoginStatusCallback() {
@Override
public void onCompleted(AccessToken accessToken) {
// User was previously logged in, can log them in directly here.
// If this callback is called, a popup notification appears that says
// "Logged in as <User Name>"
}
@Override
public void onFailure() {
// No access token could be retrieved for the user
}
@Override
public void onError(Exception exception) {
// An error occurred
}
});