Search code examples
androidfacebook-android-sdk

Unable to integrate facebook login in android


Hi All I'm creating a demo app in which i'm trying to integrate facebook login. I've done everything I came up with but it's not working properly . One can login but it doesn't show any status/message. This is My MainActivity :

package com.example.manishnegi.logindemo;



public class MainActivity extends ActionBarActivity {
LoginButton loginButton;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FacebookSdk.sdkInitialize(getApplicationContext());
    setContentView(R.layout.activity_main);
    callbackManager = CallbackManager.Factory.create();
    loginButton = (LoginButton)findViewById(R.id.login_button);
    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {

        @Override
        public void onSuccess(LoginResult loginResult) {
            // App code
            Toast.makeText(getApplicationContext(),"LOGIN SUCCESS",Toast.LENGTH_SHORT).show();
            Log.e("Status","Success");
        }

        @Override
        public void onCancel() {
            Toast.makeText(getApplicationContext(),"LOGIN CANCEL",Toast.LENGTH_SHORT).show();
            Log.e("Status","Cancelled");
            // App code
        }

        @Override
        public void onError(FacebookException exception) {
            Toast.makeText(getApplicationContext(),"LOGIN ERROR : "+exception.getMessage(),Toast.LENGTH_SHORT).show();
            Log.e("Status","Error");
            Log.e("Exception ",exception.getMessage());
            // App code
        }
    });
}


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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
@Override
protected void onResume() {
    super.onResume();

    // Logs 'install' and 'app activate' App Events.
    AppEventsLogger.activateApp(this);
}
@Override
protected void onPause() {
    super.onPause();

    // Logs 'app deactivate' App Event.
    AppEventsLogger.deactivateApp(this);
}
}

This is Manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.manishnegi.logindemo" >
<uses-permission android:name="android.permission.INTERNET"/>
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/facebook_app_id"/>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="com.facebook.FacebookActivity"
        android:configChanges=
            "keyboard|keyboardHidden|screenLayout|screenSize|orientation"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:label="@string/app_name" />
</application>

Ideally there should be one of the message success,cancelled or error, but it simply logs in without generating any message. May be I'm missing something please help. Thank you


Solution

  • You have call onActivityResult() for forwarding the login results to the callbackManager created in onCreate():-

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
         callbackManager.onActivityResult(requestCode, resultCode, data);
    }
    

    Every activity and fragment that you integrate with the FacebookSDK Login or Share should forward onActivityResult to the callbackManager.

    If you want access facebook profile then must give public_profile permission in developer console.

    And do this entry in AndroidManifest file.

    <provider
        android:name="com.facebook.FacebookContentProvider"
        android:authorities="com.facebook.app.FacebookContentProviderandyourappidfromfacebookconsole"
        android:exported="true" />
    

    for more Info follow this link. I hope this work for you. Good Luck