简体   繁体   English

使用Facebook SDK登录到应用程序

[英]using facebook sdk to login to app

Update 更新资料

I used Session class as told. 我使用了Session类。 But still, i dont think its working. 但是,我仍然认为它不起作用。 below is my code: 下面是我的代码:

But still, my edittext is not updated with name. 但是,我的edittext仍未更新名称。 it is showing You are not logged in. Neither any of the log messages in the Session.CallBack method are shown in my logcat 它显示您尚未登录。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);
  }
}

I haver researched on this tipic for quite a while. 我已经对这种技巧进行了一段时间的研究。

What I came across is that in many tutorials 我遇到的是,在许多教程中

Facebook fb = new Facebook(App_Id); 

is used while in many other sites, it is said that Facebook class is now deprecated. 在许多其他网站中使用时,据说Facebook类现在已被弃用。 In facebook developers page also, Session class is used. 同样在Facebook开发人员页面中,使用Session类。 What is that and how to use that? 那是什么,怎么用?

This is my first app. 这是我的第一个应用程序。 If any one guide me with complete detail about this concept, it will be greatful for me. 如果有人指导我提供有关此概念的完整详细信息,那对我来说将非常有用。

Thank You 谢谢

THIS CLASS SHOULD BE CONSIDERED DEPRECATED. 不应考虑该类。

All public members of this class are intentionally deprecated. 此类的所有公共成员均被有意弃用。 New code should instead use Session to manage session state, Request to make API requests, and WebDialog to make dialog requests. 相反,新代码应使用Session管理会话状态,使用Request进行API请求,并使用WebDialog进行对话框请求。

Adding @Deprecated to this class causes warnings in other deprecated classes that reference this one. 将@Deprecated添加到此类会在引用该类的其他不推荐使用的类中引起警告。 That is the only reason this entire class is not deprecated. 这是不弃用整个类的唯一原因。

Read below document regarding deprecation :- 阅读以下有关弃用的文件:-

https://developers.facebook.com/docs/reference/android/current/class/Facebook/ https://developers.facebook.com/docs/reference/android/current/class/Facebook/

Update your sdk of facebook from below link :- 从以下链接更新您的Facebook SDK:-

https://developers.facebook.com/docs/android/upgrading https://developers.facebook.com/docs/android/upgradeing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM