问题
I am doing log in facebook using facebook sdk in my application in which authorize person (through which I have created example on facebook) doing log in successfully but when another person doing log in then he is not able to log in (I think session is created but session state is closed) now I am confused what I do. my code is below
public class MainFragment extends Fragment{
private static final String TAG = "deepak";
private UiLifecycleHelper uiHelper;
Boolean isInternetPresent;
private TextView albumTextView;
private static String APP_ID = "xxxxxxxxxxxxxxxxx";
Facebook facebook;
Session session;
private boolean flag = false;
static ArrayList<String> allNames;
static ArrayList<String> uid;
static ArrayList<String> picUrl;
// String[] pics = new String[]{};
// String[] uids;
// String[] allnames;
JSONArray jArray;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.activity_main, container, false);
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setReadPermissions(Arrays.asList("read_friendlists", "user_photos", "friends_photos"));
albumTextView = (TextView) view.findViewById(R.id.text_viet_albul);
authButton.setFragment(this);
facebook = new Facebook(APP_ID);
allNames = new ArrayList<String>();
uid = new ArrayList<String>();
picUrl = new ArrayList<String>();
ConnectionDetector cd = new ConnectionDetector(getActivity());
isInternetPresent = cd.isConnectingToInternet();
checkInternetConnection();
return view;
}
private void checkInternetConnection() {
// TODO Auto-generated method stub
if(!isInternetPresent) {
// Internet connection is not present
// Ask user to connect to Internet
showAlertDialog(getActivity(), "No Internet Connection", "You don't have internet connection.", false);
}
}
public void showAlertDialog(Context context, String title, String message, Boolean status) {
AlertDialog alertDialog = new AlertDialog.Builder(context).create();
// Setting Dialog Title
alertDialog.setTitle(title);
// Setting Dialog Message
alertDialog.setMessage(message);
// Setting alert dialog icon
//alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
MainActivity.finishIfNetworkIsNotWorking();
}
});
// Showing Alert Message
alertDialog.show();
}
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(TAG, "Logged in...");
} else if (state.isClosed()) {
Log.i(TAG, "Logged out...");
}
}
private Session.StatusCallback callback = new Session.StatusCallback()
{
@Override
public void call(Session session, SessionState state, Exception exception)
{
onSessionStateChange(session, state, exception);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
Session session = Session.getActiveSession();
if (session != null &&
(session.isOpened() || session.isClosed()) ) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(getActivity(), requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
回答1:
Use
Session.getActiveSession().closeAndClearTokenInformation();
after you get the enough details or process is done with the logged in account. So when login again it automatically looks for a new Token from facebook user.
回答2:
Do you do log out when you change user? Sometimes your tokens or session is saving in share preferences, and when you run your app it load tokens from file and create your session. Your method log out must be like next:
private void onClickLogout() {
Session session = Session.getActiveSession();
if (!session.isClosed()) {
session.closeAndClearTokenInformation();
}
}
Do you get active session?
来源:https://stackoverflow.com/questions/20918405/facebook-login-is-not-working-correctly-for-another-person