0
在我的Android應用程序中,每當我重新打開我最小化的應用程序,它會再次給我登錄屏幕。它應該直接給我導航/儀表盤活動,因爲我沒有註銷會話。谷歌在Android登錄會話不起作用
所以這是我的代碼。我需要一些可以正確創建用戶會話的實現。
package com.tricktechs.googlelogin;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.google.android.gms.auth.api.Auth;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.auth.api.signin.GoogleSignInResult;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.SignInButton;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.common.api.Status;
public class MainActivity extends AppCompatActivity implements View.OnClickListener,GoogleApiClient.OnConnectionFailedListener{
LinearLayout prof_section;
Button signout;
SignInButton signInButton;
TextView name,email;
ImageView prof_pic;
GoogleApiClient googleApiClient;
final static int REQ_CODE = 9001;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prof_section = (LinearLayout)findViewById(R.id.prof_section);
signout = (Button) findViewById(R.id.btn_logout);
signInButton = (SignInButton) findViewById(R.id.bn_login);
name = (TextView) findViewById(R.id.name);
email = (TextView) findViewById(R.id.email);
prof_pic = (ImageView)findViewById(R.id.prof_pic);
//googleApiClient = (GoogleApiClient) findViewById()
prof_section.setVisibility(View.GONE);
GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
googleApiClient = new GoogleApiClient.Builder(this).enableAutoManage(this,this).addApi(Auth.GOOGLE_SIGN_IN_API,signInOptions).build();
signInButton.setOnClickListener(this);
signout.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId())
{
case R.id.btn_logout :
signOut();
break;
case R.id.bn_login :
signIn();
break;
}
}
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
private void signIn()
{
Intent intent = Auth.GoogleSignInApi.getSignInIntent(googleApiClient);
startActivityForResult(intent,REQ_CODE);
}
private void signOut()
{
Auth.GoogleSignInApi.signOut(googleApiClient).setResultCallback(new ResultCallback<Status>() {
@Override
public void onResult(@NonNull Status status) {
updateUI(false);
}
});
}
private void handleResilt(GoogleSignInResult googleSignInResult)
{
if(googleSignInResult.isSuccess())
{
GoogleSignInAccount account = googleSignInResult.getSignInAccount();
String Name = account.getDisplayName();
String Email = account.getEmail();
// String image_url = account.getPhotoUrl().toString();
name.setText(Name);
email.setText(Email);
// Glide.with(this).load(image_url).into(prof_pic);
updateUI(true);
}
else
{
updateUI(false);
}
}
private void updateUI(boolean isLogin)
{
if(isLogin)
{
prof_section.setVisibility(View.VISIBLE);
signInButton.setVisibility(View.GONE);
}
else
{
prof_section.setVisibility(View.GONE);
signInButton.setVisibility(View.VISIBLE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQ_CODE)
{
GoogleSignInResult reslut = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
handleResilt(reslut);
}
}
}