我一直试图通过facebook集成成功登录尝试启动新活动,但是当记录引脚后按下登录按钮时,它返回到登录按钮所在的相同活动 .
我已经有了转移到下一个活动的意图,但它似乎没有对登录成功做出任何响应 .
这是我的代码(抱歉我很久以来一直在这里不整洁):
import android.content.Intent;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.util.Log;
import com.facebook.AccessToken;
import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphResponse;
import com.facebook.login.Login;
import com.facebook.login.LoginManager;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;
import android.widget.Toast;
import com.facebook.GraphRequest;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import org.json.JSONObject;
public class userlogin extends FragmentActivity {
LoginButton loginButton; //The Facebook login button
TextView loginstatus;
CallbackManager callbackManager;
private AccessToken accessToken;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_userlogin);
loginButton = (LoginButton) findViewById(R.id.fb_login_button);
loginstatus = (TextView)findViewById(R.id.loginstatus);
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
// **Description: If the user cancels the login before it is completed a message will
// be shown to let them know that they are not logged in
//**Parameters:
//** Loginresult: the results of the login attempt.
//**
//**Precondition: loginButton must have been clicked first
//**
//**throws: N/A
//** returns: a textView that displays a message of whether or not the login was succesful
public void onSuccess(LoginResult loginResult) {
loginstatus.setText("Login Success");
accessToken = loginResult.getAccessToken();
Toast.makeText(userlogin.this, "Eureka", Toast.LENGTH_LONG).show();
GraphRequest request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
public void onCompleted(JSONObject object, GraphResponse response) {
Log.d("FB", "complete");
Log.d("FB", object.optString("name"));
Log.d("FB", object.optString("link"));
Log.d("FB", object.optString("id"));
Log.d("FB", object.optString("gender"));
TextView Username;
Username = (TextView) findViewById(R.id.loginstatus);
String id = object.optString("id");
String name = object.optString("name");
Username.setText("Welcome:" + name);
ImageView userPhoto = (ImageView) findViewById(R.id.facebookuserPortrait);
String i = "http://graph.facebook.com/" + id + "/picture?type=large";
Glide.with(userlogin.this).load(i).override(350, 350).into(userPhoto);
Intent intent = new Intent(userlogin.this, AlertsandPlanning.class);
startActivity(intent);
}
// **Description: If the user cancels the login before it is completed a message will
// be shown to let them know that they are not logged in
//**Parameters: N/A
//**
//**Precondition: loginButton must have been clicked first
//**
//**throws: N/A
//** returns: a textView that displays a message
public void onCancel() {
loginstatus.setText("Login Canceled");
}
// **Description: If the user has an error during the login before it is completed
// a message will be shown to let them know that they are not logged in
//**Parameters: FacebookException: an error transpoding if the login fails
//**
//**Precondition: loginButton must have been clicked first
//**
//**throws: Exception
//** returns: a textView that displays a message
public void onError(FacebookException exception) {
loginstatus.setText("Something went wrong \n+" +
"Check your credentials and try again");
}
});
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
}
protected void onActivivityResult(int requestCode, int resultCode, Intent data) {
userlogin.super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
});
}
}
我很欣赏任何有用的见解,我可以从头开始讨论这项活动,如果似乎没有任何工作可以再试一次 .
重申此问题:单击“继续使用Facebook”按钮,显示的下一个活动是userlogin活动,而不是AlertsandPlanning活动 .
更新:
我在这里尝试了当前的第一条评论是更新的代码,
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import com.facebook.CallbackManager;
import com.facebook.login.widget.LoginButton;
import com.facebook.FacebookSdk;
import android.view.View;
import com.facebook.login.LoginResult;
import com.facebook.FacebookCallback;
import org.json.JSONObject;
import android.widget.Toast;
import com.facebook.AccessToken;
import com.facebook.login.LoginManager;
import com.facebook.GraphResponse;
import com.facebook.FacebookException;
import com.facebook.GraphRequest;
import com.facebook.GraphRequestAsyncTask;
public class UserLogin extends AppCompatActivity implements View.OnClickListener {
LoginButton btnFacebookLogin;
CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_user_login);
callbackManager = CallbackManager.Factory.create();
setUI();
}
private void setUI() {
btnFacebookLogin = (LoginButton) findViewById(R.id.FBLoginBtn);
btnFacebookLogin.setOnClickListener(this);
btnFacebookLogin.setReadPermissions("email", "public_profile");
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.FBLoginBtn:
facebookLogin();
}
}
private void facebookLogin() {
// Callback registration
btnFacebookLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
//应用代码
final AccessToken accessToken = loginResult.getAccessToken();
GraphRequestAsyncTask request = GraphRequest.newMeRequest(accessToken, new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject user, GraphResponse graphResponse) {
LoginManager.getInstance().logOut();
String username = (user.optString("name"));
}
}).executeAsync();
Toast.makeText(getApplicationContext(), "Login Success with facebook", Toast.LENGTH_SHORT).show();
Intent startAlertsAndPlanning = new Intent(UserLogin.this, AlertsAndPlanning.class);
startActivity(startAlertsAndPlanning);
}
@Override
public void onCancel() {
// App code
}
@Override
public void onError(FacebookException exception) {
// App code
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
}
1 回答
请尝试使用以下代码 . 我希望它对您有所帮助,请将CamelCase表示法用于类名 .