我在我的Android应用程序中运行后台服务.我使用从登录活动中获取的IdToken在后端服务器上进行身份验证.该服务正在START_STICKY模式下运行,因此即使应用程序关闭,该服务仍会在后台运行以从后端服务器获取任何通知.我面临的问题是当IdToken过期时,我无法在服务中更新它.如果令牌已过期,则回调函数不会收到任何结果.如果令牌尚未过期,它会立即获得结果.
这是signIn函数和handleSignIn函数的代码.
private void signIn() {
new Thread(new Runnable() { public void run() {
GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.requestIdToken("<My server_client_id>")
.build();
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(context)
.addApi(Auth.GOOGLE_SIGN_IN_API, gso)
.build();
OptionalPendingResult<GoogleSignInResult> opr = Auth.GoogleSignInApi.silentSignIn
(mGoogleApiClient);
if (opr.isDone()) {
// If the user's cached credentials are valid, the OptionalPendingResult will be "done"
// and the GoogleSignInResult will be available instantly.
Log.d(TAG, "Got cached sign-in");
GoogleSignInResult result = opr.get();
handleSignInResult(result);
} else {
// If the user has not …Run Code Online (Sandbox Code Playgroud)