问题
There are a few other old SO questions asking a similar question, but there doesn't seem to be any definitive answer for them. Some time has passed since those questions were asked, so I was wondering if there was a way to do this yet?
Looking at Facebook's Android SDK, there are examples on how to share things on your wall, post pictures to your wall, login, logout, etc... but it there doesn't appear to be anything about adding a simple Like button... I was expecting to be able to add a Like Button to my app (via a ImageView or maybe a styled Button), once clicked Facebook would load an authorization login dialog and then ask for your permission to Like the app.
It seems like something they would have in there... am I just not seeing it?
Some people have suggested making a tiny WebView that is the size of the HTML Facebook Like button and integrating it into your layout. The problem with this, is that when the user clicks the Like button in the WebView, it will want to open a Javascript window for the user to login to Facebook, but this dialog will almost definitely not fit in the tiny WebView.
Are there any definitive approaches to adding a FaceBook Like button in an Android App?
回答1:
The Like button can be used to like a Facebook Page or any Open Graph object and can be referenced by URL or ID. Here's what the code looks like. documentation android facebook sdk In your Activity or Fragment's onCreate method, use either the UiLifecycleHelper or call Settings.sdkInitialize:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(this, callback);
// if you don't want to use the UiLifecycleHelper, call sdkInitialize instead
// Settings.sdkInitialize(this);
...
Then set the object ID for the Like button (this can be a URL or a Facebook ID):
LikeView likeView = (LikeView) findViewById(R.id.like_view);
likeView.setObjectId("http://shareitexampleapp.parseapp.com/photo1/");
Lastly, call the UiLifecycleHelper again in your onActivityResult method
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data, null);
// if you don't use the UiLifecycleHelper, call handleOnActivityResult on the LikeView instead
// LikeView.handleOnActivityResult(this, requestCode, resultCode, data);
...
回答2:
Well, I tried that a week ago. There is a "/like" graph method, which returns an error since it is not possible to like something from the SDK. So you have no choice but launching the website.
来源:https://stackoverflow.com/questions/7690345/facebook-like-button-in-android-app