我已经实现了fb集成,我通过设置一些权限来获取用户详细信息但是它给了我错误 java.lang.UnsupportedOperationException:Session:尝试为具有挂起的会话请求新权限请求。 当我第一次和第二次运行时它没有给我这个例外,我使用最新的fb SDK进行集成。我会发给我代码。
public class MainActivity extends Activity {
Button loginbtn;
Session session = Session.getActiveSession();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loginbtn = (Button)findViewById(R.id.loginbutton);
loginbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,FacebookLogin.class);
startActivity(intent);
}
});
}
}
public class FacebookLogin extends Activity{
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, Arrays.asList("email,user_birthday,user_location,user_hometown,user_about_me,user_relationships"));
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.facebooklogin);
Session.openActiveSession(this, true, new Session.StatusCallback() {
// callback when session changes state
@SuppressWarnings("deprecation")
@Override
public void call(final Session session, SessionState state, Exception exception) {
if (session.isOpened()) {
// Log.i("session.isOpened", "session.isOpened");
session.requestNewReadPermissions(newPermissionsRequest);
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
//session.requestNewReadPermissions(newPermissionsRequest);
//System.out.println("Response : "+response.toString());
Log.i("Response", response.toString());
if (user != null) {
Log.i("USERName", ""+user.getName());
Log.i("Birthday", ""+user.getBirthday());
Log.i("LastName", ""+user.getLastName());
Log.i("FirstName", ""+user.getFirstName());
Log.i("getId", ""+user.getId());
Log.i("email", " "+user.getLink()+" email : ) "+user.asMap().get("email"));
Log.i("location", ""+user.asMap().get("location"));
Log.i("gender", ""+user.asMap().get("gender"));
/* Intent i =new Intent(getApplicationContext(), MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);*/
finish();
}
}
});
}
}
});
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.i(" onResume()", "called");
};
@Override
public void onStart() {
Log.i(" onStart", "called");
super.onStart();
}
@Override
public void onStop() {
super.onStop();
}
@SuppressWarnings("deprecation")
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i("onActivityResult", "called");
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
final Session session = Session.getActiveSession();
if (session.isOpened()) {
session.requestNewReadPermissions(newPermissionsRequest);
// make request to the /me API
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
// callback after Graph API response with user object
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
//session.requestNewReadPermissions(newPermissionsRequest);
Log.i("USERName2", ""+user.getName());
Log.i("Birthday2", ""+user.getBirthday());
Log.i("LastName2", ""+user.getLastName());
Log.i("FirstName2", ""+user.getFirstName());
Log.i("email2", " "+user.getLink()+" email : ) "+user.asMap().get("email"));
Log.i("location", ""+user.asMap().get("location"));
Log.i("gender", ""+user.asMap().get("gender"));
/* Intent i =new Intent(getApplicationContext(), MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
finish();*/
//Log.i("USERName", ""+user.getName());
}
}
});
}else {
finish();
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
Log.i("onSaveInstanceState", "called");
super.onSaveInstanceState(outState);
Session session = Session.getActiveSession();
Session.saveSession(session, outState);
}
}
答案 0 :(得分:1)
使用onActivityResult方法更新活动会话。
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}