我试图在我的应用程序上设置一个Facebook按钮登录,但我有一个问题,当我按下按钮时它说“你的登录”,当我启动我的应用程序时说“退出”(因为我'自动登录)但我无法获得有关配置文件的信息,我有这个代码:
package com.utad.clasi.facebook;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.Signature;
import android.media.Image;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.Profile;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
public class MainActivity extends ActionBarActivity {
private View otherView;
private static final String TAG = "MainActivity";
//private String welcome = getResources().getString(R.string.welcome);
private LoginButton loginButton;
private CallbackManager callbackManager;
private AccessToken accessToken;
private Profile profile;
private TextView txtProfileDetails;
private FacebookCallback<LoginResult> myCallBack = new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
accessToken = loginResult.getAccessToken();
profile = Profile.getCurrentProfile();
if(profile != null){
txtProfileDetails.setText("Welcome" + profile.getFirstName());
}
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException e) {
}
};
@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);
setLoginButton();
txtProfileDetails = (TextView) findViewById(R.id.txtWelcomeProfile);
}
@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);
}
public void setLoginButton(){
loginButton.setReadPermissions("public_profile");
loginButton.registerCallback(callbackManager, myCallBack);
}
@Override
protected void onActivityResult (int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
loginButton.registerCallback(callbackManager, myCallBack);
callbackManager.onActivityResult(requestCode,resultCode, data);
}
}
我只想将配置文件中的名称和姓氏保存为2个变量。但我不能这样做。当我的应用程序登录到Facebook以获取它们时,我想知道这一点。谢谢,任何建议或意见将不胜感激。
答案 0 :(得分:0)
好的,我在onCreate中遗忘了两行代码:
Profile actualProfile = Profile.getCurrentProfile();
String name = actualProfile.getName();
使用Profile.getCurrentProfile,您可以在应用中查看哪些配置文件是loggin。