Atlas加载Google登录的远程Bundle设计

采用startActivity方式

1
2
3
4
5
6
7
AppBundleHelper.bundleExplicitly(mActivity, "google", "me.yezhou.lib", new AtlasBundleLoadedListener() {
@Override
public void onBundleLoaded() {
AtlasDelegateHelper.startBundleActivityForResult(mActivity, ActivityConfig.ACTIVITY_GOOGLE_SIGNIN, IntentCode.REQUEST_GOOGLE_SIGNIN.ordinal());
mActivity.overridePendingTransition(R.anim.push_bottom_in, R.anim.push_bottom_out);
}
});

注意设置GoogleSignInActivity的背景为透明,让用户看不出有页面跳转的痕迹

1
2
3
4
5
<style name="Transparent" parent="AppTheme">
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsTranslucent">true</item>
</style>

初始化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public void initGoogleAuth() {
GoogleSignInOptions gso = new GoogleSignInOptions
.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestIdToken(getResources().getString(R.string.server_client_id))
.requestId()
.requestEmail()
.requestProfile()
.build();

mGoogleSignInClient = GoogleSignIn.getClient(this, gso);
//如果用戶已经登录返回account对象,没有登录 返回null
GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);
if (account == null) {
//初始化完成,检查到google账户没有登录
googleAuthSignIn();
} else {
//初始化完成,检查到google账户已经登录,返回账号信息
getGoogleUserInfo(account);
}
}

登录

1
2
3
4
5
//登录
public void googleAuthSignIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, REQUEST_GOOGLE_SIGNIN);
}

登录结果返回并处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
//登录结果返回
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
if (requestCode == REQUEST_GOOGLE_SIGNIN) {
// The Task returned from this call is always completed, no need to attach
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
handleSignInResult(task);
}
}

//处理登录结果,并回调登录结果
private void handleSignInResult(Task<GoogleSignInAccount> completedTask) {
try {
GoogleSignInAccount account = completedTask.getResult(ApiException.class);
// Signed in successfully, show authenticated UI.
getGoogleUserInfo(account);

} catch (ApiException e) {
// The ApiException status code indicates the detailed failure reason.
// Please refer to the GoogleSignInStatusCodes class reference for more information.
NLog.w(TAG, "signInResult:failed code=" + e.getStatusCode());
NLog.d(TAG, e.getMessage());
e.printStackTrace();
setResult(RESULT_CANCELED);
finish();
}
}

public void getGoogleUserInfo(GoogleSignInAccount account) {
String id = account.getId();
String name = account.getDisplayName();
String familyName = account.getFamilyName();
String givenName = account.getGivenName();
String email = account.getEmail();
Uri photoUri = account.getPhotoUrl();
String photoUrl = photoUri != null ? photoUri.toString() : "";
String token = account.getIdToken();
long expirationTimeSecs = account.getExpirationTimeSecs();
NLog.i(TAG, "name: " + name + ", email: " + email);

GoogleAccess googleAccess = new GoogleAccess();
googleAccess.setId(id);
googleAccess.setName(name);
googleAccess.setFamilyName(familyName);
googleAccess.setGivenName(givenName);
googleAccess.setEmail(email);
googleAccess.setPhotoUrl(photoUrl);
googleAccess.setToken(token);
googleAccess.setExpirationTimeSecs(expirationTimeSecs);

Intent intent = new Intent();
intent.putExtra("google_access", googleAccess);
setResult(RESULT_OK, intent);
finish();
}

若登录结果返回报如下类似错误:

1
2
3
4
com.google.android.gms.common.api.ApiException: 8: 
at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(Unknown Source)
at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(Unknown Source)
at me.yezhou.lib.google.signin.GoogleSignActivity.onActivityResult(GoogleSignActivity.java:73)

可根据结果码查询错误原因:https://developers.google.com/android/reference/com/google/android/gms/common/api/CommonStatusCodes

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :