简体   繁体   中英

Android - Facebook log in outside of an Activity with your own button

EDIT: When I click my button and call fbLogin() The app automatically logs in but I still can't access my onSuccess() method and get it to print "success", is it beacuse I'm not doing it from my activities onCreate method, but how do I do that when Im using a button? (Im extending my Activity):

public class AndroidPlatform extends AndroidLauncher implements PlatformSpecific{

        private Activity context;
        private CallbackManager callbackManager;

        public AndroidPlatform(Activity context){
            this.context = context;
            callbackManager = CallbackManager.Factory.create();
        }

        @Override
        public void fbLogin() {

        System.out.println("Hello Android");

            String a[] = new String[]{"user_friends"};
            LoginManager.getInstance().logInWithReadPermissions(context, Arrays.asList(a)); //Log in to FB
            LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    System.out.println("Success");
                }

                @Override
                public void onCancel() {
                    System.out.println("Cancel");
                }

                @Override
                public void onError(FacebookException e) {
                    System.out.println("Error");
                }
            });
        }

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

    }

For clarity here is the class I extend (my activity):

public class AndroidLauncher extends AndroidApplication {
    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
        initialize(new MyGdxGame(new AndroidPlatform(this)), config);
    }
}

If I can't access that method I cannot get any information on the user, The problem is I have to do it in this class, I can't do it in my activity, is there a way I can make that work?

You should be overriding onActivityResult so that when Facebook returns control to your activity, the activity will be able to process the result using the callback manager that you registered.

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

See https://developers.facebook.com/docs/facebook-login/android/v2.4#callback_handling for an example with the LoginButton.

To log in to facebook using your own button from a class that is not an Activity you can do the following:

The class you log in from:

    public AndroidPlatform(Activity context){
        this.context = context;
        callbackManager = CallbackManager.Factory.create();
    }

    @Override
    public void fbLogin() {

        String a[] = new String[]{"user_friends"};
        LoginManager.getInstance().logInWithReadPermissions(context, Arrays.asList(a)); //Log in to FB
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                System.out.println("Success");
                GraphRequest request = GraphRequest.newMeRequest(
                        loginResult.getAccessToken(),
                        new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(
                                    JSONObject object,
                                    GraphResponse response) {
                                // Application code
                                Log.v("LoginActivity", response.toString());
                            }
                        });
                Bundle parameters = new Bundle();
                parameters.putString("fields", "id,name,email,gender, birthday");
                request.setParameters(parameters);
                request.executeAsync();
            }

            @Override
            public void onCancel() {
                System.out.println("Cancel");
            }

            @Override
            public void onError(FacebookException e) {
                System.out.println("Error");
            }
        });

    }

    public CallbackManager getCM(){
        return callbackManager;
    }

This is pretty standard, what is key is that you make a get-method for your CallbackManager in your class so you can call onActivityResult from your Activity like so:

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

And that's all there is to it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM