Atlas加载Google登录的远程Bundle设计
采用startActivity方式
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的背景为透明,让用户看不出有页面跳转的痕迹
<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>
初始化
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);
}
}
登录
//登录
public void googleAuthSignIn() {
Intent signInIntent = mGoogleSignInClient.getSignInIntent();
startActivityForResult(signInIntent, REQUEST_GOOGLE_SIGNIN);
}
登录结果返回并处理
//登录结果返回
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();
}
若登录结果返回报如下类似错误:
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
版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/18/atlas-load-remote-bundle-design-for-google-login/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。
THE END
1
二维码
打赏
海报
Atlas加载Google登录的远程Bundle设计
采用startActivity方式
AppBundleHelper.bundleExplicitly(mActivity, "google", "me.yezhou.lib", new AtlasBundleLoadedListener() {
……
文章目录
关闭
共有 0 条评论