我一直在浏览文档Android - Facebook login
我仍面临的问题仍然是 - 在用户首次登录后,在图形请求和api调用之后,不会调用intentToHomeScreen()
方法。请注意,这种情况很少发生。进度条继续加载。
这是我的代码,抱歉发布这么长的帖子:
public class SignInActivity extends BaseActivity {
final int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 1;
@BindView(R.id.sign_fb_splash_linear)
LinearLayout mSignIn;
@BindView(R.id.sign_in_logo_img)
ImageView mLogoImg;
@BindView(R.id.splash_relative)
RelativeLayout mRelative;
private CallbackManager mCallbackManager;
private Profile profile;
private String fbid;
private String fUserName;
private String gender;
private String email;
private Uri imageUri;
private SharedPref preferences;
private Context mContext;
@BindView(R.id.progress_bar)
ProgressBar progressBar;
private User userFromResponse;
private GpsLocation mGpsLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setting the context
mContext = this;
preferences = SharedPref.getInstance(mContext);
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(getApplication());
if (BuildConfig.enableCrashlytics) {
Fabric.with(this, new Crashlytics());
}
setContentView(R.layout.activity_signin);
ButterKnife.bind(this);
mGpsLocation = GpsLocation.getInstance(getApplicationContext(), this);
mGpsLocation.onStart();
if (userIsLoggedIn()) {
intentToHomeScreen();
}
Animation animShow = AnimationUtils.loadAnimation(this, R.anim.slide_up);
mSignIn.startAnimation(animShow);
}
private void intentToHomeScreen() {
Intent intent = new Intent(SignInActivity.this, HomeScreenActivity.class);
startActivity(intent);
finish();
}
private boolean userIsLoggedIn() {
return preferences.getLongValue(mContext, preferences.KEY_USER_ID) != 0;
}
private void initialiseFacebookSdk() {
LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "email"));
LoginManager.getInstance().registerCallback(mCallbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
showProgressBar(true, progressBar);
GraphRequest request = GraphRequest.newMeRequest(loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
try {
if (Profile.getCurrentProfile() != null && object != null && response != null) {
profile = Profile.getCurrentProfile();
email = object.optString("email");
gender = object.optString("gender");
fbid = profile.getId();
fUserName = profile.getName();
imageUri = profile.getProfilePictureUri(300, 300);
String loginType = "Facebook";
User user = new User();
user.email = email;
user.phoneNumber = "";
user.loginType = loginType;
user.gender = gender;
user.name = fUserName;
user.facebookId = fbid;
// sending to api
IAppAPIInterface apiManagerInterface = ApiManager.getApiManager();
Call<User> call = apiManagerInterface.postFbDetails(user);
call.enqueue(new Callback<User>() {
@Override
public void onResponse(Call<User> call, Response<User> response) {
showProgressBar(false, progressBar);
if (response != null && response.body() != null) {
userFromResponse = response.body();
if (preferences != null) {
SharedPref.setBoolean(mContext, preferences.KEY_FIRST_USE_HOME, true);
SharedPref.setLong(mContext, preferences.KEY_USER_ID, userFromResponse.userRecordId);
SharedPref.setString(mContext, preferences.KEY_NAME, userFromResponse.name);
SharedPref.setString(mContext, preferences.KEY_PROF_IMG_URL, String.valueOf(imageUri));
SharedPref.setString(mContext, preferences.KEY_USER_TOKEN, userFromResponse.token);
}
// Sending to home screen
intentToHomeScreen();
} else if (response != null) {
AppUtilityMethods.getInstance(SignInActivity.this).showMsgToast(SignInActivity.this, response.message(), mSignIn);
}
}
@Override
public void onFailure(Call<User> call, Throwable t) {
showProgressBar(false, progressBar);
if (t.getMessage().equals("timeout")) {
AppUtilityMethods.getInstance(SignInActivity.this).showMsgToast(SignInActivity.this, getString(R.string.timeout), mSignIn);
} else {
AppUtilityMethods.getInstance(SignInActivity.this).showMsgToast(SignInActivity.this, t.getMessage(), mSignIn);
}
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
);
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name,email,picture");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
showProgressBar(false, progressBar);
Log.d("FACEBOOK", "On Cancel");
}
@Override
public void onError(FacebookException ex)
{
showProgressBar(false, progressBar);
AppUtilityMethods.getInstance(getApplicationContext()).showMsgToast(getApplicationContext(), "Connetion error!", null);
Log.d("FACEBOOK", "On Error: " + ex.getMessage());
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
}
return;
}
case OpenDialogPermission.MY_PERMISSIONS_REQUEST_LOCATION:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
mGpsLocation.isGPSEnable();
} else {
finish();
}
}
}
@Override
protected void onStop() {
super.onStop();
mGpsLocation.onStop();
}
@Override
protected void onStart() {
super.onStart();
mGpsLocation.onStart();
}
@Override
protected void onResume() {
super.onResume();
mGpsLocation.checkPermissions();
}
@OnClick({R.id.sign_fb_splash_linear})
void onClick() {
if (Utilities.isNetworkAvailable(this)) {
mCallbackManager = CallbackManager.Factory.create();
initialiseFacebookSdk();
} else {
AppUtilityMethods.getInstance(this).showNetworkNotAvailToast(this, mSignIn);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mCallbackManager.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onBackPressed() {
finish();
}
}