开发者

Facebook Like button in Android Application

I want to display like button in my android application. Below is the code I use to displa开发者_如何学JAVAy a Like button in my android application.

String url = "http://www.facebook.com/plugins/like.php?layout=standard&show_faces=true&width=80&height=50&action=like&colorscheme=light&href=http://beta.demo.dy/web/service/304.htm"
webview = (WebView) findViewById(R.id.webView1);
webview.loadUrl(url);
webview.setWebViewClient(new LikeWebviewClient());

public class LikeWebviewClient  extends WebViewClient {
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

But when I run this application it displays a white area. How to resolve this?


The like option can be implemented easily on Native platform languages(Android, iOS) as well as browsers (curl, PHP, JavaScript) as follows. Go to developer.facebook app section and within the Open Graph section of the Developer App configuration, add the built-in Like action, which should appear in the drop-down when adding a new Open Graph action. Refer here for latest updates.

Once done, Select "Get Code" option to retrieve sample code as depicted below for Android platform. You can choose platform of your choice. Note that app_specific_unique_identifier is uniquee for apps, i have removed it for security reasons and you have to use the one for your app.

I have been able to test the Like flow successfully. Hope this helps.

Bundle params = new Bundle();
params.putString("object", "http://samples.ogp.me/<app_specific_unique_identifier>");

Request request = new Request(
    Session.getActiveSession(),
    "me/og.likes",
    params,
    HttpMethod.POST
);
Response response = request.executeAndWait();
// handle the response


Facebook SDK hasn't provided like button feature for native mobile apps( As of Oct 17 2011). It is available in Mobile Web apps. For more info you can check out these link:
Mobile - Facebook Developers
Like Button - Facebook Developers


Finally Facebook and Launched Like Button for Android

Steps:

1 - Add Facebook Library to Project

2 - Create App on Facebook 3 - Update Manifest

**In the Application tab add meta-data**

<meta-data
        android:name="com.facebook.sdk.ApplicationId"
        android:value="@string/fb_id" />

4 - Add LikeView in Layout

//activitymain.xml
<com.facebook.widget.LikeView
            android:id="@+id/like_view"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            >
        </com.facebook.widget.LikeView>

5 - ActivityMain.java

//set facebook page or link to this like button
LikeView likeView;
UiLifecycleHelper uiHelper;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activitymain);
    uiHelper = new UiLifecycleHelper(this, null);
    likeView = (LikeView) findViewById(R.id.like_view);
    likeView.setObjectId("https://www.facebook.com/<page_username>");//it can be any link

    likeView.setLikeViewStyle(LikeView.Style.STANDARD);
    likeView.setAuxiliaryViewPosition(LikeView.AuxiliaryViewPosition.INLINE);
    likeView.setHorizontalAlignment(LikeView.HorizontalAlignment.LEFT);

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    uiHelper.onActivityResult(requestCode, resultCode, data, null);

}

Output

Facebook Like button in Android Application


You can use facebook like button in android app using special permission from facebook developer account https://developers.facebook.com. Add your app here and submit app special permission. Go to app review and submit items for approval. click on start submission and after that select LIKE native button and submit all details they want like why you want get like permission , how your app will use this permission everything. If Facebook will approve your request then you can use facebook like button inside app.enter code here

      <com.facebook.share.widget.LikeView
         android:id="@+id/facebooklike"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content">
        </com.facebook.share.widget.LikeView>

After this you need to do some java code.

likeView = (LikeView) findViewById(R.id.facebooklike);
    likeView.setLikeViewStyle(LikeView.Style.STANDARD);            
   likeView.setAuxiliaryViewPosition(LikeView.AuxiliaryViewPosition.INLINE);
    likeView.setHorizontalAlignment(LikeView.HorizontalAlignment.CENTER);
    likeView.setObjectIdAndType("url of like page", LikeView.ObjectType.PAGE);

like functionality automatically call when you click on like button.

now you need to get response from like page that user like that page of unlike that page.

enter code here

public class FbLikes extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_fb_likes);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    try {
        if (resultCode == RESULT_OK) {
            // verify we're returning from like action
                // get action results
                bundle = data.getExtras().getBundle("com.facebook.platform.protocol.RESULT_ARGS");
                if (bundle != null) {
                    like = bundle.getBoolean("object_is_liked");// liked/unliked
                    bundle.getInt("didComplete");
                    bundle.getInt("like_count"); // object like count
                    bundle.getString("like_count_string");
                    bundle.getString("social_sentence");
                    bundle.getString("completionGesture"); // liked/cancel/unliked
                    Log.e(TAG, bundle.getString("social_sentence") + "");
                    Log.e(TAG, "likeornot" + bundle.getBoolean("object_is_liked") + "");
                    Log.e(TAG, "lcomplete" + bundle.getString("completionGesture") + "");
                    Log.e(TAG, "count" + bundle.getInt("like_count") + "");
                    Log.e(TAG, "countstr" + bundle.getString("like_count_string") + "");
                    Log.e(TAG, "did" + bundle.getInt("didComplete") + "");
                }
        }
    } catch (Exception e) {

    }
}

} this code will return everything you want from like functionality.


Try this one i made some modification

String url = "http://www.facebook.com/plugins/like.php?layout=standard&show_faces=true&width=80&height=50&action=like&colorscheme=light&href=http://beta.demo.dy/web/service/304.htm";
                 //   webview = (WebView) findViewById(R.id.webview);
                    webView.loadUrl(url);
                     webView.setWebViewClient(new WebViewClient());
                 class LikeWebviewClient  extends WebViewClient
                    {
                        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                            view.loadUrl(url);
                            return true;
                        }
                    }


Facebook has recently provided the feature of likebutton in facebook sdk 3.21.1. You can go through these links to download sdk and tutorial for implementation of likebutton.

Download SDK: http://developers.facebook.com/docs/android

Tutorial http://developers.facebook.com/docs/android/like-button

I hope it may solve your problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