Android Access Facebook Share feature

Source: Internet
Author: User
Tags openssl sha1

Android access to Facebook share


  1. Register as a fackbook developer first. Https://developers.facebook.com

  2. Login to Facebook Developer backend and click "Apps", "Add a New app", enter the app name "Teshshare" and select category to create the app

    650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/54/7E/wKiom1SEYwuA2wgAAABoJ6Ic9Fg362.jpg "title=" 1.png " alt= "Wkiom1seywua2wgaaaboj6ic9fg362.jpg"/>



    650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/54/7C/wKioL1SEY92gLrrfAAEWzyX8kg8669.jpg "title=" 2.png " alt= "Wkiol1sey92glrrfaaewzyx8kg8669.jpg"/>

  3. Click "Apps", "TestShare", the app we just created, then click on "Settings" on the left to make the relevant information settings.

    650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/54/7E/wKiom1SEY5iyJiW4AAMFzIEXFI4792.jpg "title=" 22.png "alt=" Wkiom1sey5iyjiw4aamfziexfi4792.jpg "/>

    Configure the following information

    (1). App's package name Com.test.share

    (2). The app's main activity name Com.test.share.MainActivity

    (3). Hash value of the signature file

    Eclipse Debug Signature Hash value get method open Mac terminal enter the following command

    keytool -exportcert -alias androiddebugkey -keystore ~/.android/debug.keystore | openssl sha1 -binary | openssl base64


    650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/54/7C/wKioL1SEZEThovfmAAE0ALMslYk919.jpg "title=" 23.png "alt=" Wkiol1sezethovfmaae0almslyk919.jpg "/>

    The formal signature needs to be added to the program by adding the following code signature after packaging, look at the console input values to copy this value to the background for configuration. Let's start with the debug signature hash of Eclipse. The code is as follows


  4. /** *  get the hash value of the signature key Facebook backstage needs to add this if this value is not on the back of the background, then it will share the failure prompt Key hash value is not  */public static  void getkeyhash (CONTEXT CTX) {// add code to print out the  Key hash    try {        packageinfo  info = ctx.getpackagemanager (). Getpackageinfo (                  "Com.test.share",  //Replace with your app's package name                  packagemanager.get_signatures);         for  (Signature signature : info.signatures)  {            messagedigest md =  messagedigest.getinstance ("SHA");             md.update (signature.tobytearRay ());             LOG.D ("KeyHash:",  Base64.encodetostring (Md.digest (),  base64.default));             }    } catch  (exception e)  {         }}

4. Download the Facebook SDK for example. Tell Facebook to import into eclipse. Add the imported Facebook as a Lib project to our project

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/54/7C/wKioL1SEZKqjD5oOAAEGa95falk573.jpg "title=" 5.png " alt= "Wkiol1sezkqjd5ooaaega95falk573.jpg"/>


650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/54/7E/wKiom1SEZCbCvfXRAABYNiD3ujQ366.jpg "title=" 6.png " alt= "Wkiom1sezcbcvfxraabynid3ujq366.jpg"/>


Just turn on the VPN and install Facebook on Google Play.

5. Configure Facebook AppID and network access

Add in Strings.xml

<string name = "facebook_app_id" >365646333616560</string>

Add in Androidmanifest.xml

<meta-data android:name= "Com.facebook.sdk.ApplicationId" android:value= "@string/facebook_app_id"/> < Uses-permission android:name= "Android.permission.INTERNET"/>

6. Listing Mainactivity's Code

package com.test.share;import android.app.activity;import android.content.intent;import  android.os.bundle;import android.util.log;import android.view.menu;import android.view.view; import android.widget.toast;import com.facebook.uilifecyclehelper;import  com.facebook.widget.facebookdialog;public class mainactivity extends activity { private uilifecyclehelper uihelper; @Overrideprotected  void oncreate (bundle  savedinstancestate)  {super.oncreate (savedinstancestate); Uihelper = new uilifecyclehelper ( This, null);     uihelper.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Final activity  activity = this;findviewbyid (R.id.button1). Setonclicklistener (New view.onclicklistener ()  {@Overridepublic  void onclick (View arg0 )  {//  determine if facebookif  is installed (facebookdialog.canpresentsharediaLog (Activity,facebookdialog.sharedialogfeature.share_dialog))  {facebookdialog sharedialog =  new facebookdialog.sharedialogbuilder (activity). Setlink ("www.baidu.com"). Setapplicationname (" TestShare "). SetName (" Test "). Setcaption (" this is  a test share  "). Build (); Uihelper.trackpendingdialogcall (Sharedialog.present ());}  else {toast.maketext (activity,  "facebook app is not installed", Toast.length_short). Show ();}});} @Overrideprotected  void onactivityresult (int requestcode, int resultcode, intent  data)  {    super.onactivityresult (requestcode, resultcode, data);     uihelper.onactivityresult (requestcode, resultcode, data, new  Facebookdialog.callback ()  {         @Override          public void onerror (FacebookdiaLog. Pendingcall pendingcall, exception error, bundle data)  {     &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.E ("Activity",  string.format ("error: %s",  Error.tostring ()));        }          @Override         public void oncomplete ( Facebookdialog.pendingcall pendingcall, bundle data)  {       &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;LOG.I ("Activity",  "success!");         }    });} @Overrideprotected  void onresume ()  {    super.onresume ();     uihelper.onresume ();} @Overrideprotected  void onsaveinstancestate (bundle outstate)  {     Super.onsaveinstancestate (outstate);     uihelper.onSaveinstancestate (outstate);} @Overridepublic  void onpause ()  {    super.onpause ();     uihelper.onpause ();} @Overridepublic  void ondestroy ()  {    super.ondestroy ();     uihelper.ondestroy ();} @Overridepublic  boolean oncreateoptionsmenu (Menu menu)  {// inflate the menu ; this adds items to the action bar if it is  Present.getmenuinflater (). Inflate (r.menu.main, menu); return true;}}

7. Run the test

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/54/7C/wKioL1SEZhXQGIvbAAF8lNOD1vI439.jpg "title=" 7.png " alt= "Wkiol1sezhxqgivbaaf8lnod1vi439.jpg"/>

Click Post to "Share success"


8. Issues of attention

(1). appname issues when sharing

Setapplicationname ("must be the appname you set in the background") Facebookdialog Sharedialog = new Facebookdialog.sharedialogbuilder ( Activity). Setlink ("www.baidu.com"). Setapplicationname ("TestShare"). SetName ("Test"). Setcaption ("This is a test Share "). Build ();

(2). When the signature hash value is published in the background, the hash value is changed to a formal signature.

(3). About confusion

-keep class com.facebook.** {*;} -keepattributes Signature

(4). When the official release of the background to change a state

"Status&review", "Do you want to meak this app and all its live...public?", "Yes" 650) this.width=650; " Src= "Http://s3.51cto.com/wyfs02/M00/54/7E/wKiom1SEZ0jCrFuOAAG69LMwjxc623.jpg" title= "9.png" alt= " Wkiom1sez0jcrfuoaag69lmwjxc623.jpg "/>


Finally attach the demo source code

Demo


Well, let's share it with you today.


This article is from the "birds learn C to Dapeng" blog, please be sure to keep this source http://vincenttung.blog.51cto.com/6249439/1587351

Android Access Facebook Share feature

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.