gpt4 book ai didi

android - 未调用 NewPermissionsRequest 回调

转载 作者:太空狗 更新时间:2023-10-29 15:03:44 28 4
gpt4 key购买 nike

这是我的代码:

public class MainFragment extends Fragment implements OnClickListener {

private static final String EMAIL_PERMISSION = "email";
private static final String TAG = MainFragment.class.getSimpleName();

private UiLifecycleHelper uiHelper;
private LoginButton loginButton;
private GraphUser loggedInUser;
private TextView tvUserName, tvEmail;
private Button btnRequestEmail;
private Session mSession;

private Session.StatusCallback callback = new Session.StatusCallback() {

@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main, container, false);
tvUserName = (TextView) view.findViewById(R.id.tv_username);
tvEmail = (TextView) view.findViewById(R.id.tv_email);
btnRequestEmail = (Button) view.findViewById(R.id.btn_requestEmail);
btnRequestEmail.setOnClickListener(this);

loginButton = (LoginButton) view.findViewById(R.id.btn_login);
loginButton.setReadPermissions(Arrays.asList("public_profile"));
loginButton.setFragment(this);
loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
@Override
public void onUserInfoFetched(GraphUser user) {
loggedInUser = user;
updateUI();
}
});
return view;
}

@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);
}

@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);
}

private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (session != null && session.isOpened()) {
mSession = session;
btnRequestEmail.setEnabled(true);
makeMeRequest(session);
}
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_requestEmail:
requestEmailPermissions();
break;

default:
break;
}
}

private void requestEmailPermissions() {
Session.NewPermissionsRequest newPermissionsRequest =
new Session.NewPermissionsRequest(getActivity(), Arrays.asList(EMAIL_PERMISSION));

newPermissionsRequest.setCallback(new StatusCallback() {

@Override
public void call(Session session, SessionState state, Exception exception) {
if(session.isPermissionGranted(EMAIL_PERMISSION)){
Toast.makeText(getActivity(), "Email permission granted :)", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getActivity(), "Email permission denied :(", Toast.LENGTH_LONG).show();
}
}
});

Session.getActiveSession().requestNewReadPermissions(newPermissionsRequest);
}

private void makeMeRequest(final Session session) {
Request request = Request.newMeRequest(session,
new GraphUserCallback() {

@Override
public void onCompleted(GraphUser user, Response response) {
if (session == Session.getActiveSession()) {
loggedInUser = user;
updateUI();
}
if (response.getError() != null) {
Log.e(TAG, response.getError().getErrorMessage());
}
}
});
request.executeAsync();
}

protected void updateUI() {
if (loggedInUser != null) {
tvUserName.setText(getString(R.string.welcome_user,
loggedInUser.getFirstName()));

if (loggedInUser.asMap().get(EMAIL_PERMISSION) != null) {
String email = loggedInUser.asMap().get(EMAIL_PERMISSION).toString();
tvEmail.setText(email);
}
} else {
tvUserName.setText("");
}
}
}

起初我使用默认的 facebook 登录按钮登录。
当 session 打开时,我启用“请求电子邮件权限”按钮。
当用户单击该按钮时,我请求电子邮件权限。

这有效,我看到了权限请求屏幕,但如果用户不接受权限,我想捕获。

所以我添加了这个回调来检查权限是否被授予:

newPermissionsRequest.setCallback(new StatusCallback() {

@Override
public void call(Session session, SessionState state, Exception exception) {
if(session.isPermissionGranted(EMAIL_PERMISSION)){
Toast.makeText(getActivity(), "Email permission granted :)", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(getActivity(), "Email permission denied :(", Toast.LENGTH_LONG).show();
}
}
});

但是这个回调永远不会被调用。我该如何解决这个问题?
PS:我使用的是 Facebook SDK 3.14.1 版

最佳答案

必须调用正确 Activity 的 onActivityResult 方法才能运行 newPermissionRequest 回调。

如果 newPermissionRequest 在 fragment 中发生,请确保通过覆盖 Activity 中的 onActivityRequest 然后在 fragment 中显式调用 onActivityRequest 来将 Activity 的 onActivityRequest 调用传递给它,如下所示:

MyActivity.java:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
getSupportFragmentManager().findFragmentById(R.id.my_fragment_frame).onActivityResult(requestCode, resultCode, data);
}

onActivityResult 如果混合使用静态 (xml) 和动态(编程) fragment ,则可能无法调用。有关更多信息,请在此处查看已接受的答案:FB sdk, requestNewPublishPermissions() doesn't call any callback

在我的例子中,我的 newPermissionRequest 附加到一个与我想象的不同的 Activity ,所以我看到 onActivityResult 被调用,但我没有意识到这是错误的 Activity 。

关于android - 未调用 NewPermissionsRequest 回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24037866/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com