您不需要在 android studio 中导入 facebook SDK 文件,只需转到您的应用程序模块 gradle:build.gradle 并粘贴
repositories {
mavenCentral()
}
dependencies {
compile 'com.android.support:support-v4:21.0.3'
compile 'com.facebook.android:facebook-android-sdk:4.8.0'
}
.不要忘记同步。此时 min sdk 版本必须是我 15 并转到https://developers.facebook.com/然后创建您的应用程序 ID 并将其粘贴到 values >> trirings.xml 中,也不要忘记创建您的应用程序 keyHASH 。
在您的项目中创建以下文件:MyApplication.java
粘贴这些代码:
public class MyApplication extends Application {
// Updated your class body:
@Override
public void onCreate() {
super.onCreate();
// Initialize the SDK before executing any other operations,
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
}
}
让你的 SigninFragment 像这样:
public class SigninFragment extends Fragment {
CallbackManager callbackManager;
private LoginButton loginButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Other app specific specialization
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(getActivity());
callbackManager = CallbackManager.Factory.create();
// Initialize the SDK before executing any other operations,
View rootView = inflater.inflate(R.layout.signin_fragment, container, false);
loginButton = (LoginButton)rootView.findViewById(R.id.login_button);
loginButton.setReadPermissions("email");
// If using in a fragment
loginButton.setFragment(this);
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Toast.makeText(getApplicationContext(), "Login attempt succes.",Toast.LENGTH_LONG).show();
}
@Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "Login attempt cancelled.",Toast.LENGTH_LONG).show();
}
@Override
public void onError(FacebookException e) {
Toast.makeText(getApplicationContext(), "Login attempt failed.",Toast.LENGTH_LONG).show();
}
});
return rootView;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
您的 android 清单必须如下所示:
//facebook
<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"
android:exported="true"
>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="@string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
<meta-data android:name="com.facebook.sdk.ApplicationId"
android:value="@string/facebook_app_id"/>
//end facebook
必须在 strings.xml 中找到什么:
<string name="facebook_app_id">here there is id number you get from facebook developpers</string>
<string name="fb_login_protocol_scheme">fbAPP_ID</string>
我您的 signinFragment.xml 添加 facebook 按钮,如下所示:
<com.facebook.login.widget.LoginButton
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="71dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />