我com.facebook.widget.UserSettingsFragment
用来管理登录/注销:
Fragment userSettingsFragment = new UserSettingsFragment();
fragmentTransaction.replace(R.id.content_container, userSettingsFragment, "userSettingsFragment");
然后在另一个Fragment
我想在 Facebook 上发布一条消息。我检查了文档并写了MyFragment
如下所示。
与 的连接com.facebook.widget.UserSettingsFragment
似乎有效(我可以登录/注销),但是当我想在 中发布时MyFragment
,似乎session.requestNewPublishPermissions(newPermissionsRequest);
不会改变Session
,因为onSessionStateChange
从未调用过。
它调用一些Facebook
UI 来请求许可,然后什么也没有发生。
我可以只com.facebook.widget.UserSettingsFragment
用于登录/注销,即它是否管理所有 Facebook SDK 对象?我想知道,因为Session.getActiveSession();
inMyFragment
总是返回 a Session
,即使我已注销。
有人可以帮忙吗?
public class MyFragment extends Fragment{
private Button mBtnShare;
private UiLifecycleHelper mUiHelper;
private Session.StatusCallback mCallback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
private static final String PENDING_PUBLISH_KEY = "pendingPublishReauthorization";
private boolean pendingPublishReauthorization = false;
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
if (pendingPublishReauthorization &&
state.equals(SessionState.OPENED_TOKEN_UPDATED)) {
pendingPublishReauthorization = false;
publish();
}
} else if (state.isClosed()) {
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (savedInstanceState != null) {
pendingPublishReauthorization =
savedInstanceState.getBoolean(PENDING_PUBLISH_KEY, false);
}
return inflater.inflate(R.layout.my_fragment, container, false);
}
@Override
public void onActivityCreated (Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
mBtnShare = (Button) getActivity().findViewById(R.id.btn_share);
mBtnShare.setOnClickListener( new Button.OnClickListener() {
public void onClick(View v)
{
publish();
}
);
}
@Override
public void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
mUiHelper = new UiLifecycleHelper(getActivity(), mCallback);
mUiHelper.onCreate(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
mUiHelper.onResume();
}
@Override
public void onPause() {
mUiHelper.onPause();
super.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
mUiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(PENDING_PUBLISH_KEY, pendingPublishReauthorization);
mUiHelper.onSaveInstanceState(outState);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
mUiHelper.onActivityResult(requestCode, resultCode, intent);
}
private void publish() {
Session session = Session.getActiveSession();
if (session != null){
// Check for publish permissions
List<String> permissions = session.getPermissions();
if (!isSubsetOf(PERMISSIONS, permissions)) {
pendingPublishReauthorization = true;
Session.NewPermissionsRequest newPermissionsRequest = new Session
.NewPermissionsRequest(this, PERMISSIONS);
session.requestNewPublishPermissions(newPermissionsRequest);
return;
}
Bundle postParams = new Bundle();
postParams.putString("name", "Facebook SDK for Android");
postParams.putString("caption", "Build great social apps and get more installs.");
postParams.putString("description", "The Facebook SDK for Android makes it easier and faster to develop Facebook integrated Android apps.");
postParams.putString("link", "https://developers.facebook.com/android");
postParams.putString("picture", "https://raw.github.com/fbsamples/ios-3.x-howtos/master/Images/iossdk_logo.png");
Request.Callback callback= new Request.Callback() {
public void onCompleted(Response response) {
JSONObject graphResponse = response
.getGraphObject()
.getInnerJSONObject();
String postId = null;
try {
postId = graphResponse.getString("id");
} catch (JSONException e) {
Log.i(this.getClass().getSimpleName(), "JSON error "+ e.getMessage(), MyApplication.LOG_LEVEL);
}
FacebookRequestError error = response.getError();
if (error != null) {
Toast.makeText(getActivity()
.getApplicationContext(),
error.getErrorMessage(),
Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity()
.getApplicationContext(),
postId,
Toast.LENGTH_LONG).show();
}
}
};
Request request = new Request(session, "me/feed", postParams,
HttpMethod.POST, callback);
RequestAsyncTask task = new RequestAsyncTask(request);
task.execute();
}
}
private boolean isSubsetOf(Collection<String> subset, Collection<String> superset) {
for (String string : subset) {
if (!superset.contains(string)) {
return false;
}
}
return true;
}
}