试试这个完整的工作解决方案
1) 创建活动
import java.util.ArrayList;
import java.util.List;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.facebook.Session;
import com.facebook.Session.OpenRequest;
import com.facebook.SessionLoginBehavior;
import com.facebook.SessionState;
import com.facebook.UiLifecycleHelper;
public class FacebookLoginActivity extends FragmentActivity {
private LinearLayout lnrPbr;
private TextView txtPbr;
private FacebookLoginMainFragment mainFragment;
private UiLifecycleHelper uiHelper;
/**
* Overrides method
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
mainFragment = new FacebookLoginMainFragment();
getSupportFragmentManager().beginTransaction().add(android.R.id.content, mainFragment).commit();
} else {
// Or set the fragment from restored state info
mainFragment = (FacebookLoginMainFragment) getSupportFragmentManager().findFragmentById(android.R.id.content);
}
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
lnrPbr = (LinearLayout) findViewById(R.id.lnrPbr);
txtPbr = (TextView) findViewById(R.id.txtPbr);
txtPbr.setText("");
Session session = Session.getActiveSession();
if (session != null && (session.isOpened() || session.isClosed())) {
onSessionStateChange(session, session.getState(), null);
} else {
if (session == null) {
session = new Session(this);
Session.setActiveSession(session);
}
OpenRequest openRequest = new OpenRequest(this);
List<String> readPermissions = new ArrayList<String>();
readPermissions.add("email");
openRequest.setPermissions(readPermissions);
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
openRequest.setCallback(callback);
session.openForRead(openRequest);
}
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
finish();
} else {
uiHelper.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
try {
Session.getActiveSession().closeAndClearTokenInformation();
Session.setActiveSession(null);
} catch (Throwable e) {
e.printStackTrace();
}
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
/**
* Class method
*/
/**
* This method used to after session state change listner.
*
* @param session
* represent {@link Session}
* @param state
* represent {@link SessionState}
* @param exception
* represent exception
*/
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
// Your session is opend here
session.getAccessToken();
} else if (state.isClosed()) {
OpenRequest openRequest = new OpenRequest(this);
List<String> readPermissions = new ArrayList<String>();
readPermissions.add("email");
openRequest.setPermissions(readPermissions);
openRequest.setLoginBehavior(SessionLoginBehavior.SSO_WITH_FALLBACK);
openRequest.setCallback(callback);
session.openForRead(openRequest);
}
}
/**
* This method used to get session status callback change listner.
*/
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
}
2)创建片段
import java.util.Arrays;
import java.util.List;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.facebook.widget.LoginButton;
public class FacebookLoginMainFragment extends Fragment {
private static final List<String> PERMISSIONS = Arrays.asList("email");
/**
* Override method
*/
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.facebook_main, container, false);
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setFragment(this);
authButton.setReadPermissions(PERMISSIONS);
authButton.setApplicationId("123124234234-YourAppID");
return view;
}
}
3) Fragment 需要的布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<com.facebook.widget.LoginButton
android:id="@+id/authButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:visibility="gone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp"
android:text="Facebook"
android:textSize="45sp" />
<LinearLayout
android:id="@+id/lnrPbr"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_marginBottom="150dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone" >
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txtPbr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center" />
</LinearLayout>
</LinearLayout>