{"id":1240,"date":"2023-03-18T09:31:07","date_gmt":"2023-03-18T01:31:07","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1240"},"modified":"2023-04-29T09:33:25","modified_gmt":"2023-04-29T01:33:25","slug":"atlas-load-remote-bundle-design-for-google-login","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/18\/atlas-load-remote-bundle-design-for-google-login\/","title":{"rendered":"Atlas\u52a0\u8f7dGoogle\u767b\u5f55\u7684\u8fdc\u7a0bBundle\u8bbe\u8ba1"},"content":{"rendered":"<h2>\u91c7\u7528startActivity\u65b9\u5f0f<\/h2>\n<pre><code class=\"language-java\">AppBundleHelper.bundleExplicitly(mActivity, &quot;google&quot;, &quot;me.yezhou.lib&quot;, new AtlasBundleLoadedListener() {\n        @Override\n        public void onBundleLoaded() {\n            AtlasDelegateHelper.startBundleActivityForResult(mActivity, ActivityConfig.ACTIVITY_GOOGLE_SIGNIN, IntentCode.REQUEST_GOOGLE_SIGNIN.ordinal());\n            mActivity.overridePendingTransition(R.anim.push_bottom_in, R.anim.push_bottom_out);\n        }\n    });<\/code><\/pre>\n<p><!-- more --><\/p>\n<blockquote>\n<p>\u6ce8\u610f\u8bbe\u7f6eGoogleSignInActivity\u7684\u80cc\u666f\u4e3a\u900f\u660e\uff0c\u8ba9\u7528\u6237\u770b\u4e0d\u51fa\u6709\u9875\u9762\u8df3\u8f6c\u7684\u75d5\u8ff9<\/p>\n<\/blockquote>\n<pre><code class=\"language-xml\">&lt;style name=&quot;Transparent&quot; parent=&quot;AppTheme&quot;&gt;\n    &lt;item name=&quot;android:windowBackground&quot;&gt;@color\/transparent&lt;\/item&gt;\n    &lt;item name=&quot;android:windowNoTitle&quot;&gt;true&lt;\/item&gt;\n    &lt;item name=&quot;android:windowIsTranslucent&quot;&gt;true&lt;\/item&gt;\n&lt;\/style&gt;<\/code><\/pre>\n<h2>\u521d\u59cb\u5316<\/h2>\n<pre><code class=\"language-java\">public void initGoogleAuth() {\n    GoogleSignInOptions gso = new GoogleSignInOptions\n            .Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)\n            .requestIdToken(getResources().getString(R.string.server_client_id))\n            .requestId()\n            .requestEmail()\n            .requestProfile()\n            .build();\n\n    mGoogleSignInClient = GoogleSignIn.getClient(this, gso);\n    \/\/\u5982\u679c\u7528\u6236\u5df2\u7ecf\u767b\u5f55\u8fd4\u56deaccount\u5bf9\u8c61\uff0c\u6ca1\u6709\u767b\u5f55 \u8fd4\u56denull\n    GoogleSignInAccount account = GoogleSignIn.getLastSignedInAccount(this);\n    if (account == null) {\n        \/\/\u521d\u59cb\u5316\u5b8c\u6210\uff0c\u68c0\u67e5\u5230google\u8d26\u6237\u6ca1\u6709\u767b\u5f55\n        googleAuthSignIn();\n    } else {\n        \/\/\u521d\u59cb\u5316\u5b8c\u6210\uff0c\u68c0\u67e5\u5230google\u8d26\u6237\u5df2\u7ecf\u767b\u5f55\uff0c\u8fd4\u56de\u8d26\u53f7\u4fe1\u606f\n        getGoogleUserInfo(account);\n    }\n}<\/code><\/pre>\n<h2>\u767b\u5f55<\/h2>\n<pre><code class=\"language-java\">\/\/\u767b\u5f55\npublic void googleAuthSignIn() {\n    Intent signInIntent = mGoogleSignInClient.getSignInIntent();\n    startActivityForResult(signInIntent, REQUEST_GOOGLE_SIGNIN);\n}<\/code><\/pre>\n<h2>\u767b\u5f55\u7ed3\u679c\u8fd4\u56de\u5e76\u5904\u7406<\/h2>\n<pre><code class=\"language-java\">\/\/\u767b\u5f55\u7ed3\u679c\u8fd4\u56de\npublic void onActivityResult(int requestCode, int resultCode, Intent data) {\n    super.onActivityResult(requestCode, resultCode, data);\n    \/\/ Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);\n    if (requestCode == REQUEST_GOOGLE_SIGNIN) {\n        \/\/ The Task returned from this call is always completed, no need to attach\n        Task&lt;GoogleSignInAccount&gt; task = GoogleSignIn.getSignedInAccountFromIntent(data);\n        handleSignInResult(task);\n    }\n}\n\n\/\/\u5904\u7406\u767b\u5f55\u7ed3\u679c\uff0c\u5e76\u56de\u8c03\u767b\u5f55\u7ed3\u679c\nprivate void handleSignInResult(Task&lt;GoogleSignInAccount&gt; completedTask) {\n    try {\n        GoogleSignInAccount account = completedTask.getResult(ApiException.class);\n        \/\/ Signed in successfully, show authenticated UI.\n        getGoogleUserInfo(account);\n\n    } catch (ApiException e) {\n        \/\/ The ApiException status code indicates the detailed failure reason.\n        \/\/ Please refer to the GoogleSignInStatusCodes class reference for more information.\n        NLog.w(TAG, &quot;signInResult:failed code=&quot; + e.getStatusCode());\n        NLog.d(TAG, e.getMessage());\n        e.printStackTrace();\n        setResult(RESULT_CANCELED);\n        finish();\n    }\n}\n\npublic void getGoogleUserInfo(GoogleSignInAccount account) {\n    String id = account.getId();\n    String name = account.getDisplayName();\n    String familyName = account.getFamilyName();\n    String givenName = account.getGivenName();\n    String email = account.getEmail();\n    Uri photoUri = account.getPhotoUrl();\n    String photoUrl = photoUri != null ? photoUri.toString() : &quot;&quot;;\n    String token = account.getIdToken();\n    long expirationTimeSecs = account.getExpirationTimeSecs();\n    NLog.i(TAG, &quot;name: &quot; + name + &quot;, email: &quot; + email);\n\n    GoogleAccess googleAccess = new GoogleAccess();\n    googleAccess.setId(id);\n    googleAccess.setName(name);\n    googleAccess.setFamilyName(familyName);\n    googleAccess.setGivenName(givenName);\n    googleAccess.setEmail(email);\n    googleAccess.setPhotoUrl(photoUrl);\n    googleAccess.setToken(token);\n    googleAccess.setExpirationTimeSecs(expirationTimeSecs);\n\n    Intent intent = new Intent();\n    intent.putExtra(&quot;google_access&quot;, googleAccess);\n    setResult(RESULT_OK, intent);\n    finish();\n}<\/code><\/pre>\n<p>\u82e5\u767b\u5f55\u7ed3\u679c\u8fd4\u56de\u62a5\u5982\u4e0b\u7c7b\u4f3c\u9519\u8bef\uff1a<\/p>\n<pre><code>com.google.android.gms.common.api.ApiException: 8: \n    at com.google.android.gms.common.internal.ApiExceptionUtil.fromStatus(Unknown Source)\n    at com.google.android.gms.auth.api.signin.GoogleSignIn.getSignedInAccountFromIntent(Unknown Source)\n    at me.yezhou.lib.google.signin.GoogleSignActivity.onActivityResult(GoogleSignActivity.java:73)<\/code><\/pre>\n<p>\u53ef\u6839\u636e\u7ed3\u679c\u7801\u67e5\u8be2\u9519\u8bef\u539f\u56e0\uff1a<a target=\"_blank\" rel=\"noopener\" href=\"https:\/\/developers.google.com\/android\/reference\/com\/google\/android\/gms\/common\/api\/CommonStatusCodes\">https:\/\/developers.google.com\/android\/reference\/com\/google\/android\/gms\/common\/api\/CommonStatusCodes<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u91c7\u7528startActivity\u65b9\u5f0f AppBundleHelper.bundleExplicitly(mAct [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[275],"tags":[239],"class_list":["post-1240","post","type-post","status-publish","format-standard","hentry","category-atlas","tag-google"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1240","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1240"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1240\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1240"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1240"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1240"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}