Google登录"首次登录时检查"和"退出" android应用程序中的问题

时间:2015-05-12 05:11:16

标签: android sharedpreferences logout google-plus-signin

我正在创建一个Android应用程序,当用户安装应用程序时,他/她需要使用Google帐户登录,登录成功后,他/她将被重定向到仪表板页面。如果他/她关闭应用程序并再次打开它并且如果他们已经登录,那么应该显示仪表板活动。另外,我的仪表板活动中有一个抽屉布局,其中有一个"退出"用户可以从中注销并再次打开登录活动的按钮。

  • 我正在使用SharedPreference检查android中的首次登录

    以下是我所面临的问题

  • 应用程序打开仪表板活动而不要求登录。

  • 当"退出"从仪表板活动中的抽屉布局中单击,一秒钟它将进入登录活动,但会再次自动登录。简而言之"退出"它不起作用,它让我登录。

以下是用户首次打开应用时的屏幕截图。它没有要求我登录。 It doesn't ask for log-in

但是当我特意点击退出时,会显示登录活动, enter image description here

下面我已经显示了屏幕截图或结果,每次我尝试注销但它再次登录我。任何输入将不胜感激。 enter image description here

LoginActivity.java

public class GooglePlayServicesActivity extends Activity implements OnClickListener, ConnectionCallbacks, OnConnectionFailedListener {

    private static final int RC_SIGN_IN = 0;
    private static final String TAG = "GoogleLogin";

    // Google client to communicate with Google
    public GoogleApiClient mGoogleApiClient;
    private boolean mIntentInProgress;
    private boolean signedInUser;
    private ConnectionResult mConnectionResult;
    private SignInButton signinButton;
    private TextView username, emailLabel;
    private LinearLayout signInFrame;
    private RelativeLayout profileFrame;
    private View relativeLayout;
    private String personPhotoUrl;
    private String personID;
    private String personName;
    private String personEmail;
    private boolean hasLoggedIn;
    private String personGender;
    private String personDOB;
    private String personFullName;
    private String user_gender = "";
    // DatabaseHandler dbHandler = new DatabaseHandler(this);
   public void checkLogIn(){
       Log.d(TAG,"Checked Logged In called ");
       SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE);
       hasLoggedIn = settings.getBoolean("hasLoggedIn", false);
       Log.d("Value of HasLoggedin",":"+String.valueOf(hasLoggedIn));
       if(hasLoggedIn){
           Intent i = new Intent(getApplicationContext(),ActivityFeedActivity.class);
           startActivityFeedActivity(i);
           GooglePlayServicesActivity.this.finish();
       }
   }

    private void startActivityFeedActivity(Intent i) {
        startActivity(i);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //checkLogIn();
        setContentView(R.layout.google_login);
        //initFacebookLogin();
        signinButton = (SignInButton) findViewById(R.id.signin);
        signinButton.setOnClickListener(this);
        relativeLayout = getLayoutInflater().inflate(R.layout.profile_view, null);
        username = (TextView) relativeLayout.findViewById(R.id.name);
        emailLabel = (TextView)  relativeLayout.findViewById(R.id.email);

        profileFrame = (RelativeLayout) relativeLayout.findViewById(R.id.profileView);
        signInFrame = (LinearLayout) findViewById(R.id.signinFrame);

        mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(Plus.API, Plus.PlusOptions.builder().build()).addScope(Plus.SCOPE_PLUS_LOGIN).build();
    }

    protected void onStart() {
        super.onStart();
        mGoogleApiClient.connect();
    }

    protected void onStop() {
        super.onStop();
        if (mGoogleApiClient.isConnected()) {
            mGoogleApiClient.disconnect();
        }
    }

    private void resolveSignInError() {
        if (mConnectionResult.hasResolution()) {
            try {
                mIntentInProgress = true;
                mConnectionResult.startResolutionForResult(this, RC_SIGN_IN);
            } catch (SendIntentException e) {
                mIntentInProgress = false;
                mGoogleApiClient.connect();
            }
        }
    }

    @Override
    public void onConnectionFailed(ConnectionResult result) {
        if (!result.hasResolution()) {
            GooglePlayServicesUtil.getErrorDialog(result.getErrorCode(), this, 0).show();
            return;
        }

        if (!mIntentInProgress) {
            // store mConnectionResult
            mConnectionResult = result;

            if (signedInUser) {
                resolveSignInError();
            }
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int responseCode, Intent intent) {
        switch (requestCode) {
            case RC_SIGN_IN:
                if (responseCode == RESULT_OK) {
                    signedInUser = false;

                }
                mIntentInProgress = false;
                if (!mGoogleApiClient.isConnecting()) {
                    mGoogleApiClient.connect();
                }
                break;
        }
    }

    @Override
    public void onConnected(Bundle arg0) {
        signedInUser = false;
        getProfileInformation();
        SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE); // 0 - for private mode
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean("hasLoggedIn", true);
        editor.putString("personID", getPersonID());
        editor.putString("personName", getPersonFullName());
        editor.putString("personEmail",getPersonEmail());
        editor.putString("picURL",getPicUrl());
        editor.commit();
        Intent i = new Intent(this,ActivityFeedActivity.class);
        startActivityFeedActivity(i);
        GooglePlayServicesActivity.this.finish();
    }


    private void getProfileInformation() {
        try {
            if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
                Person currentPerson = Plus.PeopleApi.getCurrentPerson(mGoogleApiClient);

                String personID = currentPerson.getId();
                String personFullName = currentPerson.getDisplayName(); // Gives FullName
                String personName = String.valueOf(currentPerson.getName()); //Gives First Name and Last Name in Json formay
                String personGender = String.valueOf(currentPerson.getGender()); //Gives Gender in int 0:Male, 1:Female
                String personDOB = currentPerson.getBirthday();
                String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
                String personPicUrl = currentPerson.getImage().getUrl();

                setpersonID(currentPerson.getId());
                setpersonFullName(currentPerson.getDisplayName());
                setpersonName(currentPerson.getName());
                setpersonEmail(email);
                setpersonGender(personGender);
                setPicUrl(currentPerson.getImage().getUrl());
                setpersonDOB(currentPerson.getBirthday());

                Log.d(TAG, "Gender:" + personGender + " DOB:" + personDOB + " FirstName:" + personName + "  ");
                username.setText(personFullName);
                emailLabel.setText(email);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onConnectionSuspended(int cause) {
        mGoogleApiClient.connect();
       // updateProfile(false);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.signin:
                googlePlusLogin();
                break;
        }
    }

    private void googlePlusLogin() {
        if (!mGoogleApiClient.isConnecting()) {
            signedInUser = true;
            resolveSignInError();
        }
    }

}

