XML : How to Remember Sign In in Google Account Android

Login Activity Please Help I just wanted to Remember Login in App in background, so that when he reopen the app automatically next activity open rather than sign in activity.

  package xxx.xxxxx.xxxxxx;    /**   * Created by Yoyo on 12/21/2015.   */  import android.app.ProgressDialog;  import android.content.Intent;  import android.os.Bundle;  import android.support.v7.app.AppCompatActivity;  import android.view.View;  import android.view.Menu;  import android.view.MenuItem;  import android.widget.ImageView;  import android.widget.TextView;  import android.widget.Toast;  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.GoogleApiClient.ConnectionCallbacks;  import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;  import com.google.android.gms.common.api.ResultCallback;  import com.google.android.gms.common.api.Status;    public class Login extends AppCompatActivity implements OnConnectionFailedListener, View.OnClickListener, ConnectionCallbacks {      GoogleApiClient mGoogleApiClient;      GoogleSignInOptions gso;      SignInButton signIn_btn;      private static final int RC_SIGN_IN = 0;      ProgressDialog progress_dialog;      @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);            buidNewGoogleApiClient();          setContentView(R.layout.login);          customizeSignBtn();          setBtnClickListeners();          progress_dialog = new ProgressDialog(this);          progress_dialog.setMessage("Signing in....");      }        /*      Configure sign-in to request the user's ID, email address, and basic profile.      User's ID and basic profile are included in DEFAULT_SIGN_IN.      create and  initialize GoogleApiClient object to use Google  Sign-In API and the options specified by gso..      */        private void buidNewGoogleApiClient(){            gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)                  .requestEmail()                  .build();          mGoogleApiClient = new GoogleApiClient.Builder(this)                  .enableAutoManage(this, this )                  .addApi(Auth.GOOGLE_SIGN_IN_API, gso)                  .build();      }        /*        Customize sign-in button. The sign-in button can be displayed in        multiple sizes and color schemes. It can also be contextually        rendered based on the requested scopes. For example. a red button may        be displayed when Google+ scopes are requested, but a white button        may be displayed when only basic profile is requested. Try adding the        Plus.SCOPE_PLUS_LOGIN scope to see the  difference.      */        private void customizeSignBtn(){            signIn_btn = (SignInButton) findViewById(R.id.sign_in_button);          signIn_btn.setSize(SignInButton.SIZE_STANDARD);          signIn_btn.setScopes(gso.getScopeArray());        }        /*        Set on click Listeners on the sign-in sign-out and disconnect buttons       */        private void setBtnClickListeners(){          // Button listeners          signIn_btn.setOnClickListener(this);          findViewById(R.id.sign_out_button).setOnClickListener(this);          findViewById(R.id.disconnect_button).setOnClickListener(this);        }        protected void onStart() {          super.onStart();          mGoogleApiClient.connect();      }        protected void onStop() {          super.onStop();          if (mGoogleApiClient.isConnected()) {              mGoogleApiClient.disconnect();          }      }        @Override      public boolean onCreateOptionsMenu(Menu menu) {          // Inflate the menu; this adds items to the action bar if it is present.          getMenuInflater().inflate(R.menu.menu_main, menu);          return true;      }        @Override      public boolean onOptionsItemSelected(MenuItem item) {          // Handle action bar item clicks here. The action bar will          // automatically handle clicks on the Home/Up button, so long          // as you specify a parent activity in AndroidManifest.xml.          int id = item.getItemId();            //noinspection SimplifiableIfStatement          if (id == R.id.action_settings) {              return true;          }            return super.onOptionsItemSelected(item);      }        @Override      public void onActivityResult(int requestCode, int resultCode, Intent data) {          super.onActivityResult(requestCode, resultCode, data);            // Result returned from launching the Intent from GoogleSignInApi.getSignInIntent(...);          if (requestCode == RC_SIGN_IN) {              if (resultCode != RESULT_OK) {                    progress_dialog.dismiss();                }              GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);              getSignInResult(result);          }      }        @Override      public void onClick(View v) {          switch (v.getId()) {              case R.id.sign_in_button:                  Toast.makeText(this, "start sign process", Toast.LENGTH_SHORT).show();                  gSignIn();                  break;              case R.id.sign_out_button:                  Toast.makeText(this, "Google Sign Out", Toast.LENGTH_SHORT).show();                  gSignOut();                  break;              case R.id.disconnect_button:                  Toast.makeText(this, "Google Access Revoked", Toast.LENGTH_SHORT).show();                  gRevokeAccess();                  break;          }      }        private void gSignIn() {          Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient);          startActivityForResult(signInIntent, RC_SIGN_IN);          progress_dialog.show();      }        private void gSignOut() {          Auth.GoogleSignInApi.signOut(mGoogleApiClient).setResultCallback(                  new ResultCallback<Status>() {                      @Override                      public void onResult(Status status) {                            updateUI(false);                        }                  });      }        private void gRevokeAccess() {          Auth.GoogleSignInApi.revokeAccess(mGoogleApiClient).setResultCallback(                  new ResultCallback<Status>() {                      @Override                      public void onResult(Status status) {                            updateUI(false);                        }                  });      }        private void getSignInResult(GoogleSignInResult result) {            if (result.isSuccess()) {                // Signed in successfully, show authenticated UI.              GoogleSignInAccount acct = result.getSignInAccount();                TextView user_name= (TextView)findViewById(R.id.userName);              TextView email_id= (TextView)findViewById(R.id.emailId);              user_name.setText("UserName: "+ acct.getDisplayName());              email_id.setText("Email Id: " + acct.getEmail());              updateUI(true);              progress_dialog.dismiss();          } else {              // Signed out, show unauthenticated UI.              updateUI(false);          }      }        private void updateUI(boolean signedIn) {          if (signedIn) {              findViewById(R.id.sign_in_button).setVisibility(View.GONE);              findViewById(R.id.sign_out_and_disconnect).setVisibility(View.VISIBLE);          } else {              findViewById(R.id.sign_in_button).setVisibility(View.VISIBLE);              findViewById(R.id.sign_out_and_disconnect).setVisibility(View.GONE);          }      }        @Override      public void onConnected(Bundle bundle) {        }        @Override      public void onConnectionSuspended(int i) {        }        @Override      public void onConnectionFailed(ConnectionResult connectionResult) {        }  }    

Login xml file for the Activity login xml

  <?xml version="1.0" encoding="utf-8"?>  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"      xmlns:app="http://schemas.android.com/apk/res-auto"      xmlns:tools="http://schemas.android.com/tools"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:paddingBottom="@dimen/activity_vertical_margin"      android:paddingLeft="@dimen/activity_horizontal_margin"      android:paddingRight="@dimen/activity_horizontal_margin"      android:paddingTop="@dimen/activity_vertical_margin"      app:layout_behavior="@string/appbar_scrolling_view_behavior"      tools:context="num.app.xxx.xxxx.xxxx.Login"      tools:showIn="@layout/Login">        <com.google.android.gms.common.SignInButton          android:id="@+id/sign_in_button"          android:layout_width="fill_parent"          android:layout_height="wrap_content" />        <LinearLayout          android:id="@+id/sign_out_and_disconnect"          android:layout_width="fill_parent"          android:layout_height="fill_parent"          android:orientation="vertical"          android:visibility="gone"          tools:visibility="visible"          android:layout_centerHorizontal="true"          android:layout_alignParentTop="true"          android:layout_alignParentBottom="true">            <TextView              android:id="@+id/userName"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text=" user name:"              android:layout_marginTop="15dp"              android:layout_marginLeft="10dp"              android:textColor="@android:color/black"                android:textSize="14sp"              />            <TextView              android:id="@+id/emailId"              android:layout_width="wrap_content"              android:layout_height="wrap_content"              android:text="email id:"              android:textColor="@android:color/black"              android:layout_below="@+id/userName"              android:layout_marginTop="15dp"              android:layout_marginLeft="10dp"              android:textSize="14sp"              />            <Button              android:id="@+id/sign_out_button"              android:layout_width="match_parent"              android:layout_height="wrap_content"              android:layout_gravity="center_vertical"              android:text="Google Sign out"/>            <Button              android:id="@+id/disconnect_button"              android:layout_width="match_parent"              android:layout_height="wrap_content"              android:layout_gravity="center_vertical"              android:text="revoke Google Access"/>        </LinearLayout>    </RelativeLayout>    

No comments:

Post a Comment