在 Android 上实现 facebook 登录的最佳方法是啥?
Posted
技术标签:
【中文标题】在 Android 上实现 facebook 登录的最佳方法是啥?【英文标题】:What is the best way to implement facebook login on Android?在 Android 上实现 facebook 登录的最佳方法是什么? 【发布时间】:2015-04-09 00:35:12 【问题描述】:我在facebook developers 上查看了教程,它看起来与我在互联网上找到的不同。哪一种是实现facebook登录的最佳方式?还有,哪里有学习facebook sdk for android的好地方,好像官方的不完整?
Facebook.developer 使用 loginButton.setRegisterCallback();
@Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
View view = inflater.inflate(R.layout.splash, container, false);
loginButton = (LoginButton) view.findViewById(R.id.login_button);
loginButton.setReadPermissions("user_friends");
// If using in a fragment
loginButton.setFragment(this);
// Other app specific specialization
// Callback registration
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>()
@Override
public void onSuccess(LoginResult loginResult)
// App code
@Override
public void onCancel()
// App code
@Override
public void onError(FacebookException exception)
// App code
);
programmerguru.com 使用 UiLifecycleHelper,我在官方网站上没有找到它
public class MainActivity extends ActionBarActivity
// Create, automatically open (if applicable), save, and restore the
// Active Session in a way that is similar to Android UI lifecycles.
private UiLifecycleHelper uiHelper;
private View otherView;
private static final String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set View that should be visible after log-in invisible initially
otherView = (View) findViewById(R.id.other_views);
otherView.setVisibility(View.GONE);
// To maintain FB Login session
uiHelper = new UiLifecycleHelper(this, callback);
uiHelper.onCreate(savedInstanceState);
// Called when session changes
private Session.StatusCallback callback = new Session.StatusCallback()
@Override
public void call(Session session, SessionState state,
Exception exception)
onSessionStateChange(session, state, exception);
;
// When session is changed, this method is called from callback method
private void onSessionStateChange(Session session, SessionState state,
Exception exception)
final TextView name = (TextView) findViewById(R.id.name);
final TextView gender = (TextView) findViewById(R.id.gender);
final TextView location = (TextView) findViewById(R.id.location);
// When Session is successfully opened (User logged-in)
if (state.isOpened())
Log.i(TAG, "Logged in...");
// make request to the /me API to get Graph user
Request.newMeRequest(session, new Request.GraphUserCallback()
// callback after Graph API response with user
// object
@Override
public void onCompleted(GraphUser user, Response response)
if (user != null)
// Set view visibility to true
otherView.setVisibility(View.VISIBLE);
// Set User name
name.setText("Hello " + user.getName());
// Set Gender
gender.setText("Your Gender: "
+ user.getProperty("gender").toString());
location.setText("Your Current Location: "
+ user.getLocation().getProperty("name")
.toString());
).executeAsync();
else if (state.isClosed())
Log.i(TAG, "Logged out...");
otherView.setVisibility(View.GONE);
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
Log.i(TAG, "OnActivityResult...");
@Override
public void onResume()
super.onResume();
uiHelper.onResume();
@Override
public void onPause()
super.onPause();
uiHelper.onPause();
@Override
public void onDestroy()
super.onDestroy();
uiHelper.onDestroy();
@Override
public void onSaveInstanceState(Bundle outState)
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
【问题讨论】:
使用官方教程。之所以不同,是因为 Facebook 刚刚发布了 SDK v4,让登录和分享变得更加容易。互联网上的许多其他教程仍然适用于 v3 甚至 v2 (gasp),并且已经过时了。 感谢您的回答。它回答了我的问题。 【参考方案1】:正如@Ming Li 所说,坚持the official tutorials。之所以有许多不同的教程,是因为 Facebook SDK 有许多不同的版本,每个版本可能都有不同的方式来处理 Facebook 的事情。如果他们想出一个更好的主意来用 Facebook 做事,他们就会改变它。
【讨论】:
以上是关于在 Android 上实现 facebook 登录的最佳方法是啥?的主要内容,如果未能解决你的问题,请参考以下文章
使 facebook 登录与 Android Webview 一起工作