DashboardActivity.java

private void initDrawerList(String[] values){
        this.drawerList = (ListView) findViewById(R.id.navdrawer);
        final DrawerLayout layout = this.drawerLayout;
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, values);
        this.drawerList.setAdapter(adapter);
        this.drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                switch (position) {
                    case 0:
                        layout.closeDrawer(Gravity.START);
                        break;
                    case 1:
                        layout.closeDrawer(Gravity.START);
                        break;
                    case 2:
                        layout.closeDrawer(Gravity.START);
                        break;
                    case 3:
                        googleLogout();
                        Log.d(TAG,"Returned from function");
                        break;
                }
            }
        });
    }

    private void googleLogout() {
        SharedPreferences settings = getSharedPreferences(Constants.PREFS_NAME, MODE_PRIVATE);
        SharedPreferences.Editor editor = settings.edit();
        editor.clear();
        editor.commit();
        if (mGoogleApiClient.isConnected()){
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            mGoogleApiClient.disconnect();
            mGoogleApiClient.connect();
            Intent i = new Intent(this,GooglePlayServicesActivity.class);
            startActivity(i);
            ActivityFeedActivity.this.finish();
        }
        else {
            Log.d(TAG,"entered else");
        }

        //updateProfile(false);
    }

1 个答案:

答案 0 :(得分:1)

点击退出时试试方法

从Google退出

private void signOutFromGplus(){
        if (mGoogleApiClient.isConnected(){
            Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
            mGoogleApiClient.disconnect();
            mGoogleApiClient.connect();
        }
}