花了好几天试图解决这个问题后,我无法想出任何有效的方法。使用我的代码,它向我显示了我接受并登录的权限表单,但它没有到达onSuccess,onError或onCancel。无论何时我点击按钮,它都没有做任何事情。并且LogCat上没有错误。我不知道我哪里出错了。
fb = (Button) findViewById(R.id.fb_button);
fb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this,permissionNeeds);
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("kkkkkk","kkllkl");
}
@Override
public void onCancel() {
Log.d("kkkkkk","kkllkl2");
}
@Override
public void onError(FacebookException error) {
Log.d("kkkkkk","kkllkl4");
}
});
}
});
在FacebookSdk.sdkInitialize(this.getApplicationContext());
super.onCreate(savedInstanceState);
答案 0 :(得分:3)
我终于明白了。我将代码从初始代码改为:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
Log.d("kkkkkk","kkllkl");
}
@Override
public void onCancel() {
Log.d("kkkkkk","kkllkl2");
}
@Override
public void onError(FacebookException error) {
Log.d("kkkkkk","kkllkl4");
}
});
然后在点击监听器的按钮上,我只需要拨打登录电话。
fb.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
LoginManager.getInstance().logInWithReadPermissions(LoginActivity.this,permissionNeeds);
}
});
也不要忘记添加onActivityResult。我也犯了错误。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
答案 1 :(得分:1)
完整的代码如下。
public class MainActivity extends ActionBarActivity {
CallbackManager callbackManager;
Button but, but2;
AccessTokenTracker accessTokenTracker;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
but = (Button) findViewById(R.id.button);
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
LoginManager.getInstance().registerCallback(callbackManager,
new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
// App code
}
@Override
public void onCancel() {
// App code
// savedInstanceState
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends" ));
// }
accessTokenTracker = new AccessTokenTracker() {
@Override
protected void onCurrentAccessTokenChanged(
AccessToken oldAccessToken,
AccessToken currentAccessToken) {
}
};
but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getUserData(AccessToken.getCurrentAccessToken());
Log.d("Access Token: ", AccessToken.getCurrentAccessToken().toString());
}
});
}
public void getUserData(AccessToken accessToken){
GraphRequest request = GraphRequest.newMeRequest(
accessToken,
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(
JSONObject object,
GraphResponse response) {
//JSONParser parser=new JSONParser();
// Data data = new Gson().fromJson(json, Data.class);
long ID = 0;
try {
String name = response.getRawResponse();
Toast.makeText(MainActivity.this, "response is: "+object.toString(), Toast.LENGTH_LONG).show();
}
catch (Exception e){
Toast.makeText(MainActivity.this, "error is: "+e.toString(), Toast.LENGTH_LONG).show();
}
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,link");
request.setParameters(parameters);
request.executeAsync();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onDestroy() {
super.onDestroy();
accessTokenTracker.stopTracking();
}
}