当用户转到另一个活动并返回MainActivity.class时,屏幕闪烁。

问题描述 投票:2回答:1
我的用户已登录,但是当我在下面的代码行中调用时,如果用户已登录,则我需要权限错误。

mDriveServiceHelper = new DriveServiceHelper(getGoogleDriveService(getApplicationContext(), alreadyloggedAccount, "appname")); public static Drive getGoogleDriveService(Context context, GoogleSignInAccount account, String appName) { GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2( context, Collections.singleton(DriveScopes.DRIVE_FILE)); credential.setSelectedAccount(account.getAccount()); com.google.api.services.drive.Drive googleDriveService = new com.google.api.services.drive.Drive.Builder( AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential) .setApplicationName(appName) .build(); return googleDriveService; }

就我而言,每次必须调用以下方法时,它才有效。

当我调用

requestSignIn()

然后是[startActivityForResult(googleSignInClient.getSignInIntent(), REQUEST_CODE_SIGN_IN)时。那么只有当用户没有权限时,驱动器访问弹出窗口才会出现,而下一次当用户登录到应用程序时,驱动器访问弹出窗口将不会出现(这是预期的)。但是我认为该应用程序正在尝试打开某些内容(某些活动或Google驱动器弹出窗口),并且随着用户的许可,它没有显示弹出窗口,但是每次我们转到另一个活动并返回mainactivity.class时,屏幕就会闪烁一次。 (此Google驱动器代码是在主要活动中编写的) private void requestSignIn() { Log.d(TAG, "Requesting sign-in"); GoogleSignInOptions signInOptions = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestScopes(new Scope(DriveScopes.DRIVE_FILE)) .requestEmail() .build(); googleSignInClient = GoogleSignIn.getClient(this, signInOptions); } @Override public void onActivityResult(int requestCode, int resultCode, Intent resultData) { switch (requestCode) { case REQUEST_CODE_SIGN_IN: if (resultCode == Activity.RESULT_OK && resultData != null) { handleSignInResult(resultData); } break; } super.onActivityResult(requestCode, resultCode, resultData); } String TAG = ":::: Backup User Data ::::::::::"; private void handleSignInResult(Intent result) { GoogleSignIn.getSignedInAccountFromIntent(result) .addOnSuccessListener(googleAccount -> { //System.out.println(">>>>>>>>>>>> handleSignInResult inside Mainactivity>>>>>>>>>>>>>>>. "); Log.d(TAG, "Signed in as " + googleAccount.getEmail()); // Use the authenticated account to sign in to the Drive service. GoogleAccountCredential credential = GoogleAccountCredential.usingOAuth2( this, Collections.singleton(DriveScopes.DRIVE_FILE)); credential.setSelectedAccount(googleAccount.getAccount()); Drive googleDriveService = new Drive.Builder( AndroidHttp.newCompatibleTransport(), new GsonFactory(), credential) .setApplicationName("Drive API Migration") .build(); // The DriveServiceHelper encapsulates all REST API and SAF functionality. // Its instantiation is required before handling any onClick actions. mDriveServiceHelper = new DriveServiceHelper(googleDriveService); }) .addOnFailureListener(exception -> Log.e(TAG, "Unable to sign in.", exception)); }
我不希望每次我转到另一个屏幕并返回MainActivity.class时,黑线都应从屏幕上通过。 (我认为黑色正在从屏幕上消失,因为每当我进入MainActivity.class时,我的代码都会再次检查google驱动器访问权限并尝试显示弹出窗口,但由于用户有权限,弹出窗口正在消失)

您能帮我解决上述问题吗?

我的用户已登录,但是当我在下面的代码行中调用时,如果用户已登录,则会出现需要权限错误。 mDriveServiceHelper = new DriveServiceHelper(getGoogleDriveService(...

android android-studio google-drive-api
1个回答
0
投票
研究相关类的源代码,我发现此函数GoogleSignIn.getLastSignedInAccount应该有用。

第一次调用requestSignIn()。如果以前登录过,请使用下面的函数代替requestSignIn()。

© www.soinside.com 2019 - 2024. All rights reserved.