我想要以下行动: -
我的问题是如何实现这一目标?鉴于facebook sdk教程,我一直在摆弄登录按钮和自定义Ui课程,但我不想让片段存在,因为我甚至不需要它。
有人建议吗?
答案 0 :(得分:0)
添加Facebook SDK浏览您的项目,在您的主要活动中,它就像这样
package com.example.integration;
import java.util.Arrays;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.facebook.Request;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.SessionState;
import com.facebook.model.GraphUser;
import com.facebook.widget.LoginButton;
public class MainActivity extends Activity {
private LoginButton fbLoginBtn;
private SharedPreferences mPrefs;
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(final Session session, final SessionState state, final Exception exception) {
onSessionStateChange(session, state, exception);
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uiHelper = new UiLifecycleHelper(MainActivity.this, callback);
uiHelper.onCreate(savedInstanceState);
fbLoginBtn = (LoginButton) findViewById(R.id.fb_login_button);
fbLoginBtn.setReadPermissions(Arrays.asList("email"));
mPrefs = getPreferences(MODE_PRIVATE);
}
@Override
public void onResume() {
super.onResume();
Session session = Session.getActiveSession();
if (session != null &&
(session.isOpened() || session.isClosed()) ) {
onSessionStateChange(session, session.getState(), null);
}
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
private void onSessionStateChange(final Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Editor edit = mPrefs.edit();
edit.putString("access_token", session.getAccessToken());
edit.putString("expires", session.getExpirationDate() + "");
edit.commit();
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (user != null) {
String firstName = user.getFirstName();
String lastName = user.getLastName();
String id = user.getId();
String email = (String) user.asMap().get("email");
Log.i("facebookid", id);
Log.i("firstName", firstName);
Log.i("lastName", lastName);
Log.i("AccessToken", session.getAccessToken());
Log.i("Email", email);
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("name", firstName);
startActivity(intent);
}
}
});
}
else if (state.isClosed()) {
Toast.makeText(this, "Please Login", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(MainActivity.this, requestCode, resultCode, data);
}
}
在您的xml中,您需要添加Facebook登录按钮
<com.facebook.widget.LoginButton
android:id="@+id/fb_login_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
facebook:confirm_logout="false"
facebook:fetch_user_info="true" />