1. First register an account on the Facebook developer Portal
https://developers.facebook.com/
Developer Backstage Https://developers.facebook.com/apps
2. Create an account and get the APP ID
Figure A
Figure II
Might
Figure Four
Figure Five
3, get the app signed key hashes value (two ways)3.1 Method 1:
1 package com.pegasus.map.presentation.utils; 2 Import Android.content.Context; 3 Import Android.content.pm.PackageInfo; 4 Import Android.content.pm.PackageManager; 5 Import Android.content.pm.Signature; 6 Import android.util.Base64; 7 Import Android.widget.Toast; 8 Import Java.security.MessageDigest; 9 Import java.security.nosuchalgorithmexception;10/**12 * Created by ${zyj} in 2016/3/29.13 */14 public class Keyhas h {15 16/**17 * Note that when running, the app requires a formal signature of * @param context19 * @param packagename App package name */21 p ublic static void Getkeyhash (context context, String PackageName) {try {PackageInfo info = nul l;24 info = Context.getpackagemanager (). Getpackageinfo (PackageName, packagemanager.get_signatures); 25 for (Signature signature:info.signatures) {messagedigest messagedigest = null;27 MessageDigest = Messagedigest.getinstance ("SHA"); messagedigest. Update (Signature.tobytearray ()); String hs = Base64.encodetostring (Messagedigest.digest (), Base64.defau LT); Toast.maketext (Context, "" + HS, Toast.length_short). Show ();}32} catch (P Ackagemanager.namenotfoundexception e) {e.printstacktrace (); (NoSuchAlgorithmException e) {e.printstacktrace (); 36}37}38}
3.2 command line method to get the Key hash value3.2.1: The preferred guarantee is that you have openSSl library files on your computer
Download http://gnuwin32.sourceforge.net/packages/openssl.htm
3.2.2: Unzip the downloaded file
3.2.3:cmd command line mode to the bin directory of OpenSSL
3.2.4: Execute command
Keytool-exportcert-alias Yiba -keystore C:\work\Key.jks | OpenSSL sha1-binary | OpenSSL base64
Note: 1, the Yiba here represents the alias of the app signature , which is alias
2, C:\WORK\KEY.JKS representative signature path, you need to change the path of your signature
3.2.5 If you do not download OpenSSL on your computer then you execute the following code will appear
4, set the basic configuration of the application
Figure A
Figure II
Figure III
5, the app began to integrate FACEBOOKSDK 5.1 in the project Build.gradle add
Compile ' com.facebook.android:facebook-android-sdk:4.10.1 '
View the latest version: http://search.maven.org/#search%7cga%7c1%7ccom.facebook.android
5.2 Initializing the SDK inside the app's application
Facebook Login Initialization
Facebooksdk.sdkinitialize (Getapplicationcontext ());
5.3 In the Value folder under Strings.xml
<!--Facebook Login key-->
<string name= "facebook_app_id" >1001702146573854</string>
5.4 In the Androidmanifest.xml
<meta-data android:name= "com.facebook.sdk.ApplicationId" android:value= "@string/facebook_app_id"/ >
<activity
Android:name= "Com.facebook.FacebookActivity"
Android:configchanges= "Keyboard|keyboardhidden|screenlayout|screensize|orientation"
Android:theme= "@android: Style/theme.translucent.notitlebar"
Android:label= "@string/app_name"
>
</activity>
5.5 Defining the Login button in the layout file
1234567 |
<com.facebook.login.widget.LoginButton
android:id=
"@+id/facebook_bt"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:text=
"facebook login"
>
</com.facebook.login.widget.LoginButton>
|
5.6 implementation in code
1 package com.pegasus.map.presentation.ui.activity; 2 Import android.content.Intent; 3 Import Android.os.Bundle; 4 Import Android.widget.Toast; 5 Import Com.facebook.CallbackManager; 6 Import Com.facebook.FacebookCallback; 7 Import com.facebook.FacebookException; 8 Import Com.facebook.login.LoginResult; 9 Import com.facebook.login.widget.loginbutton;10 Import com.pegasus.map.r;11 Import COM.PEGASUS.MAP.PRESENTATION.UI.BASE.BASEACTIVITY;12 import Butterknife. BIND;13 import Butterknife. Butterknife;14/**17 * Created by ${zyj} on 2016/3/24.18 * login */20 public class Loginloginactivity extends B aseactivity {@Bind (R.ID.FACEBOOK_BT) public Loginbutton facebook_bt;25-Private Callbackmana Ger Callbackmanager; @Override29 protected void OnCreate (Bundle savedinstancestate) {Super.on Create (savedinstancestate); Setcontentview (R.layout.activity_login); Butterknife.bind (this); 34 Callbackmanager =CallbackManager.Factory.create (),}38 init (), Panax Notoginseng-private void init () {40 41 Facebook_bt.registercallback (Callbackmanager, New facebookcallback<loginresult> () {@Override43 public void onsuccess (Loginresult loginresult) {toast.maketext (Loginloginactivity.this, "Facebo OK login succeeded ", Toast.length_short). Show (); log (" Facebook login succeeded "+ Loginresult.getaccesstoken ());
Get login Information
Getlogininfo (Loginresult.getaccesstoken ());}47 @Override49 public void Oncanc El () {Toast.maketext (loginloginactivity.this, "Facebook login Canceled", Toast.length_short). Show (); 51 Log ("Facebook login Canceled"),}53 @Override55 public void OnError (facebookexception Error) {Toast.maketext (loginloginactivity.this, "Facebook login Failed", Toast.length_short). Show (); 57 Log ("Facebook login Failed");}59});}62 @Override64 protected Voi d onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult, Requestcode e, data);//facebook callback Callbackmanager.onactivityresult (Requestcode, ResultCode, data); 69} 70}
1/** 2 * Get login Information 3 * @param accesstoken 4 */5 public void Getlogininfo (Accesstoken accesstoken) {6 Graphrequest request = Graphrequest.newmerequest (Accesstoken, New Graphrequest.graphjsonobjectcallback () {7 @Override 8 public void oncompleted (Jsonobject object, graphresponse response) {9 if ( Object! = null) {Ten String id = object.optstring ("id"); For example: 156545522156511 String name = object.optstring ("name"); For example: Zhang San12 String gender = object.optstring ("gender"); Gender: such as male (male) female (female) String Emali = object.optstring ("email"); E-mail: For example: [email protected]14 15//Get user avatar Jsonobject object_pic = OBJECT.OPTJ Sonobject ("picture"); Jsonobject object_data = Object_pic.optjsonobject ("data"); 18 String photo = Object_data.optstring ( "url"); 19 20//Get geographical information by String locale = object.optstring ("locale"); ZH_CN represents Chinese Simplified toast.maketext (activity, "" + object.tostring (), Toast.length_short). Show (); 24 }25}26}); bundle parameters = new bundle (); parameters.putst Ring ("Fields", "Id,name,link,gender,birthday,email,picture,locale,updated_time,timezone,age_range,first_name, Last_Name "); request.setparameters (parameters); Request.executeasync (); 32}
5.7 Business account needs to be aware of matters, the release button to open
6, to the FACEBOOKSDK two times package
1 package com.pegasus.map.presentation.utils; 2 3 Import android.app.Activity; 4 Import Android.content.DialogInterface; 5 Import Android.os.Bundle; 6 Import Android.support.v7.app.AlertDialog; 7 Import Android.widget.Toast; 8 9 Import Com.facebook.AccessToken; Ten import Com.facebook.CallbackManager; Import Com.facebook.FacebookCallback; Import com.facebook.FacebookException; Import Com.facebook.GraphRequest; Import Com.facebook.GraphResponse; Com.facebook.Profile import; Com.facebook.login.LoginManager import; + Import Com.facebook.login.LoginResult; Import Org.json.JSONObject; Import Java.util.Arrays; Import java.util.Collections; Java.util.List import; /** * Created by ${zyj} on 2016/3/30. * * * public class Facebooklogin {$ private activity activity; Callbackmanager Callbackmanag ER; + Private Facebooklistener Facebooklistener; Private list<string> permissions = CollectionS.<string>emptylist (); Private Loginmanager Loginmanager; Facebooklogin public (activity activity) {PNS this.activity = activity; 38 39//Initialize Facebook Login Service Callbackmanager = CallbackManager.Factory.create (); Getloginmanager (). RegisterCallback (Callbackmanager, New facebookcallback<loginresult> () {42 @Override public void onsuccess (Loginresult loginresult) {//Login Success 45 Accesstoken Accesstoken = Loginresult.getaccesstoken (); Getlogininfo (Accesstoken); () @Override public void OnCancel () {51//Cancel Login 52 } @Override-public void OnError (facebookexception error) {56//Login out Error 57} 58}); Permissions = Arrays. Aslist ("email", "user_likes", "User_status", "user_phOTOs "," User_birthday "," Public_profile "," user_friends "); 62} 63 64/** 65 * Login * * * the public void login () {Getloginmanager (). loginwithreadperm Issions (activity, permissions); 70} 71 72/** 73 * Exit * * Logout () {$ logout = Activity.getresour CES (). getString (com.facebook.r.string.com_facebook_loginview_log_out_action); The String Cancel = activity.getresources (). getString (COM.FACEBOOK.R.STRING.COM_FACEBOOK_LOGINV Iew_cancel_action); a String message; Bayi Profile profile = Profile.getcurrentprofile (); Profile.getname if (profile! = NULL && *. =) = null) {String.Format message = A. 84 Activity.getresources (). getString (Com.facebook.r.string.com_facebook_loginview _logged_in_as), Profile.getname ()); The Els}e {message = Activity.getresources (). getString (Com.facebook.r.string.com_facebook_ Loginview_logged_in_using_facebook); Alertdialog.builder Builder = new Alertdialog.builder (activity); Builder.setmessage (Message) setcancelable (TRUE) 94. Setpositivebutton (Logou T, new Dialoginterface.onclicklistener () {Up public void OnClick (Dialoginterface dialog, int which) { Getloginmanager (). LogOut (); 98}) Setnegativebutton (cancel, null); Builder.cre Ate (). Show (); 101}102 103/**104 * Get login information * @param accessToken106 */107 public void GetLogin Info (Accesstoken accesstoken) {108 Graphrequest request = Graphrequest.newmerequest (Accesstoken, New graphrequest . Graphjsonobjectcallback () {109 @Override110 public void oncomPleted (Jsonobject object, graphresponse response) {111 if (Object! = null) {String id = object.optstring ("id"); For example: 1565455221565113 String name = object.optstring ("name"); For example: Zhang San114 String gender = object.optstring ("gender"); Gender: such as male (male) female (female), String Emali = object.optstring ("email"); Email: for example: [email protected]116 117//Get user avatar 118 Jsonobject object_pic = OBJECT.O Ptjsonobject ("picture"); 119 Jsonobject Object_data = Object_pic.optjsonobject ("data"); 120 String photo = object_data.optstring ("url"); 121 122//Get GEO Information 123 string LOC Ale = object.optstring ("locale"); ZH_CN represents Chinese Simplified 124 toast.maketext (activity, "" + object.tostring (), Toast.length_short). Show (); 126 }127}128}); 129 130 Bundle parameters = new Bundle (); 131 parameters.putstring ("Fields", "id,name,link,gender,birthday,email,pictu Re,locale,updated_time,timezone,age_range,first_name,last_name "); request.setparameters (parameters); 133 Request.executeasync (); 134}135 136/**137 * Get loginMananger138 * @return139 */140 private Lo Ginmanager Getloginmanager () {141 if (Loginmanager = = null) {142 Loginmanager = Loginmanager.getinstan CE (); 143}144 return loginmanager;145}146 147 public Callbackmanager Getcallbackmanager () {148 return Callbackmanager; 149}150 151/**152 * Set Login Simplified 153 * @param facebookListener154 */155 public void Setfacebooklistener (Facebooklistener facebooklistener) {156 This.facebooklistener =facebookListener ; 157}158 159 public interface Facebooklistener {void facebookloginsuccess (); 161 void Faceboo Kloginfail (); 162}163 164 165}
In activity use
1 //Initialize Facebook Login service 2 facebooklogin facebooklogin = new Facebooklogin (this); 3 4 //Login 5 Facebooklogin.login (); 6 7 //Exit 8 facebooklogin.logout ();
1 @Override2 protected void onactivityresult (int requestcode, int resultcode, Intent data) {3 Super.onactivityresult (Requestcode, ResultCode, data); 4 5 //facebook callback 6 Facebooklogin.getcallbackmanager (). Onactivityresult (Requestcode, ResultCode, data); 7 8 }
Android App integrates Facebook login and two-time package