Google android 应用程序中的 "check first time login" 和 "Logout" 登录问题
Google Sign-in "check first time login" and "Logout" issues in android application
我正在制作一个android应用程序,当用户安装应用程序时,he/she需要使用google帐户登录,登录成功后,he/she 将被重定向到仪表板页面。如果 he/she 关闭应用程序并再次打开它并且如果他们已经登录,那么应该显示仪表板 activity 本身。我的仪表板 activity 中还有一个 drawerLayout,它有一个“注销”按钮,用户可以从该按钮注销和登录 activity 再次打开。
我正在使用 SharedPreference 在 android
中检查首次登录
以下是我面临的问题
应用程序在不要求登录的情况下打开仪表板activity。
当从仪表板 activity 的抽屉布局中单击“注销”时,它会在一秒钟内进入登录 activity 但会自动再次登录。简而言之,“注销”不起作用,它让我保持登录状态。
以下为用户首次打开应用时的截图。它不要求我登录。
但是当我特别点击注销时,会显示登录 activity,
下面我已经显示了屏幕截图或结果,每次我尝试注销但它再次登录我。任何输入将不胜感激。
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);
}
点击注销时尝试方法
退出 google
private void signOutFromGplus(){
if (mGoogleApiClient.isConnected(){
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
}
}
我正在制作一个android应用程序,当用户安装应用程序时,he/she需要使用google帐户登录,登录成功后,he/she 将被重定向到仪表板页面。如果 he/she 关闭应用程序并再次打开它并且如果他们已经登录,那么应该显示仪表板 activity 本身。我的仪表板 activity 中还有一个 drawerLayout,它有一个“注销”按钮,用户可以从该按钮注销和登录 activity 再次打开。
我正在使用 SharedPreference 在 android
中检查首次登录以下是我面临的问题
应用程序在不要求登录的情况下打开仪表板activity。
当从仪表板 activity 的抽屉布局中单击“注销”时,它会在一秒钟内进入登录 activity 但会自动再次登录。简而言之,“注销”不起作用,它让我保持登录状态。
以下为用户首次打开应用时的截图。它不要求我登录。
但是当我特别点击注销时,会显示登录 activity,
下面我已经显示了屏幕截图或结果,每次我尝试注销但它再次登录我。任何输入将不胜感激。
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);
}
点击注销时尝试方法
退出 google
private void signOutFromGplus(){
if (mGoogleApiClient.isConnected(){
Plus.AccountApi.clearDefaultAccount(mGoogleApiClient);
mGoogleApiClient.disconnect();
mGoogleApiClient.connect();
}
}