我会避免在共享首选项中保存令牌,而是尝试执行如下所述的静默登录:http://android-developers.blogspot.de/2015/12/api-updates-for-sign-in-with -google.html
这样做的好处是可以让您验证用户是否仍然登录并最终让您提示他请求重新登录.
这里有一些代码(基本上是从链接中的示例改编而来):
public void silentLogin() { OptionalPendingResultpendingResult = Auth.GoogleSignInApi.silentSignIn(googleApiClient); if (pendingResult != null) { handleGooglePendingResult(pendingResult); } else { //no result from silent login. Possibly display the login page again } } private void handleGooglePendingResult(OptionalPendingResult pendingResult) { if (pendingResult.isDone()) { // There's immediate result available. GoogleSignInResult signInResult = pendingResult.get(); onSilentSignInCompleted(signInResult); } else { // There's no immediate result ready, waits for the async callback. pendingResult.setResultCallback(new ResultCallback () { @Override public void onResult(@NonNull GoogleSignInResult signInResult) { onSilentSignInCompleted(signInResult, callback); } }); } } private void onSilentSignInCompleted(GoogleSignInResult signInResult) { GoogleSignInAccount signInAccount = signInResult.getSignInAccount(); if (signInAccount != null) { // you have a valid sign in account. Skip the login. } else { // you don't have a valid sign in account. Eventually display the login page again } }