android - Login automatically after signing in with Google Login -


i have integrated sign in google feature in application. question is, how store information user can skip login screen next time , go straight application without needing sign in again?

this code far:

public class login extends appcompatactivity implements googleapiclient.onconnectionfailedlistener {      @bind(r.id.googlesignin)signinbutton googlesignin;     googlesigninoptions options;     googleapiclient client;      private static final int google_sign_in = 101;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.login);         butterknife.bind(this);          /** configure google sign in options **/         options = new googlesigninoptions.builder(googlesigninoptions.default_sign_in)                 .requestemail()                 .requestprofile().build();         client = new googleapiclient.builder(this)                 .enableautomanage(this, this)                 .addapi(auth.google_sign_in_api, options)                 .addapi(plus.api)                 .build();          /** configure sign in button **/         googlesignin.setsize(signinbutton.size_wide);         googlesignin.setscopes(options.getscopearray());         googlesignin.setonclicklistener(new view.onclicklistener() {             @override             public void onclick(view view) {                 intent signin = auth.googlesigninapi.getsigninintent(client);                 startactivityforresult(signin, google_sign_in);             }         });     }      @override     protected void onactivityresult(int requestcode, int resultcode, intent data) {         super.onactivityresult(requestcode, resultcode, data);          if (requestcode == google_sign_in && resultcode == result_ok)   {             googlesigninresult result = auth.googlesigninapi.getsigninresultfromintent(data);             googlesigninaccount account = result.getsigninaccount();             string name = account.getdisplayname();             log.e("display name", name);             string email = account.getemail();             log.e("user email", email);             string profile = string.valueof(account.getphotourl());             log.e("user profile", profile);         } else {             toast.maketext(getapplicationcontext(), "login failed!!", toast.length_short).show();         }     }      @override     public void onconnectionfailed(@nonnull connectionresult connectionresult) {      }      @override     protected void attachbasecontext(context newbase) {         super.attachbasecontext(calligraphycontextwrapper.wrap(newbase));     } } 

i haven't found persisting google login when user closes app , opens anytime later. tutorials deal integration part have working. bunch of questions on entirely different too. nothing in context.

would need store account data in sharedpreferences? or sdk handle part itself.

i appreciate folks can offer.

p.s.: question different previous question creating record in mysql database.

actually, turned out, simpler suggested in 2 answers. since both answers didn't offer substantial other suggestions of sorts, searched more. led finding google+ developers live youtube video. , in description of video link quick start link led further https://github.com/googlesamples/google-services.git.

the quick start sample on github page demonstrated precisely looking for.

i posting updated version of code posted in op other users might stumble on question , save them time hopefully.

beside slight structural changes, important addition of onstart() uses optionalpendingresult<googlesigninresult> silently determines if active session of signed in account exists in case, sign in automatically. if session has expired, create new session without additional code. if...else block in onstart() key 2 possibilities.

public class newlogin extends appcompatactivity implements googleapiclient.onconnectionfailedlistener {      @onclick(r.id.googlesignin) public void googlelogin()   {         intent signin = auth.googlesigninapi.getsigninintent(client);         startactivityforresult(signin, google_sign_in);     }     googlesigninoptions options;     googleapiclient client;      private progressdialog mprogressdialog;      private static final int google_sign_in = 101;      @override     protected void oncreate(@nullable bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.login);         butterknife.bind(this);          /** configure google sign in options **/         options = new googlesigninoptions.builder(googlesigninoptions.default_sign_in)                 .requestemail()                 .requestprofile().build();         client = new googleapiclient.builder(this)                 .enableautomanage(this, this)                 .addapi(auth.google_sign_in_api, options)                 .addapi(plus.api)                 .build();     }      @override     public void onactivityresult(int requestcode, int resultcode, intent data) {         super.onactivityresult(requestcode, resultcode, data);          if (requestcode == google_sign_in) {             googlesigninresult result = auth.googlesigninapi.getsigninresultfromintent(data);             handlesigninresult(result);         }     }      @override     protected void onstart() {         super.onstart();          optionalpendingresult<googlesigninresult> opr = auth.googlesigninapi.silentsignin(client);         if (opr.isdone()) {             googlesigninresult result = opr.get();             handlesigninresult(result);             log.e("cache status", "got cached sign-in");         } else {             showprogressdialog();             opr.setresultcallback(new resultcallback<googlesigninresult>() {                 @override                 public void onresult(googlesigninresult googlesigninresult) {                     hideprogressdialog();                     handlesigninresult(googlesigninresult);                 }             });         }     }      // [start handlesigninresult]     private void handlesigninresult(googlesigninresult result) {         log.d("handlesigninresult:", string.valueof(result.issuccess()));         if (result.issuccess()) {             googlesigninaccount acct = result.getsigninaccount();             toast.maketext(getapplicationcontext(), acct.getdisplayname(), toast.length_short).show();             string name = acct.getdisplayname();             log.e("display name", name);             string email = acct.getemail();             log.e("user email", email);             string profile = string.valueof(acct.getphotourl());             log.e("user profile", profile);             log.e("id", acct.getid());         }     }      private void showprogressdialog() {         if (mprogressdialog == null) {             mprogressdialog = new progressdialog(this);             mprogressdialog.setmessage("loading....");             mprogressdialog.setindeterminate(true);         }          mprogressdialog.show();     }      private void hideprogressdialog() {         if (mprogressdialog != null && mprogressdialog.isshowing()) {             mprogressdialog.hide();         }     }      @override     public void onconnectionfailed(@nonnull connectionresult connectionresult) {      }      @override     protected void attachbasecontext(context newbase) {         super.attachbasecontext(calligraphycontextwrapper.wrap(newbase));     } } 

Comments