简体   繁体   中英

How can I login Facebook SDK 4.3 with my own button?

I want to login Facebook SDK 4.3 with my own button but I'm facing some problems. Here is my code

public class MainActivity extends Activity {
    CallbackManager callbackManager;
    ShareDialog shareDialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        callbackManager = CallbackManager.Factory.create();
        Button bt= (Button)findViewById(R.id.button1);
        shareDialog = new ShareDialog(this);

        bt.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                openFB();
            }
        });
    }

    public void openFB() {
        //Login Callback registration
        LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {
                Toast.makeText(getApplicationContext(), "in LoginResult on success", Toast.LENGTH_LONG).show();

                //Login success - process to Post
                if (ShareDialog.canShow(ShareLinkContent.class)) {
                    String description = "description";
                    ShareLinkContent linkContent = new ShareLinkContent.Builder()
                            .setContentTitle("title")
                            .setContentDescription(description)
                            .setContentUrl(Uri.parse("http://google.com"))
                            .setImageUrl(Uri.parse("http://google.com"))
                            .build();

                    shareDialog.show(linkContent, ShareDialog.Mode.FEED);
                }

            }

            @Override
            public void onCancel() {
                Toast.makeText(getApplicationContext(), "in LoginResult on cancel", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onError(FacebookException exception) {
                Toast.makeText(getApplicationContext(), "in LoginResult on error", Toast.LENGTH_LONG).show();
            }
        });

        LoginManager.getInstance().logInWithReadPermissions(this, "user_friends"); //Log in to FB
    }
}

I get this error at the last line:

The method logInWithReadPermissions(Fragment, Collection) in the type LoginManager is not applicable for the arguments (MainActivity, String)

As you know there is another logInWithReadPermissions method with 2 parameters are (Activity,Collection) but I don't know I get this error. Please help me to fix this!

For someone who encounter the same problem with me, just change the last line like this:

LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends")); //Log in to FB

and it should be fine.

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