我正在开发一个 facebook 集成的应用程序,我遇到了一个非常奇怪的问题。
我正常登录,但是当我即将注销时,SDK 会强制再次登录...
package com.machinarius.facebook;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.facebook.Request;
import com.facebook.Request.GraphUserCallback;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.Session.NewPermissionsRequest;
import com.facebook.Session.StatusCallback;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
public class MainActivity extends Activity implements StatusCallback, GraphUserCallback {
private TextView statusText;
private Button loginButton;
private Boolean loggedIn;
private Session actualSession;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loggedIn = false;
statusText = (TextView)findViewById(R.id.statusText);
loginButton = (Button)findViewById(R.id.loginButton);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
public void toggleFacebookLogin(View source) {
if(!loggedIn) {
actualSession = Session.openActiveSession(this, true, this);
}
else
if(actualSession == null)
return;
else {
actualSession.close();
statusText.setText("Please login");
loggedIn = false;
}
}
@Override
public void call(Session session, SessionState state, Exception exception) {
if(state.equals(SessionState.OPENED)) {
loggedIn = true;
List<String> permissions = getNewPermissions(actualSession.getPermissions(),
new LinkedList<String>(Arrays.asList("user_about_me", "user_birthday", "user_hometown", "user_relationships", "user_location", "user_relationships", "email")));
Session.NewPermissionsRequest permissionsRequest = new NewPermissionsRequest(this, permissions);
actualSession.requestNewReadPermissions(permissionsRequest);
populateInterface();
} else {
loggedIn = false;
}
}
private void populateInterface() {
Request.executeMeRequestAsync(Session.getActiveSession(), this);
}
@Override
public void onCompleted(GraphUser user, Response response) {
if(response.getError() != null) {
statusText.setText("There was an error fetching your info");
Log.e(getClass().getName(), response.getError().toString());
return;
}
if(user != null) {
statusText.setText("Hello " + user.getName() + "\n"
+ "Your user ID is " + user.getId() + "\n"
+ "Your birthday is " + user.getBirthday());
loginButton.setText("Log out");
} else {
statusText.setText("There was an error fetching your info");
}
}
private List<String> getNewPermissions(List<String> actualPermissions, List<String> desiredPermissions) {
ArrayList<String> permissionsGranted = new ArrayList<String>();
for(String nPermission : desiredPermissions)
for(String aPermission : actualPermissions)
if(nPermission.equals(aPermission))
permissionsGranted.add(nPermission);
for(String gPermission : permissionsGranted)
desiredPermissions.remove(gPermission);
return desiredPermissions;
}
}
toggleFacebookLogin 由 loginButton 上的 XML onClick 处理程序调用。
还有一个问题,我如何将权限请求集成到 session.open 调用中,这样我就不必让用户看到两次 Facebook 对话框?