从facebook Android成功登录后切换活动

时间:2017-03-21 05:33:01

标签: android facebook facebook-login

我正在尝试从Facebook登录。它很成功但是当我尝试切换活动时,它并没有发生在野兔身上。它只显示带有注销选项的按钮。

以下是我的代码

    fbLogin=(LoginButton)findViewById(R.id.login_button);
    fbLogin.setReadPermissions(Arrays.asList("email"));
    fbLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            AccessToken accessToken = loginResult.getAccessToken();

            System.out.println("  accesstoken=  " + accessToken);
            Profile profile = Profile.getCurrentProfile();

            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            Log.v("LoginActivity", response.toString());

                            JSONObject json = response.getJSONObject();
                            try {
                                email1 = json.getString("email");

                                Toast.makeText(Login_Activity.this, "email is" + email1, Toast.LENGTH_SHORT).show();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });

            Bundle parameters = new Bundle();
            parameters.putString("fields", "id,name,email");
            request.setParameters(parameters);
            request.executeAsync();


            displayMessage(profile);
        }

        @Override
        public void onCancel() {
            Toast.makeText(Login_Activity.this, "Login Canceled", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onError(FacebookException e) {
            Toast.makeText(Login_Activity.this, "Cannot connect.. Facebook error!!", Toast.LENGTH_SHORT).show();
        }
    });

及以下是切换活动的功能

private void displayMessage(Profile profile){
    if(profile != null) {

        mail.setText(email1);
        session.createUserLoginSession(true, "FB");
        FGname=profile.getName();
        FGimage=String.valueOf(profile.getProfilePictureUri(100, 100));
        FGemail= email1;
        gv.setEmail(email1);

        Toast.makeText(Login_Activity.this, "display profile executed", Toast.LENGTH_SHORT).show();
        new AsynDataClassFbGoogleLogin().execute();

    }
    else
    {

    }
}

此外,当我再次启动我的应用程序时,登录已成功完成。

2 个答案:

答案 0 :(得分:0)

Just add an intent to switch to another activity in onCompleted callback as follows :


GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback() {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response) {
                            Log.v("LoginActivity", response.toString());

                            JSONObject json = response.getJSONObject();
                            try {
                                email1 = json.getString("email");

                                Toast.makeText(Login_Activity.this, "email is" + email1, Toast.LENGTH_SHORT).show();

                             // switch to another activity after successful login
                             Intent intent = new Intent(LoginActivity.this, AnotherActivity.class);
                             startActivity(intent);

                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    });

答案 1 :(得分:0)

您需要使用Intent来切换活动:

 private void displayMessage(Profile profile){
if(profile != null) {

    mail.setText(email1);
    session.createUserLoginSession(true, "FB");
    FGname=profile.getName();
    FGimage=String.valueOf(profile.getProfilePictureUri(100, 100));
    FGemail= email1;
    gv.setEmail(email1);

    Toast.makeText(Login_Activity.this, "display profile executed", Toast.LENGTH_SHORT).show();
    new AsynDataClassFbGoogleLogin().execute();

  //This code is what switches activities
  Intent i = new Intent(this, NewActivity.class);
  startActivity(i);
}
else
{

}

}

您还需要确保将新活动添加到清单中:

<activity
            android:name=".NewActivity"
            android:theme="@style/AppTheme.NoActionBar">


        </activity>

您可以在此处阅读有关活动的信息:https://developer.android.com/guide/components/activities/index.html