6

我正在尝试为用户提供通过复选框(Android 版 Facebook SDK)设置/撤销发布权限的选项。下面提供了代码。一切正常,除了撤销负责检查发布权限的代码后惨遭失败。

我知道 Session 无法知道用户在登录后是否撤销了任何权限。处理这种情况的正确方法是什么?我是否必须手动查询可用权限,或者有没有办法无缝地重新创建具有基本权限的会话?

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    ...
    publishCheckbox = (CheckBox) view.findViewById(R.id.publishCheckbox);
        publishCheckbox.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                CheckBox chb = (CheckBox) v;
                if (chb.isChecked() && checkForPublishPermission() == false){
                    requestPublishPermissions();                            
                }
                else{
                    removePublishPermissions();
                }   

            }

        });
    ...
}

private void requestPublishPermissions() {
    Session session = Session.getActiveSession();
    if ( session!= null && session.isOpened()){
        Session.NewPermissionsRequest newPermissionsRequest = new Session.NewPermissionsRequest(this, PERMISSIONS).setRequestCode(REAUTH_ACTIVITY_CODE);
        newPermissionsRequest.setCallback(callback);
        session.requestNewPublishPermissions(newPermissionsRequest);
    }               
}   

private void removePublishPermissions() {       
    Request r = new Request(Session.getActiveSession(), PUBLISH_ACTIONS_PERMISSION_PATH, null, HttpMethod.DELETE);
    r.setCallback(new Request.Callback() {

        @Override
        public void onCompleted(Response response) {
            publishCheckbox.setEnabled(true);                               
        }
    });
    r.executeAsync();
    publishCheckbox.setEnabled(false);
}

private boolean checkForPublishPermission(){
    boolean result = false;

    Session session = Session.getActiveSession();

    if (session == null || !session.isOpened()) {
        result = false;
    }
    else{
        List<String> permissions = session.getPermissions();
        if (permissions.containsAll(PERMISSIONS)) {
            result = true;
        }
    }       

    return(result);
}   

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (state.isOpened()) { 
        ...
        publishCheckbox.setChecked(checkForPublishPermission());
        ...
    } else if (state.isClosed()) {          
        ...                             
    }       
}
4

1 回答 1

1

好吧,每次我需要这样的检查时,我最终都会请求用户权限,类似于以下内容:

private void checkForPublishPermission(){
    mPublishCheckbox.setEnabled(false);
    Request r = new Request(Session.getActiveSession(), PUBLISH_ACTIONS_PERMISSION_PATH, null, HttpMethod.GET);
    r.setCallback(new Request.Callback() {

        @Override
        public void onCompleted(Response response) {
            if (response != null) {
                GraphObject o = response.getGraphObject();              
                    if ( o != null){
                        JSONArray data = o.getInnerJSONObject().optJSONArray("data");                       
                        mCanPublish = (data.optJSONObject(0).has("publish_actions"))?true:false;                            
                        mPublishCheckbox.setChecked(mCanPublish);                                                       
                    }
            }
            mPublishCheckbox.setEnabled(true);
        }
    });
    r.executeAsync();
}
于 2014-06-11T22:42:19.773 回答