使用Facebook SDK登录到App
问题描述:
我用Session类所讲述。但仍然,我不认为它的工作。下面是我的代码:
但是,我的edittext没有更新名称。它显示您还没有登录。无论任何在Session.CallBack方法的日志消息都在我的logcat
public class profile extends Activity implements View.OnClickListener {
ImageView profilePropic;
EditText name;
Dialog dialog;
Button gallery;
Bitmap bmp;
FileOutputStream fos;
LoginButton loginButton;
private UiLifecycleHelper uiHelper;
private static final List<String> PERMISSIONS = Arrays.asList("publish_actions");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.profile);
uiHelper = new UiLifecycleHelper(this, statusCallback);
uiHelper.onCreate(savedInstanceState);
name = (EditText) findViewById(R.id.profile_name);
profilePropic = (ImageView) findViewById(R.id.profile_propic);
loginButton = (LoginButton) findViewById(R.id.fb_login_button);
loginButton.setUserInfoChangedCallback(new LoginButton.UserInfoChangedCallback() {
@Override
public void onUserInfoFetched(GraphUser user) {
if (user != null) {
name.setText("Hello, " + user.getName());
} else {
name.setText("You are not logged");
}
}
});
}
private Session.StatusCallback statusCallback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.d("FacebookSampleActivity", "Facebook session opened");
} else if (state.isClosed()) {
Log.d("FacebookSampleActivity", "Facebook session closed");
}
}
};
private void beginCrop(Uri source) {
Uri outputUri = Uri.fromFile(new File(getCacheDir(), "cropped"));
new Crop(source).output(outputUri).asSquare().start(this);
}
private void handleCrop(int resultCode, Intent result) {
if (resultCode == RESULT_OK) {
Uri uri = Crop.getOutput(result);
Picasso.with(this).load(uri).into(profilePropic);
try {
bmp = MediaStore.Images.Media.getBitmap(this.getContentResolver(), uri);
} catch (IOException e) {
e.printStackTrace();
}
try {
fos = openFileOutput("ProPic", Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
bmp.compress(Bitmap.CompressFormat.JPEG, 60, fos);
} else if (resultCode == Crop.RESULT_ERROR) {
Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onResume() {
super.onResume();
uiHelper.onResume();
FileInputStream fis;
try {
fis = openFileInput("ProPic");
Bitmap bitmapA = BitmapFactory.decodeStream(fis);
profilePropic.setImageBitmap(bitmapA);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public void changeAvatar(View view) {
// Intent pickFromGallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
// startActivityForResult(pickFromGallery, OpenGallery);
dialog = new Dialog(this);
dialog.setContentView(R.layout.propic_dialog);
dialog.setTitle("Show us how you look like !");
dialog.show();
gallery = (Button) dialog.findViewById(R.id.gallery);
gallery.setOnClickListener(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == Crop.REQUEST_PICK && resultCode == RESULT_OK) {
beginCrop(data.getData());
} else if (requestCode == Crop.REQUEST_CROP) {
handleCrop(resultCode, data);
}
}
@Override
public void onClick(View v) {
if (v == gallery) {
Crop.pickImage(this);
dialog.dismiss();
}
}
public boolean checkPermissions() {
Session s = Session.getActiveSession();
if (s != null) {
return s.getPermissions().contains("publish_actions");
} else
return false;
}
public void requestPermissions() {
Session s = Session.getActiveSession();
if (s != null)
s.requestNewPublishPermissions(new Session.NewPermissionsRequest(
this, PERMISSIONS));
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle savedState) {
super.onSaveInstanceState(savedState);
uiHelper.onSaveInstanceState(savedState);
}
}
我哈弗研究了这个tipic相当长的一段所示。
我遇到什么,在很多的教程,而在许多其他的网站,它是说,Facebook类是现在已经过时
Facebook fb = new Facebook(App_Id);
使用。在Facebook开发者页面也使用Session类。那是什么以及如何使用它?
这是我的第一个应用程序。如果有人指导我详细了解这个概念,那对我来说会很有帮助。
谢谢
答
此类应考虑废弃。
此类的所有公共成员都被故意弃用。新代码应改为使用Session来管理会话状态,请求进行API请求,并使用WebDialog进行对话请求。
将@Deprecated添加到此类会导致引用此类的其他弃用类中的警告。这是整个班级不被弃用的唯一原因。
阅读下面的文档就弃用: -
https://developers.facebook.com/docs/reference/android/current/class/Facebook/
更新Facebook的SDK从下面的链接: -
我使用Session类,但还是有一些问题,我更新我的问题。 Plz看看 – 2014-11-05 06:01:04