应用程序应该在第一次启动时对用户进行身份验证(如果第一次尝试失败,则应在未来启动时)。身份验证逻辑封装在AsyncTask
中,并通过以下方法实现(用户帐户和范围有效):
private String fetchToken() throws IOException {
try {
return GoogleAuthUtil.getToken(mParentActivity, mUserEmail, mScope);
} catch (UserRecoverableAuthException userRecoverableException) {
if (!secondTry) {
secondTry = true;
mParentActivity.handleException(userRecoverableException);
}
} catch (GoogleAuthException fatalException) {
fatalException.printStackTrace();
mParentActivity.handleException(fatalException);
}
return null;
}
handleException
方法:
public void handleException(final Exception e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (e instanceof GooglePlayServicesAvailabilityException) {
int statusCode = ((GooglePlayServicesAvailabilityException) e).getConnectionStatusCode();
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(statusCode, MainActivity.this,
REQUEST_CODE_RECOVER_FROM_PLAY_SERVICES_ERROR);
dialog.show();
} else if (e instanceof UserRecoverableAuthException) {
Intent intent = ((UserRecoverableAuthException) e).getIntent();
startActivityForResult(intent, REQUEST_CODE_RECOVER_FROM_AUTH_ERROR);
} else if (e instanceof GoogleAuthException) {
showToast("Fatal error during authentication");
}
}
});
}
在不同设备上测试此代码表明没有严格的模式来检测错误原因。症状是:
UserRecoverableAuthException
标志以防止它,也会抛出多个secondTry
; 此外,在LogCat中发现以下例外情况:
11-10 13:50:30.610: D/GLSActivity(4223): Starting LoginActivityTask for user: qwerty@gmail.com...
11-10 13:50:30.615: E/Parcel(2345): Class not found when unmarshalling: com.google.android.gms.auth.firstparty.shared.LatencyTracker
11-10 13:50:30.615: E/Parcel(2345): java.lang.ClassNotFoundException: com.google.android.gms.auth.firstparty.shared.LatencyTracker
11-10 13:50:30.615: E/Parcel(2345): at java.lang.Class.classForName(Native Method)
11-10 13:50:30.615: E/Parcel(2345): at java.lang.Class.forName(Class.java:204)
11-10 13:50:30.615: E/Parcel(2345): at java.lang.Class.forName(Class.java:169)
11-10 13:50:30.615: E/Parcel(2345): at android.os.Parcel.readParcelableCreator(Parcel.java:2091)
11-10 13:50:30.615: E/Parcel(2345): at android.os.Parcel.readParcelable(Parcel.java:2055)
11-10 13:50:30.615: E/Parcel(2345): at android.os.Parcel.readValue(Parcel.java:1971)
11-10 13:50:30.615: E/Parcel(2345): at android.os.Parcel.readMapInternal(Parcel.java:2255)
11-10 13:50:30.615: E/Parcel(2345): at android.os.Bundle.unparcel(Bundle.java:223)
11-10 13:50:30.615: E/Parcel(2345): at android.os.Bundle.getString(Bundle.java:1082)
11-10 13:50:30.615: E/Parcel(2345): at android.content.Intent.getStringExtra(Intent.java:4961)
11-10 13:50:30.615: E/Parcel(2345): at com.android.server.am.ActivityStack.startActivityLocked(ActivityStack.java:3761)
11-10 13:50:30.615: E/Parcel(2345): at com.android.server.am.ActivityStack.startActivityMayWait(ActivityStack.java:4977)
11-10 13:50:30.615: E/Parcel(2345): at com.android.server.am.ActivityManagerService.startActivityAsUser(ActivityManagerService.java:3173)
11-10 13:50:30.615: E/Parcel(2345): at com.android.server.am.ActivityManagerService.startActivity(ActivityManagerService.java:3129)
11-10 13:50:30.615: E/Parcel(2345): at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:157)
11-10 13:50:30.615: E/Parcel(2345): at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2125)
11-10 13:50:30.615: E/Parcel(2345): at android.os.Binder.execTransact(Binder.java:388)
11-10 13:50:30.615: E/Parcel(2345): at dalvik.system.NativeStart.run(Native Method)
11-10 13:50:30.615: E/Parcel(2345): Caused by: java.lang.NoClassDefFoundError: com/google/android/gms/auth/firstparty/shared/LatencyTracker
11-10 13:50:30.615: E/Parcel(2345): ... 18 more
11-10 13:50:30.615: E/Parcel(2345): Caused by: java.lang.ClassNotFoundException: Didnt find class "com.google.android.gms.auth.firstparty.shared.LatencyTracker" on path: .
11-10 13:50:30.615: E/Parcel(2345): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64)
11-10 13:50:30.615: E/Parcel(2345): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-10 13:50:30.615: E/Parcel(2345): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-10 13:50:30.615: E/Parcel(2345): ... 18 more
...
11-10 13:50:30.625: E/Parcel(2345): Class not found when unmarshalling: com.google.android.gms.auth.firstparty.shared.AppDescription
11-10 13:50:30.625: E/Parcel(2345): java.lang.ClassNotFoundException: com.google.android.gms.auth.firstparty.shared.AppDescription
11-10 13:50:30.625: E/Parcel(2345): at java.lang.Class.classForName(Native Method)
11-10 13:50:30.625: E/Parcel(2345): at java.lang.Class.forName(Class.java:204)
11-10 13:50:30.625: E/Parcel(2345): at java.lang.Class.forName(Class.java:169)
11-10 13:50:30.625: E/Parcel(2345): at android.os.Parcel.readParcelableCreator(Parcel.java:2091)
11-10 13:50:30.625: E/Parcel(2345): at android.os.Parcel.readParcelable(Parcel.java:2055)
11-10 13:50:30.625: E/Parcel(2345): at android.os.Parcel.readValue(Parcel.java:1971)
11-10 13:50:30.625: E/Parcel(2345): at android.os.Parcel.readMapInternal(Parcel.java:2255)
11-10 13:50:30.625: E/Parcel(2345): at android.os.Bundle.unparcel(Bundle.java:223)
11-10 13:50:30.625: E/Parcel(2345): at android.os.Bundle.getString(Bundle.java:1082)
11-10 13:50:30.625: E/Parcel(2345): at android.content.Intent.getStringExtra(Intent.java:4961)
11-10 13:50:30.625: E/Parcel(2345): at com.android.server.am.ActivityStack.startActivityLocked(ActivityStack.java:3761)
11-10 13:50:30.625: E/Parcel(2345): at com.android.server.am.ActivityStack.startActivityMayWait(ActivityStack.java:4977)
11-10 13:50:30.625: E/Parcel(2345): at com.android.server.am.ActivityManagerService.startActivityInPackage(ActivityManagerService.java:3391)
11-10 13:50:30.625: E/Parcel(2345): at com.android.server.am.PendingIntentRecord.sendInner(PendingIntentRecord.java:254)
11-10 13:50:30.625: E/Parcel(2345): at com.android.server.am.ActivityManagerService.startActivityIntentSender(ActivityManagerService.java:3283)
11-10 13:50:30.625: E/Parcel(2345): at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:258)
11-10 13:50:30.625: E/Parcel(2345): at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2125)
11-10 13:50:30.625: E/Parcel(2345): at android.os.Binder.execTransact(Binder.java:388)
11-10 13:50:30.625: E/Parcel(2345): at dalvik.system.NativeStart.run(Native Method)
11-10 13:50:30.625: E/Parcel(2345): Caused by: java.lang.NoClassDefFoundError: com/google/android/gms/auth/firstparty/shared/AppDescription
11-10 13:50:30.625: E/Parcel(2345): ... 19 more
11-10 13:50:30.625: E/Parcel(2345): Caused by: java.lang.ClassNotFoundException: Didnt find class "com.google.android.gms.auth.firstparty.shared.AppDescription" on path: .
11-10 13:50:30.625: E/Parcel(2345): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64)
11-10 13:50:30.625: E/Parcel(2345): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
11-10 13:50:30.625: E/Parcel(2345): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-10 13:50:30.625: E/Parcel(2345): ... 19 more
有没有人有什么建议导致这个问题?