startActivity()无效

时间:2016-04-06 19:12:15

标签: android android-intent

我有一项活动允许用户通过Facebook登录该应用程序。在onCreate()方法中,我检查用户是否已连接,如果是,则启动其他活动的意图。

如果用户未登录,他必须单击按钮然后输入他的凭证,然后他应该被重定向到另一个活动。

我的问题是,成功登录后,应用程序保持“LoginActivity”状态,Facebook按钮变为“logout”。我的意图似乎没有被调用。这是我的代码:

import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.CallbackManager;
import com.facebook.FacebookCallback;
import com.facebook.FacebookException;
import com.facebook.FacebookSdk;
import com.facebook.GraphRequest;
import com.facebook.GraphResponse;
import com.facebook.login.LoginResult;
import com.facebook.login.widget.LoginButton;

import org.json.JSONObject;


public class LoginActivity extends AppCompatActivity
{
    private CallbackManager callbackManager;

    private LoginButton loginButton;

    private TextView btnLogin;

    private ProgressDialog progressDialog;

    public User user;


    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_login);

        if(PrefUtils.getCurrentUser(LoginActivity.this) != null)
        {
            Intent intent = new Intent(LoginActivity.this, GBPSActivity.class);
            startActivity(intent);
            finish();
        }
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        callbackManager = CallbackManager.Factory.create();

        loginButton = (LoginButton) findViewById(R.id.login_button);
        loginButton.setReadPermissions("public_profile", "email", "user_friends");

        btnLogin = (TextView)findViewById(R.id.btnLogin);
        btnLogin.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                progressDialog = new ProgressDialog(LoginActivity.this);
                progressDialog.setMessage("...Loading...");
                progressDialog.show();

                loginButton.performClick();
                loginButton.setPressed(true);
                loginButton.invalidate();
                loginButton.registerCallback(callbackManager, fbCallback);
                loginButton.setPressed(false);
                loginButton.invalidate();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
    }

    private FacebookCallback<LoginResult> fbCallback = new FacebookCallback<LoginResult>()
    {
        @Override
        public void onSuccess(LoginResult loginResult)
        {
            progressDialog.dismiss();

            GraphRequest request = GraphRequest.newMeRequest(
                    loginResult.getAccessToken(),
                    new GraphRequest.GraphJSONObjectCallback()
                    {
                        @Override
                        public void onCompleted(JSONObject object, GraphResponse response)
                        {
                            try {
                                user = new User();
                                user.facebookID = object.getString("id").toString();
                                user.email = object.getString("email").toString();
                                user.name = object.getString("name").toString();
                                user.gender = object.getString("gender").toString();

                                PrefUtils.setCurrentUser(user, LoginActivity.this);
                            }
                            catch (Exception e)
                            {
                                e.printStackTrace();
                            }

                            Intent intent = new Intent(LoginActivity.this, GBPSActivity.class);
                            startActivity(intent);
                            finish();
                        }
                    }
            );

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

        @Override
        public void onCancel()
        {
            progressDialog.dismiss();
        }

        @Override
        public void onError(FacebookException error)
        {
            progressDialog.dismiss();
        }
    };
}

User类只是一个类似结构的类,而PrefUtils只是一个允许设置/获取当前用户的类。

0 个答案:

没有答案