gpt4 book ai didi

android - Facebook LoginButton 回调未被调用

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:57:17 25 4
gpt4 key购买 nike

我正在尝试在我的 android 应用程序上实现一个简单的 facebook 登录。当我单击登录按钮时,应用程序被重定向到 facebook 页面,输入凭据后,它再次返回到我的应用程序。但是不会调用 LoginButton 回调函数。虽然有一些类似的问题,但他们有一个单独的 fragment 类。但是,我正在从主要 Activity 开始做所有事情。以下是我的代码:

package com.example.ankur.facebookdemo;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

public class MainActivity extends AppCompatActivity {
private LoginButton loginButton;
CallbackManager callbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
setContentView(R.layout.activity_main);

loginButton = (LoginButton) findViewById(R.id.login_button);
if (loginButton == null) {
Log.v("CheckLogin", "null");
}
else {
Log.v("CheckLogin", "not null");
}


loginButton.setReadPermissions("user_friends");

loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
Toast.makeText(getApplicationContext(),"Fb Login Success", Toast.LENGTH_LONG);
Log.v("CheckLogin", "successfully connected to facebook");
}

@Override
public void onCancel() {
// App code
Toast.makeText(getApplicationContext(),"Fb on cancel",Toast.LENGTH_LONG);
Log.v("CheckLogin", " connection to facebook cancelled");

}

@Override
public void onError(FacebookException exception) {
// App code
Toast.makeText(getApplicationContext(),"Fb Login Error",Toast.LENGTH_LONG);
Log.v("CheckLogin", "Error on connection to facebook");
}
});

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

最佳答案

您忘记覆盖 onActivityResult() 方法。 onActivityResult() 导致回调方法被调用。

做这样的事情:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}

希望对您有所帮助!一切顺利:)

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

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